git.iko.soy/typeable/pandoc
on master help sign in
Markdown reader: support GitHub wiki's internal links (#2923) (#6458)

Canges overview: * Add a `Ext_markdown_github_wikilink` constructor to `Extension` [API change]. * Add the parser `githubWikiLink` in `Text.Pandoc.Readers.Markdown` * Add tests.

commit6efd3460a776620fdb93812daa4f6831e6c332ce authorGautier DI FOLCO <gautier.difolco@gmail.com> dateSun 17 Jan 2021, 01:15 (6 years ago) parent83336a4
3 files changed+58
src/Text/Pandoc/Extensions.hs+3 src/Text/Pandoc/Readers/Markdown.hs+25 test/Tests/Readers/Markdown.hs+30
src/Text/Pandoc/Extensions.hs+3
@@ -118,6 +118,7 @@ data Extension =
118118 | Ext_literate_haskell -- ^ Enable literate Haskell conventions
119119 | Ext_markdown_attribute -- ^ Interpret text inside HTML as markdown iff
120120 -- container has attribute 'markdown'
121+ | Ext_wikilinks -- ^ Interpret a markdown wiki link
121122 | Ext_markdown_in_html_blocks -- ^ Interpret as markdown inside HTML blocks
122123 | Ext_mmd_header_identifiers -- ^ Multimarkdown style header identifiers [myid]
123124 | Ext_mmd_link_attributes -- ^ MMD style reference link attributes
@@ -258,6 +259,7 @@ githubMarkdownExtensions = extensionsFromList
258259 , Ext_emoji
259260 , Ext_fenced_code_blocks
260261 , Ext_backtick_code_blocks
262+ , Ext_wikilinks
261263 ]
262264
263265-- | Extensions to be used with multimarkdown.
@@ -444,6 +446,7 @@ getAllExtensions f = universalExtensions <> getAll f
444446 , Ext_tex_math_single_backslash
445447 , Ext_tex_math_double_backslash
446448 , Ext_markdown_attribute
449+ , Ext_wikilinks
447450 , Ext_mmd_title_block
448451 , Ext_abbreviations
449452 , Ext_autolink_bare_uris
src/Text/Pandoc/Readers/Markdown.hs+25
@@ -21,6 +21,7 @@ module Text.Pandoc.Readers.Markdown (
2121import Control.Monad
2222import Control.Monad.Except (throwError)
2323import Data.Char (isAlphaNum, isPunctuation, isSpace)
24+import Data.Functor (($>))
2425import Data.List (transpose, elemIndex, sortOn)
2526import qualified Data.Map as M
2627import Data.Maybe
@@ -1485,6 +1486,7 @@ inline = choice [ whitespace
14851486 , note
14861487 , cite
14871488 , bracketedSpan
1489+ , githubWikiLink
14881490 , link
14891491 , image
14901492 , math
@@ -1780,6 +1782,29 @@ source = do
17801782linkTitle :: PandocMonad m => MarkdownParser m Text
17811783linkTitle = quotedTitle '"' <|> quotedTitle '\''
17821784
1785+-- Github wiki style link, with optional title
1786+-- syntax documented under https://help.github.com/en/github/building-a-strong-community/editing-wiki-content
1787+githubWikiLink :: PandocMonad m => MarkdownParser m (F Inlines)
1788+githubWikiLink = try $ guardEnabled Ext_wikilinks >> wikilink
1789+ where
1790+ wikilink = try $ do
1791+ string "[["
1792+ firstPart <- fmap mconcat . sequence <$> wikiText
1793+ (char '|' *> complexWikilink firstPart)
1794+ <|> (string "]]" $> (B.link
1795+ <$> (stringify <$> firstPart)
1796+ <*> return "wikilink"
1797+ <*> firstPart))
1798+
1799+ complexWikilink firstPart = do
1800+ url <- fmap stringify . sequence <$> wikiText
1801+ string "]]"
1802+ return $ B.link <$> url
1803+ <*> return "wikilink"
1804+ <*> firstPart
1805+
1806+ wikiText = many (whitespace <|> bareURL <|> str <|> endline <|> escapedChar)
1807+
17831808link :: PandocMonad m => MarkdownParser m (F Inlines)
17841809link = try $ do
17851810 st <- getState
test/Tests/Readers/Markdown.hs+30
@@ -307,6 +307,36 @@ tests = [ testGroup "inline code"
307307 "[https://example.org(](url)" =?>
308308 para (link "url" "" (text "https://example.org("))
309309 ]
310+ , testGroup "Github wiki links"
311+ [ test markdownGH "autolink" $
312+ "[[https://example.org]]" =?>
313+ para (link "https://example.org" "wikilink" (text "https://example.org"))
314+ , test markdownGH "link with title" $
315+ "[[title|https://example.org]]" =?>
316+ para (link "https://example.org" "wikilink" (text "title"))
317+ , test markdownGH "bad link with title" $
318+ "[[title|random string]]" =?>
319+ para (link "random-string" "wikilink" (text "title"))
320+ , test markdownGH "autolink not being a link" $
321+ "[[Name of page]]" =?>
322+ para (link "Name-of-page" "wikilink" (text "Name of page"))
323+ , test markdownGH "autolink not being a link with a square bracket" $
324+ "[[Name of ]page]]" =?>
325+ para (link "Name-of-]page" "wikilink" (text "Name of ]page"))
326+ , test markdownGH "formatting (strong and emphasis) should not be interpreted" $
327+ "[[***a**b **c**d*|https://example.org]]" =?>
328+ para (text "[[" <> emph (strong (str "a") <> str "b" <> space
329+ <> strong (str "c") <> str "d") <> text "|https://example.org]]")
330+ , test markdownGH "inlined code should not make a link" $
331+ "[[ti`|`le|https://example.org]]" =?>
332+ para (text "[[ti" <> code "|" <> text "le|https://example.org]]")
333+ , test markdownGH "link with title and a cut should take the middle part as link" $
334+ "[[tit|le|https://example.org]]" =?>
335+ para (link "le" "wikilink" (text "tit"))
336+ , test markdownGH "link with inline start should be a link" $
337+ "[[t`i*t_le|https://example.org]]" =?>
338+ para (link "https://example.org" "wikilink" (text "t`i*t_le"))
339+ ]
310340 , testGroup "Headers"
311341 [ "blank line before header" =:
312342 "\n# Header\n"