Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions commonmark-extensions/src/Commonmark/Extensions/Math.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import Commonmark.Html
import Text.Parsec
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Map.Strict as M

mathSpec :: (Monad m, IsBlock il bl, IsInline il, HasMath il)
=> SyntaxSpec m il bl
mathSpec = mempty
{ syntaxInlineParsers = [withAttributes parseMath]
-- A very odd workaround. Is there a better way to:
-- 1. Get the precedingTokTypes map populated with info about '$'
-- 2. Have the math spans otherwise follow the same precedence as code spans, not emphasis
, syntaxFormattingSpecs = [FormattingSpec '$' False False Nothing Nothing '$']
}

class HasMath a where
Expand All @@ -38,13 +43,16 @@ instance (HasMath i, Monoid i) => HasMath (WithSourceMap i) where

parseMath :: (Monad m, HasMath a) => InlineParser m a
parseMath = try $ do
symbol '$'
Tok _ pos _ <- symbol '$'
display <- (True <$ symbol '$') <|> (False <$ notFollowedBy whitespace)
contents <- try $ untokenize <$> pDollarsMath 0
st <- getState
let isPrecededByWordChars = M.lookup pos (precedingTokTypes st) == Just WordChars
isFollowedByWordChars <- (==) WordChars . maybe LineEnd tokType <$> optionMaybe (lookAhead anyTok)
let isWs c = c == ' ' || c == '\t' || c == '\r' || c == '\n'
if display
then displayMath contents <$ symbol '$'
else if T.null contents || isWs (T.last contents)
else if T.null contents || isWs (T.last contents) || isPrecededByWordChars || isFollowedByWordChars
-- don't allow math to end with SPACE + $
then mzero
else return $ inlineMath contents
Expand Down
15 changes: 15 additions & 0 deletions commonmark-extensions/test/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,18 @@ $b<a>c$
.
<p><span class="math inline">\(b&lt;a&gt;c\)</span></p>
````````````````````````````````

Inline math openers can't be immediately preceded by
alphanumeric characters:
```````````````````````````````` example
Expect$Nothing$
.
<p>Expect$Nothing$</p>
````````````````````````````````

Or immediately followed by them:
```````````````````````````````` example
./install.sh $PLATFORM-$VERSION
.
<p>./install.sh $PLATFORM-$VERSION</p>
````````````````````````````````
1 change: 1 addition & 0 deletions commonmark/src/Commonmark/Inlines.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Commonmark.Inlines
, pLinkTitle
, pEscaped
, pEscapedSymbol
, precedingTokTypes
, processEmphasis
, processBrackets
, pBacktickSpan
Expand Down
Loading