aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Text/Pandoc/Writers/Typst.hs20
-rw-r--r--test/command/11210.md13
2 files changed, 32 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs
index 40e1f3ac8..e129fce69 100644
--- a/src/Text/Pandoc/Writers/Typst.hs
+++ b/src/Text/Pandoc/Writers/Typst.hs
@@ -409,7 +409,25 @@ listItemToTypst ind marker blocks = do
return $ hang ind (marker <> space) contents
inlinesToTypst :: PandocMonad m => [Inline] -> TW m (Doc Text)
-inlinesToTypst ils = hcat <$> mapM inlineToTypst ils
+inlinesToTypst ils = hcat <$> mapM inlineToTypst (escapeParens ils)
+
+-- Add an escape before a parenthesis right after a non-space element.
+-- Otherwise we risk `#emph[test](3)` which will error. See #11210.
+escapeParens :: [Inline] -> [Inline]
+escapeParens [] = []
+escapeParens (s : x : xs)
+ | isSpacey s
+ = s : x : escapeParens xs
+escapeParens (Str t : xs)
+ | Just ('(',_) <- T.uncons t
+ = RawInline (Format "typst") "\\" : Str t : escapeParens xs
+escapeParens (x : xs) = x : escapeParens xs
+
+isSpacey :: Inline -> Bool
+isSpacey Space = True
+isSpacey SoftBreak = True
+isSpacey LineBreak = True
+isSpacey _ = False
inlineToTypst :: PandocMonad m => Inline -> TW m (Doc Text)
inlineToTypst inline =
diff --git a/test/command/11210.md b/test/command/11210.md
new file mode 100644
index 000000000..cfd3af7e8
--- /dev/null
+++ b/test/command/11210.md
@@ -0,0 +1,13 @@
+```
+% pandoc -t typst -f man
+.PP
+.IR login (1)
+.PP
+and a regular (paren) that should not be escaped.
+^D
+#emph[login]\(1)
+
+and a regular (paren) that should not be escaped.
+
+```
+