aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2023-10-23 19:38:58 -0700
committerJohn MacFarlane <[email protected]>2023-10-23 21:08:27 -0700
commit6f3ec91a0d64f90e955c51fbcff9f3a7f639f62c (patch)
tree9452900b6bb090a4d137d62dd6d5990334580637 /src
parentd200cacb5b83f75c9c36d2de04880576a60a1640 (diff)
CommonMark reader: handle `Ext_tex_math_gfm`.
Parse GFM-specific math constructions when `tex_math_gfm` enabled. See <https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/writing-mathematical-expressions>. Closes #9121.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Readers/CommonMark.hs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Readers/CommonMark.hs b/src/Text/Pandoc/Readers/CommonMark.hs
index ad71e5c82..08f37830e 100644
--- a/src/Text/Pandoc/Readers/CommonMark.hs
+++ b/src/Text/Pandoc/Readers/CommonMark.hs
@@ -100,6 +100,9 @@ readCommonMarkBody opts s toks =
(if isEnabled Ext_implicit_figures opts
then walk makeFigures
else id) .
+ (if isEnabled Ext_tex_math_gfm opts
+ then walk handleGfmMath
+ else id) .
(if readerStripComments opts
then walk stripBlockComments . walk stripInlineComments
else id) <$>
@@ -111,6 +114,21 @@ readCommonMarkBody opts s toks =
Left err -> throwError $ fromParsecError s err
Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
+handleGfmMath :: Block -> Block
+handleGfmMath (CodeBlock ("",["math"],[]) raw) = Para [Math DisplayMath raw]
+handleGfmMath x = walk handleGfmMathInline x
+
+handleGfmMathInline :: Inline -> Inline
+handleGfmMathInline (Math InlineMath math') =
+ let (ticks, rest) = T.span (== '`') math'
+ in if T.null ticks
+ then Math InlineMath math'
+ else case T.stripSuffix ticks rest of
+ Just middle | not (T.null middle) && (T.last middle /= '`')
+ -> Math InlineMath middle
+ _ -> Math InlineMath math'
+handleGfmMathInline x = x
+
stripBlockComments :: Block -> Block
stripBlockComments (RawBlock (B.Format "html") s) =
RawBlock (B.Format "html") (removeComments s)