git.iko.soy/typeable/pandoc
on master help sign in
Markdown reader: support GitHub wiki's internal links (#2923) (#6458)
6efd346Gautier DI FOLCO
src/Text/Pandoc/Extensions.hs+3
@@ -118,6 +118,7 @@ data Extension =
118 | Ext_literate_haskell -- ^ Enable literate Haskell conventions
118 | Ext_literate_haskell -- ^ Enable literate Haskell conventions
119 | Ext_markdown_attribute -- ^ Interpret text inside HTML as markdown iff
119 | Ext_markdown_attribute -- ^ Interpret text inside HTML as markdown iff
120 -- container has attribute 'markdown'
120 -- container has attribute 'markdown'
121+ | Ext_wikilinks -- ^ Interpret a markdown wiki link
121 | Ext_markdown_in_html_blocks -- ^ Interpret as markdown inside HTML blocks
122 | Ext_markdown_in_html_blocks -- ^ Interpret as markdown inside HTML blocks
122 | Ext_mmd_header_identifiers -- ^ Multimarkdown style header identifiers [myid]
123 | Ext_mmd_header_identifiers -- ^ Multimarkdown style header identifiers [myid]
123 | Ext_mmd_link_attributes -- ^ MMD style reference link attributes
124 | Ext_mmd_link_attributes -- ^ MMD style reference link attributes
@@ -258,6 +259,7 @@ githubMarkdownExtensions = extensionsFromList
258 , Ext_emoji
259 , Ext_emoji
259 , Ext_fenced_code_blocks
260 , Ext_fenced_code_blocks
260 , Ext_backtick_code_blocks
261 , Ext_backtick_code_blocks
262+ , Ext_wikilinks
261 ]
263 ]
262
264
263-- | Extensions to be used with multimarkdown.
265-- | Extensions to be used with multimarkdown.
@@ -444,6 +446,7 @@ getAllExtensions f = universalExtensions <> getAll f
444 , Ext_tex_math_single_backslash
446 , Ext_tex_math_single_backslash
445 , Ext_tex_math_double_backslash
447 , Ext_tex_math_double_backslash
446 , Ext_markdown_attribute
448 , Ext_markdown_attribute
449+ , Ext_wikilinks
447 , Ext_mmd_title_block
450 , Ext_mmd_title_block
448 , Ext_abbreviations
451 , Ext_abbreviations
449 , Ext_autolink_bare_uris
452 , Ext_autolink_bare_uris
src/Text/Pandoc/Readers/Markdown.hs+25
@@ -21,6 +21,7 @@ module Text.Pandoc.Readers.Markdown (
21import Control.Monad
21import Control.Monad
22import Control.Monad.Except (throwError)
22import Control.Monad.Except (throwError)
23import Data.Char (isAlphaNum, isPunctuation, isSpace)
23import Data.Char (isAlphaNum, isPunctuation, isSpace)
24+import Data.Functor (($>))
24import Data.List (transpose, elemIndex, sortOn)
25import Data.List (transpose, elemIndex, sortOn)
25import qualified Data.Map as M
26import qualified Data.Map as M
26import Data.Maybe
27import Data.Maybe
@@ -1485,6 +1486,7 @@ inline = choice [ whitespace
1485 , note
1486 , note
1486 , cite
1487 , cite
1487 , bracketedSpan
1488 , bracketedSpan
1489+ , githubWikiLink
1488 , link
1490 , link
1489 , image
1491 , image
1490 , math
1492 , math
@@ -1780,6 +1782,29 @@ source = do
1780linkTitle :: PandocMonad m => MarkdownParser m Text
1782linkTitle :: PandocMonad m => MarkdownParser m Text
1781linkTitle = quotedTitle '"' <|> quotedTitle '\''
1783linkTitle = quotedTitle '"' <|> quotedTitle '\''
1782
1784
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+
1783link :: PandocMonad m => MarkdownParser m (F Inlines)
1808link :: PandocMonad m => MarkdownParser m (F Inlines)
1784link = try $ do
1809link = try $ do
1785 st <- getState
1810 st <- getState
test/Tests/Readers/Markdown.hs+30
@@ -307,6 +307,36 @@ tests = [ testGroup "inline code"
307 "[https://example.org(](url)" =?>
307 "[https://example.org(](url)" =?>
308 para (link "url" "" (text "https://example.org("))
308 para (link "url" "" (text "https://example.org("))
309 ]
309 ]
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+ ]
310 , testGroup "Headers"
340 , testGroup "Headers"
311 [ "blank line before header" =:
341 [ "blank line before header" =:
312 "\n# Header\n"
342 "\n# Header\n"