aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Shared.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2022-09-06 20:54:06 -0700
committerJohn MacFarlane <[email protected]>2022-09-06 21:23:59 -0700
commit1ff6afdec8e0def4445026262ec941c70d6f3d07 (patch)
treeec808bcd485548141e3e21d09cbaa24931497aa6 /src/Text/Pandoc/Shared.hs
parent47dcb5720e6f3bb334df5fb58b0fbe32c062a4a4 (diff)
Add prefixes to identifiers with `--file-scope`.multifile
This change only affects the case where `--file-scope` is used and more than one file is specified on the command line. In this case, identifiers will be prefixed with a string derived from the file path, to disambiguate them. For example, an identifier `foo` in `contents/file1.txt` will become `contents__file1.txt__foo`. Links will be adjusted accordingly: if `file2.txt` links to `file1.txt#foo`, then the link will be changed to point to `#file1.txt__foo`. Similarly, a link to `file1.txt` will point to `#file1.txt`. A Div with an identifier derived from the file path will be added around each file's content, so that links to files will still work. Closes #6384. [API change]: Text.Pandoc.Shared exports `textToIdentifier`.
Diffstat (limited to 'src/Text/Pandoc/Shared.hs')
-rw-r--r--src/Text/Pandoc/Shared.hs13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 35a854bf6..3afa1c0c9 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -57,6 +57,7 @@ module Text.Pandoc.Shared (
makeSections,
uniqueIdent,
inlineListToIdentifier,
+ textToIdentifier,
isHeaderBlock,
headerShift,
stripEmptyParagraphs,
@@ -497,12 +498,10 @@ isPara :: Block -> Bool
isPara (Para _) = True
isPara _ = False
--- | Convert Pandoc inline list to plain text identifier. HTML
--- identifiers must start with a letter, and may contain only
--- letters, digits, and the characters _-.
+-- | Convert Pandoc inline list to plain text identifier.
inlineListToIdentifier :: Extensions -> [Inline] -> T.Text
inlineListToIdentifier exts =
- dropNonLetter . filterAscii . toIdent . stringify . walk unEmojify
+ textToIdentifier exts . stringify . walk unEmojify
where
unEmojify :: [Inline] -> [Inline]
unEmojify
@@ -511,6 +510,12 @@ inlineListToIdentifier exts =
| otherwise = id
unEmoji (Span ("",["emoji"],[("data-emoji",ename)]) _) = Str ename
unEmoji x = x
+
+-- | Convert string to plain text identifier.
+textToIdentifier :: Extensions -> T.Text -> T.Text
+textToIdentifier exts =
+ dropNonLetter . filterAscii . toIdent
+ where
dropNonLetter
| extensionEnabled Ext_gfm_auto_identifiers exts = id
| otherwise = T.dropWhile (not . isAlpha)