git.iko.soy/typeable/pandoc
on master help sign in
Org reader: preserve targets of spurious links

Links with (internal) targets that the reader doesn't know about are converted into emphasized text. Information on the link target is now preserved by wrapping the text in a Span of class `spurious-link`, with an attribute `target` set to the link's original target. This allows to recover and fix broken or unknown links with filters. See: #6916

commitacf932825bfe40d9a18046c9d304f4f14363a88a authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 5 Dec 2020, 22:05 (6 years ago) parentc161893
2 files changed+8−7
src/Text/Pandoc/Readers/Org/Inlines.hs+4−5 test/Tests/Readers/Org/Meta.hs+4−2
src/Text/Pandoc/Readers/Org/Inlines.hs+4−5
@@ -477,17 +477,17 @@ linkToInlinesF linkStr =
477477
478478internalLink :: Text -> Inlines -> F Inlines
479479internalLink link title = do
480− anchorB <- (link `elem`) <$> asksF orgStateAnchorIds
481− if anchorB
480+ ids <- asksF orgStateAnchorIds
481+ if link `elem` ids
482482 then return $ B.link ("#" <> link) "" title
483− else return $ B.emph title
483+ else let attr' = ("", ["spurious-link"] , [("target", link)])
484+ in return $ B.spanWith attr' (B.emph title)
484485
485486-- | Parse an anchor like @<<anchor-id>>@ and return an empty span with
486487-- @anchor-id@ set as id. Legal anchors in org-mode are defined through
487488-- @org-target-regexp@, which is fairly liberal. Since no link is created if
488489-- @anchor-id@ contains spaces, we are more restrictive in what is accepted as
489490-- an anchor.
490−
491491anchor :: PandocMonad m => OrgParser m (F Inlines)
492492anchor = try $ do
493493 anchorId <- parseAnchor
@@ -501,7 +501,6 @@ anchor = try $ do
501501
502502-- | Replace every char but [a-zA-Z0-9_.-:] with a hyphen '-'. This mirrors
503503-- the org function @org-export-solidify-link-text@.
504−
505504solidify :: Text -> Text
506505solidify = T.map replaceSpecialChar
507506 where replaceSpecialChar c
test/Tests/Readers/Org/Meta.hs+4−2
@@ -270,7 +270,8 @@ tests =
270270
271271 , "Search links are read as emph" =:
272272 "[[Wally][Where's Wally?]]" =?>
273− para (emph $ "Where's" <> space <> "Wally?")
273+ para (spanWith ("", ["spurious-link"], [("target", "Wally")])
274+ (emph $ "Where's" <> space <> "Wally?"))
274275
275276 , "Link to nonexistent anchor" =:
276277 T.unlines [ "<<link-here>> Target."
@@ -278,5 +279,6 @@ tests =
278279 , "[[link$here][See here!]]"
279280 ] =?>
280281 (para (spanWith ("link-here", [], []) mempty <> "Target.") <>
281− para (emph ("See" <> space <> "here!")))
282+ para (spanWith ("", ["spurious-link"], [("target", "link$here")])
283+ (emph ("See" <> space <> "here!"))))
282284 ]