@@ -241,9 +241,20 @@ noteToMarkdown opts num blocks = do
241241 else marker <> spacer <> contents
242242
243243-- | Escape special characters for Markdown.
244−escapeString :: String -> String
245−escapeString = escapeStringUsing markdownEscapes
246− where markdownEscapes = backslashEscapes "\\`*_$<>#~^"
244+escapeString :: WriterOptions -> String -> String
245+escapeString opts = escapeStringUsing markdownEscapes
246+ where markdownEscapes = backslashEscapes specialChars
247+ specialChars =
248+ (if isEnabled Ext_superscript opts
249+ then ('^':)
250+ else id) .
251+ (if isEnabled Ext_subscript opts
252+ then ('~':)
253+ else id) .
254+ (if isEnabled Ext_tex_math_dollars opts
255+ then ('$':)
256+ else id) $
257+ "\\`*_<>#"
247258
248259-- | Construct table of contents from list of header blocks.
249260tableOfContents :: WriterOptions -> [Block] -> Doc
@@ -742,11 +753,11 @@ inlineToMarkdown opts (Code attr str) = do
742753 if plain
743754 then return $ text str
744755 else return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs
745−inlineToMarkdown _ (Str str) = do
756+inlineToMarkdown opts (Str str) = do
746757 st <- get
747758 if stPlain st
748759 then return $ text str
749− else return $ text $ escapeString str
760+ else return $ text $ escapeString opts str
750761inlineToMarkdown opts (Math InlineMath str)
751762 | isEnabled Ext_tex_math_dollars opts =
752763 return $ "$" <> text str <> "$"