Skip to content

Commit

Permalink
Support images with wikilink syntax.
Browse files Browse the repository at this point in the history
E.g. `![[foo|bar]]`.  (This requires enabling one of the `wikilinks`
extensions.)

Closes #8853.
  • Loading branch information
jgm committed Aug 30, 2023
1 parent 7dca09d commit 09de84f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ inline = do
'_' -> strongOrEmph
'*' -> strongOrEmph
'^' -> superscript <|> inlineNote -- in this order bc ^[link](/foo)^
'[' -> note <|> cite <|> bracketedSpan <|> wikilink <|> link
'[' -> note <|> cite <|> bracketedSpan <|> wikilink B.linkWith <|> link
'!' -> image
'$' -> math
'~' -> strikeout <|> subscript
Expand Down Expand Up @@ -1844,8 +1844,10 @@ source = do
linkTitle :: PandocMonad m => MarkdownParser m Text
linkTitle = quotedTitle '"' <|> quotedTitle '\''

wikilink :: PandocMonad m => MarkdownParser m (F Inlines)
wikilink =
wikilink :: PandocMonad m
=> (Attr -> Text -> Text -> Inlines -> Inlines)
-> MarkdownParser m (F Inlines)
wikilink constructor =
(guardEnabled Ext_wikilinks_title_after_pipe *> wikilink' swap) <|>
(guardEnabled Ext_wikilinks_title_before_pipe *> wikilink' id)
where
Expand All @@ -1857,7 +1859,7 @@ wikilink =
let (title, url) = case T.break (== '|') raw of
(before, "") -> (before, before)
(before, after) -> order (before, T.drop 1 after)
return . pure . B.link url "wikilink" $ B.str title
return . pure . constructor nullAttr url "wikilink" $ B.str title

link :: PandocMonad m => MarkdownParser m (F Inlines)
link = try $ do
Expand Down Expand Up @@ -2015,14 +2017,15 @@ rebasePath pos path = do
image :: PandocMonad m => MarkdownParser m (F Inlines)
image = try $ do
char '!'
(lab,raw) <- reference
defaultExt <- getOption readerDefaultImageExtension
let constructor attr' src =
case takeExtension (T.unpack src) of
"" -> B.imageWith attr' (T.pack $ addExtension (T.unpack src)
$ T.unpack defaultExt)
_ -> B.imageWith attr' src
regLink constructor lab <|> referenceLink constructor (lab, "!" <> raw)
wikilink B.imageWith <|>
do (lab,raw) <- reference
defaultExt <- getOption readerDefaultImageExtension
let constructor attr' src =
case takeExtension (T.unpack src) of
"" -> B.imageWith attr' (T.pack $ addExtension (T.unpack src)
$ T.unpack defaultExt)
_ -> B.imageWith attr' src
regLink constructor lab <|> referenceLink constructor (lab, "!" <> raw)

note :: PandocMonad m => MarkdownParser m (F Inlines)
note = try $ do
Expand Down
6 changes: 6 additions & 0 deletions test/command/8853.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```
% pandoc -f markdown+wikilinks_title_after_pipe --wrap=none
[[hi]] and ![[hi]]
^D
<p><a href="hi" title="wikilink">hi</a> and <img src="hi" title="wikilink" alt="hi" /></p>
```

0 comments on commit 09de84f

Please sign in to comment.