Fix backticks escaped inside code blocks - #359
Open
Pranjal-SB wants to merge 1 commit into
Open
Conversation
The preformattedCode rule escaped every backtick in a code block. Backslash escapes are literal inside a fence, so `\`` reached the output verbatim and any code containing a backtick came out invalid — JS template literals and fetch(`...`) calls being the common case. Turndown's own fencedCodeBlock rule already handles this by growing the fence past the longest backtick run in the code. The custom rule exists to read data-lang/data-language, and reimplemented the body handling with an escape instead. This restores the fence-growing behaviour. Only a line-leading run of three or more backticks can close a fence, so backticks appearing mid-line need no treatment at all. tests/expected/general--obsidian.md-blog-verify-obsidian-sync-encryption.md had the bug baked into it — its console.log template literal was recorded with escaped delimiters. Updated to the corrected output. Fixes kepano#353
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #353
What
preformattedCodeescaped every backtick in a code block. Backslash escapes are literal inside a fence, so the\reached the output and the code came out invalid.Why
Any code containing a backtick was corrupted. JS template literals are the common case —
res.write(`...`),fetch(`...`)— but it applies to shell command substitution and any language that uses the character.src/markdown.ts:646:Turndown's own
fencedCodeBlockrule already handles this correctly, by growing the fence past the longest backtick run in the code. This custom rule exists to readdata-lang/data-language, which turndown's does not, and reimplemented the body handling with an escape instead. This restores the fence-growing behaviour and drops the escape.Only a line-leading run of three or more backticks can close a fence, so backticks appearing mid-line need no treatment at all — which is why the escape was never necessary.
How
String.matchAllis ES2020 andtsconfig.jsontargetses2019, so this usesmatch(/…/gm).One expected fixture changed
tests/expected/general--obsidian.md-blog-verify-obsidian-sync-encryption.mdhad the bug recorded in it — line 41 was:That is the same defect from obsidian.md's own blog. Corrected to bare backticks. It is the only expected file affected; the other 200+ are byte-identical.
Verification
Four new tests in
tests/markdown.test.tsunderfenced code blocks. Confirmed failing before the fix:Full suite after the fix:
The perf failure is the 5s default timeout on my machine, not this change — it fails identically on a clean checkout of
main.npm run buildandnpm run sizeboth pass:And against the URL from the issue, via
npx tsx src/cli.ts parse … --markdown:before
after
Not addressed here: the same article's code blocks without a language identifier, which #353 calls out as a separate request.