Skip to content

Commit

Permalink
CommonMark: add support for wiki links. [API change]
Browse files Browse the repository at this point in the history
Adds commonmark extensions `wikilinks_title_after_pipe` and
`wikilinks_title_before_pipe`. The former enables links of style `[[Name
of page|Title]]` and the latter `[[Title|Name of page]]`. Titles are
optional in both variants, so this works for both:
`[[https://example.org]]`, `[[Name of page]]`.

The writer is modified to render links with title `wikilink` as a
wikilink if a respective extension is enabled.

Pandoc will use `wikilinks_title_after_pipe` if both extensions are
enabled.

Closes: #2923
  • Loading branch information
tarleb committed Nov 21, 2021
1 parent a9e5145 commit a1e8937
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Text/Pandoc/Extensions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ data Extension =
| Ext_tex_math_dollars -- ^ TeX math between $..$ or $$..$$
| Ext_tex_math_double_backslash -- ^ TeX math btw \\(..\\) \\[..\\]
| Ext_tex_math_single_backslash -- ^ TeX math btw \(..\) \[..\]
| Ext_wikilinks_title_after_pipe -- ^ Support wikilinks of style
-- [[target|title]]
| Ext_wikilinks_title_before_pipe -- ^ Support wikilinks of style
-- [[title|target]]
| Ext_xrefs_name -- ^ Use xrefs with names
| Ext_xrefs_number -- ^ Use xrefs with numbers
| Ext_yaml_metadata_block -- ^ YAML metadata block
Expand Down Expand Up @@ -526,6 +530,8 @@ getAllExtensions f = universalExtensions <> getAll f
, Ext_implicit_header_references
, Ext_attributes
, Ext_sourcepos
, Ext_wikilinks_title_after_pipe
, Ext_wikilinks_title_before_pipe
, Ext_yaml_metadata_block
, Ext_rebase_relative_paths
]
Expand Down Expand Up @@ -628,4 +634,3 @@ parseFormatSpec = parse formatSpec ""
case polarity of
'+' -> (ext : extsToEnable, extsToDisable)
_ -> (extsToEnable, ext : extsToDisable)

5 changes: 4 additions & 1 deletion src/Text/Pandoc/Readers/CommonMark.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ specFor opts = foldr ($) defaultSyntaxSpec exts
[ (footnoteSpec <>) | isEnabled Ext_footnotes opts ] ++
[ (definitionListSpec <>) | isEnabled Ext_definition_lists opts ] ++
[ (taskListSpec <>) | isEnabled Ext_task_lists opts ] ++
[ (wikilinksSpec TitleAfterPipe <>)
| isEnabled Ext_wikilinks_title_after_pipe opts ] ++
[ (wikilinksSpec TitleBeforePipe <>)
| isEnabled Ext_wikilinks_title_before_pipe opts ] ++
[ (rebaseRelativePathsSpec <>)
| isEnabled Ext_rebase_relative_paths opts ]

6 changes: 6 additions & 0 deletions src/Text/Pandoc/Writers/Markdown/Inline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ inlineToMarkdown opts lnk@(Link attr@(ident,classes,kvs) txt (src, tit)) = do
case txt of
[Str s] | escapeURI s == srcSuffix -> True
_ -> False
let useWikilink = tit == "wikilink"
let useRefLinks = writerReferenceLinks opts && not useAuto
shortcutable <- asks envRefShortcutable
let useShortcutRefLinks = shortcutable &&
Expand All @@ -557,6 +558,11 @@ inlineToMarkdown opts lnk@(Link attr@(ident,classes,kvs) txt (src, tit)) = do
| useAuto -> return $ literal srcSuffix
| otherwise -> return linktext
_ | useAuto -> return $ "<" <> literal srcSuffix <> ">"
| useAuto && useWikilink -> return $ "[[" <> literal srcSuffix <> "]]"
| useWikilink && isEnabled Ext_wikilinks_title_after_pipe opts -> return $
"[[" <> literal src <> "|" <> linktext <> "]]"
| useWikilink && isEnabled Ext_wikilinks_title_before_pipe opts -> return $
"[[" <> linktext <> "|" <> literal src <> "]]"
| useRefLinks ->
let first = "[" <> linktext <> "]"
second = if getKey linktext == getKey reftext
Expand Down
16 changes: 16 additions & 0 deletions test/command/wikilinks_title_after_pipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```
% pandoc --from commonmark_x+wikilinks_title_after_pipe -t html
[[https://example.org]]
[[https://example.org|title]]
[[name of page]]
[[name of page|title]]
^D
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
<p><a href="https://example.org" title="wikilink">title</a></p>
<p><a href="name of page" title="wikilink">name of page</a></p>
<p><a href="name of page" title="wikilink">title</a></p>
```

26 changes: 26 additions & 0 deletions test/command/wikilinks_title_before_pipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
```
% pandoc -f commonmark+wikilinks_title_before_pipe -t html
[[https://example.org]]
[[title|https://example.org]]
[[Name of page]]
[[Title|Name of page]]
^D
<p><a href="https://example.org" title="wikilink">https://example.org</a></p>
<p><a href="https://example.org" title="wikilink">title</a></p>
<p><a href="Name of page" title="wikilink">Name of page</a></p>
<p><a href="Name of page" title="wikilink">Title</a></p>
```

# Regular links should still work

```
% pandoc -f commonmark+wikilinks_title_before_pipe -t html
[Title](Name%20of%20page)
^D
<p><a href="Name%20of%20page">Title</a></p>
```


0 comments on commit a1e8937

Please sign in to comment.