@@ -915,8 +915,11 @@ inlineToMarkdown opts (Superscript lst) =
915915 then "^" <> contents <> "^"
916916 else if isEnabled Ext_raw_html opts
917917 then "<sup>" <> contents <> "</sup>"
918− else text $ map toSuperscript
919− $ render Nothing contents
918+ else
919+ let rendered = render Nothing contents
920+ in case mapM toSuperscript rendered of
921+ Just r -> text r
922+ Nothing -> text $ "^(" ++ rendered ++ ")"
920923inlineToMarkdown opts (Subscript lst) =
921924 local (\env -> env {envEscapeSpaces = True}) $ do
922925 contents <- inlineListToMarkdown opts lst
@@ -924,8 +927,11 @@ inlineToMarkdown opts (Subscript lst) =
924927 then "~" <> contents <> "~"
925928 else if isEnabled Ext_raw_html opts
926929 then "<sub>" <> contents <> "</sub>"
927− else text $ map toSubscript
928− $ render Nothing contents
930+ else
931+ let rendered = render Nothing contents
932+ in case mapM toSubscript rendered of
933+ Just r -> text r
934+ Nothing -> text $ "_(" ++ rendered ++ ")"
929935inlineToMarkdown opts (SmallCaps lst) = do
930936 plain <- asks envPlain
931937 if not plain &&
@@ -1120,28 +1126,29 @@ makeMathPlainer = walk go
11201126 go (Emph xs) = Span nullAttr xs
11211127 go x = x
11221128
1123−toSuperscript :: Char -> Char
1124−toSuperscript '1' = '\x00B9'
1125−toSuperscript '2' = '\x00B2'
1126−toSuperscript '3' = '\x00B3'
1127−toSuperscript '+' = '\x207A'
1128−toSuperscript '-' = '\x207B'
1129−toSuperscript '=' = '\x207C'
1130−toSuperscript '(' = '\x207D'
1131−toSuperscript ')' = '\x207E'
1129+toSuperscript :: Char -> Maybe Char
1130+toSuperscript '1' = Just '\x00B9'
1131+toSuperscript '2' = Just '\x00B2'
1132+toSuperscript '3' = Just '\x00B3'
1133+toSuperscript '+' = Just '\x207A'
1134+toSuperscript '-' = Just '\x207B'
1135+toSuperscript '=' = Just '\x207C'
1136+toSuperscript '(' = Just '\x207D'
1137+toSuperscript ')' = Just '\x207E'
11321138toSuperscript c
11331139 | c >= '0' && c <= '9' =
1134− chr (0x2070 + (ord c - 48))
1135− | otherwise = c
1140+ Just $ chr (0x2070 + (ord c - 48))
1141+ | isSpace c = Just c
1142+ | otherwise = Nothing
11361143
1137−toSubscript :: Char -> Char
1138−toSubscript '+' = '\x208A'
1139−toSubscript '-' = '\x208B'
1140−toSubscript '=' = '\x208C'
1141−toSubscript '(' = '\x208D'
1142−toSubscript ')' = '\x208E'
1144+toSubscript :: Char -> Maybe Char
1145+toSubscript '+' = Just '\x208A'
1146+toSubscript '-' = Just '\x208B'
1147+toSubscript '=' = Just '\x208C'
1148+toSubscript '(' = Just '\x208D'
1149+toSubscript ')' = Just '\x208E'
11431150toSubscript c
11441151 | c >= '0' && c <= '9' =
1145− chr (0x2080 + (ord c - 48))
1146− | otherwise = c
1147−
1152+ Just $ chr (0x2080 + (ord c - 48))
1153+ | isSpace c = Just c
1154+ | otherwise = Nothing