diff --git a/commonmark-extensions/src/Commonmark/Extensions/Math.hs b/commonmark-extensions/src/Commonmark/Extensions/Math.hs index a5a7c7d..ca94d9d 100644 --- a/commonmark-extensions/src/Commonmark/Extensions/Math.hs +++ b/commonmark-extensions/src/Commonmark/Extensions/Math.hs @@ -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 @@ -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 diff --git a/commonmark-extensions/test/math.md b/commonmark-extensions/test/math.md index f8d6dc3..6ba5d05 100644 --- a/commonmark-extensions/test/math.md +++ b/commonmark-extensions/test/math.md @@ -87,3 +87,18 @@ $bc$ .

\(b<a>c\)

```````````````````````````````` + +Inline math openers can't be immediately preceded by +alphanumeric characters: +```````````````````````````````` example +Expect$Nothing$ +. +

Expect$Nothing$

+```````````````````````````````` + +Or immediately followed by them: +```````````````````````````````` example +./install.sh $PLATFORM-$VERSION +. +

./install.sh $PLATFORM-$VERSION

+```````````````````````````````` diff --git a/commonmark/src/Commonmark/Inlines.hs b/commonmark/src/Commonmark/Inlines.hs index 1988666..59d58d3 100644 --- a/commonmark/src/Commonmark/Inlines.hs +++ b/commonmark/src/Commonmark/Inlines.hs @@ -26,6 +26,7 @@ module Commonmark.Inlines , pLinkTitle , pEscaped , pEscapedSymbol + , precedingTokTypes , processEmphasis , processBrackets , pBacktickSpan