Skip to content

Commit 5676a4d

Browse files
committed
Track wikilinks with a class instead of a title
Once upon a time the only metadata element for links in Pandoc's AST was a title, and it was hijacked to track certain links as having originated in the wikilink syntax. Now we have Attrs and we can use a class to handle wikilinks instead. Requires coordinated changes to commonmark-hs.
1 parent cb8f764 commit 5676a4d

11 files changed

+93
-93
lines changed

src/Text/Pandoc/Readers/Markdown.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,7 @@ wikilink :: PandocMonad m
18591859
=> (Attr -> Text -> Text -> Inlines -> Inlines)
18601860
-> MarkdownParser m (F Inlines)
18611861
wikilink constructor = do
1862+
let attr = (mempty, ["wikilink"], mempty)
18621863
titleAfter <-
18631864
(True <$ guardEnabled Ext_wikilinks_title_after_pipe) <|>
18641865
(False <$ guardEnabled Ext_wikilinks_title_before_pipe)
@@ -1871,7 +1872,7 @@ wikilink constructor = do
18711872
| titleAfter -> (T.drop 1 after, before)
18721873
| otherwise -> (before, T.drop 1 after)
18731874
guard $ T.all (`notElem` ['\n','\r','\f','\t']) url
1874-
return . pure . constructor nullAttr url "wikilink" $
1875+
return . pure . constructor attr url "" $
18751876
B.text $ fromEntities title
18761877

18771878
link :: PandocMonad m => MarkdownParser m (F Inlines)

src/Text/Pandoc/Readers/MediaWiki.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ internalLink = try $ do
667667
sym "]]"
668668
-- see #8525:
669669
linktrail <- B.text <$> manyChar (satisfy (\c -> isLetter c && not (isCJK c)))
670-
let link = B.link (addUnderscores pagename) "wikilink" (label <> linktrail)
670+
let link = B.linkWith (mempty, ["wikilink"], mempty) (addUnderscores pagename) (stringify label) (label <> linktrail)
671671
if "Category:" `T.isPrefixOf` pagename
672672
then do
673673
updateState $ \st -> st{ mwCategoryLinks = link : mwCategoryLinks st }

src/Text/Pandoc/Readers/Vimwiki.hs

+6-17
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,7 @@ import Data.Text (Text)
5656
import qualified Data.Text as T
5757
import Safe (lastMay)
5858
import Text.Pandoc.Builder (Blocks, Inlines, fromList, toList, trimInlines)
59-
import qualified Text.Pandoc.Builder as B (blockQuote, bulletList, code,
60-
codeBlockWith, definitionList,
61-
displayMath, divWith, emph,
62-
headerWith, horizontalRule, image,
63-
imageWith, link, math, orderedList,
64-
para, plain, setMeta, simpleTable,
65-
softbreak, space, spanWith, str,
66-
strikeout, strong, subscript,
67-
superscript)
59+
import qualified Text.Pandoc.Builder as B
6860
import Text.Pandoc.Class.PandocMonad (PandocMonad (..))
6961
import Text.Pandoc.Definition (Attr, Block (BulletList, OrderedList),
7062
Inline (Space), ListNumberDelim (..),
@@ -555,17 +547,14 @@ link = try $ do
555547
then do
556548
url <- manyTillChar anyChar $ char '|'
557549
lab <- mconcat <$> manyTill inline (string "]]")
558-
let tit = if isURI url
559-
then ""
560-
else "wikilink"
561-
return $ B.link (procLink url) tit lab
550+
return $ B.linkWith (attr url) (procLink url) "" lab
562551
else do
563552
manyTill anyChar (string "]]")
564553
-- not using try here because [[hell]o]] is not rendered as a link in vimwiki
565-
let tit = if isURI contents
566-
then ""
567-
else "wikilink"
568-
return $ B.link (procLink contents) tit (B.str contents)
554+
return $ B.linkWith (attr contents) (procLink contents) "" (B.str contents)
555+
where
556+
attr t | isURI t = B.nullAttr
557+
| otherwise = (mempty, ["wikilink"], mempty)
569558

570559
image :: PandocMonad m => VwParser m Inlines
571560
image = try $ do

src/Text/Pandoc/Writers/Markdown/Inline.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ inlineToMarkdown opts lnk@(Link attr@(ident,classes,kvs) txt (src, tit)) = do
649649
case txt of
650650
[Str s] | escapeURI s == srcSuffix -> True
651651
_ -> False
652-
let useWikilink = tit == "wikilink" &&
652+
let useWikilink = "wikilink" `elem` classes &&
653653
(isEnabled Ext_wikilinks_title_after_pipe opts ||
654654
isEnabled Ext_wikilinks_title_before_pipe opts)
655655
let useRefLinks = writerReferenceLinks opts && not useAuto

test/Tests/Readers/Markdown.hs

+9-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ autolinkWith :: Attr -> String -> Inlines
5858
autolinkWith attr s = linkWith attr s' "" (str s')
5959
where s' = T.pack s
6060

61+
wikilink :: Attr
62+
wikilink = (mempty, ["wikilink"], mempty)
63+
6164
bareLinkTests :: [(Text, Inlines)]
6265
bareLinkTests =
6366
[ ("http://google.com is a search engine.",
@@ -312,22 +315,22 @@ tests = [ testGroup "inline code"
312315
, testGroup "Github wiki links"
313316
[ test markdownGH "autolink" $
314317
"[[https://example.org]]" =?>
315-
para (link "https://example.org" "wikilink" (str "https://example.org"))
318+
para (linkWith wikilink "https://example.org" "" (str "https://example.org"))
316319
, test markdownGH "link with title" $
317320
"[[title|https://example.org]]" =?>
318-
para (link "https://example.org" "wikilink" (str "title"))
321+
para (linkWith wikilink "https://example.org" "" (str "title"))
319322
, test markdownGH "bad link with title" $
320323
"[[title|random string]]" =?>
321-
para (link "random string" "wikilink" (str "title"))
324+
para (linkWith wikilink "random string" "" (str "title"))
322325
, test markdownGH "autolink not being a link" $
323326
"[[Name of page]]" =?>
324-
para (link "Name of page" "wikilink" (text "Name of page"))
327+
para (linkWith wikilink "Name of page" "" (text "Name of page"))
325328
, test markdownGH "autolink not being a link with a square bracket" $
326329
"[[Name of ]page]]" =?>
327-
para (link "Name of ]page" "wikilink" (text "Name of ]page"))
330+
para (linkWith wikilink "Name of ]page" "" (text "Name of ]page"))
328331
, test markdownGH "link with inline start should be a link" $
329332
"[[t`i*t_le|https://example.org]]" =?>
330-
para (link "https://example.org" "wikilink" (str "t`i*t_le"))
333+
para (linkWith wikilink "https://example.org" "" (str "t`i*t_le"))
331334
]
332335
, testGroup "Headers"
333336
[ "blank line before header" =:

test/command/2649.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,20 @@
9090
<tr>
9191
<td><p>1</p></td>
9292
<td style="text-align: left;"><p><a href="Sébastien_Loeb"
93-
title="wikilink">Sébastien Loeb</a></p></td>
93+
class="wikilink" title="Sébastien Loeb">Sébastien Loeb</a></p></td>
9494
<td><p>78</p></td>
9595
</tr>
9696
<tr>
9797
<td><p>2</p></td>
9898
<td style="text-align: left;"><p><strong><a href="Sébastien_Ogier"
99-
title="wikilink">Sébastien Ogier</a></strong></p></td>
99+
class="wikilink" title="Sébastien Ogier">Sébastien
100+
Ogier</a></strong></p></td>
100101
<td><p>38</p></td>
101102
</tr>
102103
<tr>
103104
<td><p>10</p></td>
104105
<td style="text-align: left;"><p><a href="Hannu_Mikkola"
105-
title="wikilink">Hannu Mikkola</a></p></td>
106+
class="wikilink" title="Hannu Mikkola">Hannu Mikkola</a></p></td>
106107
<td><p>18</p></td>
107108
</tr>
108109
</tbody>

test/command/8853.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
% pandoc -f markdown+wikilinks_title_after_pipe --wrap=none
33
[[hi]] and ![[hi]]
44
^D
5-
<p><a href="hi" title="wikilink">hi</a> and <img src="hi" title="wikilink" alt="hi" /></p>
5+
<p><a href="hi" class="wikilink">hi</a> and <img src="hi" class="wikilink" alt="hi" /></p>
66
```

test/command/wikilinks_title_after_pipe.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
1212
[[name of page|title]]
1313
^D
14-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
15-
<p><a href="https://example.org" title="wikilink">title</a></p>
16-
<p><a href="name of page" title="wikilink">name of page</a></p>
17-
<p><a href="name of page" title="wikilink">title</a></p>
14+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
15+
<p><a href="https://example.org" class="wikilink">title</a></p>
16+
<p><a href="name of page" class="wikilink">name of page</a></p>
17+
<p><a href="name of page" class="wikilink">title</a></p>
1818
```
1919

2020
## Writer
2121

2222
```
2323
% pandoc -t commonmark_x+wikilinks_title_after_pipe -f html
24-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
25-
<p><a href="https://example.org" title="wikilink">title</a></p>
26-
<p><a href="Home" title="wikilink">Home</a></p>
27-
<p><a href="Name of page" title="wikilink">Title</a></p>
24+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
25+
<p><a href="https://example.org" class="wikilink">title</a></p>
26+
<p><a href="Home" class="wikilink">Home</a></p>
27+
<p><a href="Name of page" class="wikilink">Title</a></p>
2828
^D
2929
[[https://example.org]]
3030
@@ -48,20 +48,20 @@
4848
4949
[[name of page|title]]
5050
^D
51-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
52-
<p><a href="https://example.org" title="wikilink">title</a></p>
53-
<p><a href="name of page" title="wikilink">name of page</a></p>
54-
<p><a href="name of page" title="wikilink">title</a></p>
51+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
52+
<p><a href="https://example.org" class="wikilink">title</a></p>
53+
<p><a href="name of page" class="wikilink">name of page</a></p>
54+
<p><a href="name of page" class="wikilink">title</a></p>
5555
```
5656

5757
## Writer
5858

5959
```
6060
% pandoc -t markdown+wikilinks_title_after_pipe -f html
61-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
62-
<p><a href="https://example.org" title="wikilink">title</a></p>
63-
<p><a href="Home" title="wikilink">Home</a></p>
64-
<p><a href="Name of page" title="wikilink">Title</a></p>
61+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
62+
<p><a href="https://example.org" class="wikilink">title</a></p>
63+
<p><a href="Home" class="wikilink">Home</a></p>
64+
<p><a href="Name of page" class="wikilink">Title</a></p>
6565
^D
6666
[[https://example.org]]
6767

test/command/wikilinks_title_before_pipe.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
1313
[[Title|Name of page]]
1414
^D
15-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
16-
<p><a href="https://example.org" title="wikilink">title</a></p>
17-
<p><a href="Name of page" title="wikilink">Name of page</a></p>
18-
<p><a href="Name of page" title="wikilink">Title</a></p>
15+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
16+
<p><a href="https://example.org" class="wikilink">title</a></p>
17+
<p><a href="Name of page" class="wikilink">Name of page</a></p>
18+
<p><a href="Name of page" class="wikilink">Title</a></p>
1919
```
2020

2121
## Writer
2222

2323
```
2424
% pandoc -t commonmark_x+wikilinks_title_before_pipe -f html
25-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
26-
<p><a href="https://example.org" title="wikilink">title</a></p>
27-
<p><a href="Home" title="wikilink">Home</a></p>
28-
<p><a href="Name of page" title="wikilink">Title</a></p>
25+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
26+
<p><a href="https://example.org" class="wikilink">title</a></p>
27+
<p><a href="Home" class="wikilink">Home</a></p>
28+
<p><a href="Name of page" class="wikilink">Title</a></p>
2929
^D
3030
[[https://example.org]]
3131
@@ -59,20 +59,20 @@
5959
6060
[[Title|Name of page]]
6161
^D
62-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
63-
<p><a href="https://example.org" title="wikilink">title</a></p>
64-
<p><a href="Name of page" title="wikilink">Name of page</a></p>
65-
<p><a href="Name of page" title="wikilink">Title</a></p>
62+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
63+
<p><a href="https://example.org" class="wikilink">title</a></p>
64+
<p><a href="Name of page" class="wikilink">Name of page</a></p>
65+
<p><a href="Name of page" class="wikilink">Title</a></p>
6666
```
6767

6868
## Writer
6969

7070
```
7171
% pandoc -t markdown+wikilinks_title_before_pipe -f html
72-
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
73-
<p><a href="https://example.org" title="wikilink">title</a></p>
74-
<p><a href="Home" title="wikilink">Home</a></p>
75-
<p><a href="Name of page" title="wikilink">Title</a></p>
72+
<p><a href="https://example.org" class="wikilink">https://example.org</a></p>
73+
<p><a href="https://example.org" class="wikilink">title</a></p>
74+
<p><a href="Home" class="wikilink">Home</a></p>
75+
<p><a href="Name of page" class="wikilink">Title</a></p>
7676
^D
7777
[[https://example.org]]
7878

test/mediawiki-reader.native

+17-11
Original file line numberDiff line numberDiff line change
@@ -254,40 +254,46 @@ Pandoc
254254
[ Str "internal" , Space , Str "links" ]
255255
, Para
256256
[ Link
257-
( "" , [] , [] ) [ Str "Help" ] ( "Help" , "wikilink" )
257+
( "" , [ "wikilink" ] , [] )
258+
[ Str "Help" ]
259+
( "Help" , "Help" )
258260
]
259261
, Para
260262
[ Link
261-
( "" , [] , [] )
263+
( "" , [ "wikilink" ] , [] )
262264
[ Str "the" , Space , Str "help" , Space , Str "page" ]
263-
( "Help" , "wikilink" )
265+
( "Help" , "the help page" )
264266
]
265267
, Para
266268
[ Link
267-
( "" , [] , [] ) [ Str "Helpers" ] ( "Help" , "wikilink" )
269+
( "" , [ "wikilink" ] , [] )
270+
[ Str "Helpers" ]
271+
( "Help" , "Help" )
268272
]
269273
, Para
270274
[ Link
271-
( "" , [] , [] ) [ Str "Help" ] ( "Help" , "wikilink" )
275+
( "" , [ "wikilink" ] , [] )
276+
[ Str "Help" ]
277+
( "Help" , "Help" )
272278
, Str "ers"
273279
]
274280
, Para
275281
[ Link
276-
( "" , [] , [] )
282+
( "" , [ "wikilink" ] , [] )
277283
[ Str "Contents" ]
278-
( "Help:Contents" , "wikilink" )
284+
( "Help:Contents" , "Contents" )
279285
]
280286
, Para
281287
[ Link
282-
( "" , [] , [] )
288+
( "" , [ "wikilink" ] , [] )
283289
[ Str "#My" , Space , Str "anchor" ]
284-
( "#My_anchor" , "wikilink" )
290+
( "#My_anchor" , "#My anchor" )
285291
]
286292
, Para
287293
[ Link
288-
( "" , [] , [] )
294+
( "" , [ "wikilink" ] , [] )
289295
[ Str "and" , Space , Str "text" ]
290-
( "Page#with_anchor" , "wikilink" )
296+
( "Page#with_anchor" , "and text" )
291297
]
292298
, Header 2 ( "images" , [] , [] ) [ Str "images" ]
293299
, Figure

0 commit comments

Comments
 (0)