diff options
| author | Albert Krewinkel <[email protected]> | 2025-09-09 08:42:19 +0200 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2025-09-09 10:26:05 +0200 |
| commit | 36bbdc667a72311257a4a8aedaa26b330cf01206 (patch) | |
| tree | 85b7586b3f9292f63f9de6cf6b1f9dc3bf7c9982 | |
| parent | 6ba552232a3637f2251c7fc97dac013467a21187 (diff) | |
T.P.Writers.Shared: add new function `removeLinks`.
The function converts links to spans. It is used, for example, to avoid
nested links.
The HTML writer used to put the description of nested links into small
caps, but uses a simple *span* now.
| -rw-r--r-- | src/Text/Pandoc/Writers/HTML.hs | 12 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/MediaWiki.hs | 11 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Shared.hs | 9 | ||||
| -rw-r--r-- | test/command/11124.md | 10 | ||||
| -rw-r--r-- | test/command/8738.md | 2 |
5 files changed, 25 insertions, 19 deletions
diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index bf01f663b..12444f75f 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -1548,6 +1548,9 @@ inlineToHtml opts inline = do _ -> do report $ InlineNotRendered inline return mempty (Link attr txt (s,_)) | "mailto:" `T.isPrefixOf` s -> do + -- We need to remove links from link text, because an + -- <a> element is not allowed inside another <a> + -- element. linkText <- inlineListToHtml opts (removeLinks txt) obfuscateLink opts attr linkText s (Link (ident,classes,kvs) txt (s,tit)) -> do @@ -1768,15 +1771,6 @@ isSlideVariant :: Format -> Bool isSlideVariant f = f `elem` [Format "s5", Format "slidy", Format "slideous", Format "dzslides", Format "revealjs"] - --- We need to remove links from link text, because an <a> element is --- not allowed inside another <a> element. -removeLinks :: [Inline] -> [Inline] -removeLinks = walk go - where - go (Link attr ils _) = Span attr ils - go x = x - toURI :: Bool -> Text -> Text toURI isHtml5 t = if isHtml5 then t else escapeURI t where diff --git a/src/Text/Pandoc/Writers/MediaWiki.hs b/src/Text/Pandoc/Writers/MediaWiki.hs index 2991e4ebc..d85354756 100644 --- a/src/Text/Pandoc/Writers/MediaWiki.hs +++ b/src/Text/Pandoc/Writers/MediaWiki.hs @@ -26,7 +26,6 @@ import Text.Pandoc.Definition import Text.Pandoc.ImageSize import Text.Pandoc.Logging import Text.Pandoc.Options -import Text.Pandoc.Walk import Text.DocLayout (render, literal) import Text.Pandoc.Shared import Text.Pandoc.URI @@ -484,6 +483,8 @@ inlineToMediaWiki SoftBreak = do inlineToMediaWiki Space = return " " inlineToMediaWiki (Link _ txt (src, _)) = do + -- We need to remove links from link text, because an <a> element is + -- not allowed inside another <a> element. label <- inlineListToMediaWiki (removeLinks txt) case txt of [Str s] | isURI src && escapeURI s == src -> return src @@ -518,14 +519,6 @@ inlineToMediaWiki (Note contents) = do return $ "<ref>" <> stripTrailingNewlines contents' <> "</ref>" -- note - does not work for notes with multiple blocks --- We need to remove links from link text, because an <a> element is --- not allowed inside another <a> element. -removeLinks :: [Inline] -> [Inline] -removeLinks = walk go - where - go (Link _ ils _) = SmallCaps ils - go x = x - highlightingLangs :: Set.Set Text highlightingLangs = Set.fromList [ "abap", diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs index aff1520f1..9d71f2fb3 100644 --- a/src/Text/Pandoc/Writers/Shared.hs +++ b/src/Text/Pandoc/Writers/Shared.hs @@ -45,6 +45,7 @@ module Text.Pandoc.Writers.Shared ( , toLegacyTable , splitSentences , ensureValidXmlIdentifiers + , removeLinks , setupTranslations , isOrderedListMarker , toTaskListItem @@ -805,6 +806,14 @@ walkAttr f = walk goInline . walk goBlock goBlock (Div attr bs) = Div (f attr) bs goBlock x = x +-- | Convert links to spans; most useful when writing elements that must not +-- contain links, e.g. to avoid nested links. +removeLinks :: [Inline] -> [Inline] +removeLinks = walk go + where + go (Link attr ils _) = Span attr ils + go x = x + -- | Set translations based on the `lang` in metadata. setupTranslations :: PandocMonad m => Meta -> m () setupTranslations meta = do diff --git a/test/command/11124.md b/test/command/11124.md new file mode 100644 index 000000000..0343d2650 --- /dev/null +++ b/test/command/11124.md @@ -0,0 +1,10 @@ +The heading is "unlinked" before adding it to the TOC. + +``` +% pandoc --to=latex +<http://example.com/> {-} +========================= +^D +\section*{\texorpdfstring{\url{http://example.com/}}{http://example.com/}}\label{httpexample.com} +\addcontentsline{toc}{section}{{http://example.com/}} +``` diff --git a/test/command/8738.md b/test/command/8738.md index 4b8f251e7..ae128b8bd 100644 --- a/test/command/8738.md +++ b/test/command/8738.md @@ -2,5 +2,5 @@ % pandoc -f native -t mediawiki [Link ("",[],[]) [Link ("",[],[]) [Str "test"] ("https://bar.example.com",""),Str "Test"] ("https://foo.example.com","")] ^D -[https://foo.example.com testTest] +[https://foo.example.com <span>test</span>Test] ``` |
