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
This commit is contained in:
Albert Krewinkel 2020-12-05 22:05:37 +01:00
parent c161893f44
commit acf932825b
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
2 changed files with 8 additions and 7 deletions

View file

@ -477,17 +477,17 @@ linkToInlinesF linkStr =
internalLink :: Text -> Inlines -> F Inlines
internalLink link title = do
anchorB <- (link `elem`) <$> asksF orgStateAnchorIds
if anchorB
ids <- asksF orgStateAnchorIds
if link `elem` ids
then return $ B.link ("#" <> link) "" title
else return $ B.emph title
else let attr' = ("", ["spurious-link"] , [("target", link)])
in return $ B.spanWith attr' (B.emph title)
-- | Parse an anchor like @<<anchor-id>>@ and return an empty span with
-- @anchor-id@ set as id. Legal anchors in org-mode are defined through
-- @org-target-regexp@, which is fairly liberal. Since no link is created if
-- @anchor-id@ contains spaces, we are more restrictive in what is accepted as
-- an anchor.
anchor :: PandocMonad m => OrgParser m (F Inlines)
anchor = try $ do
anchorId <- parseAnchor
@ -501,7 +501,6 @@ anchor = try $ do
-- | Replace every char but [a-zA-Z0-9_.-:] with a hyphen '-'. This mirrors
-- the org function @org-export-solidify-link-text@.
solidify :: Text -> Text
solidify = T.map replaceSpecialChar
where replaceSpecialChar c

View file

@ -270,7 +270,8 @@ tests =
, "Search links are read as emph" =:
"[[Wally][Where's Wally?]]" =?>
para (emph $ "Where's" <> space <> "Wally?")
para (spanWith ("", ["spurious-link"], [("target", "Wally")])
(emph $ "Where's" <> space <> "Wally?"))
, "Link to nonexistent anchor" =:
T.unlines [ "<<link-here>> Target."
@ -278,5 +279,6 @@ tests =
, "[[link$here][See here!]]"
] =?>
(para (spanWith ("link-here", [], []) mempty <> "Target.") <>
para (emph ("See" <> space <> "here!")))
para (spanWith ("", ["spurious-link"], [("target", "link$here")])
(emph ("See" <> space <> "here!"))))
]