fix: remove up to the fence indentation from each content line - #4074
Open
Kjubikstronk wants to merge 1 commit into
Open
fix: remove up to the fence indentation from each content line#4074Kjubikstronk wants to merge 1 commit into
Kjubikstronk wants to merge 1 commit into
Conversation
CommonMark removes up to N spaces from each line of a fenced code block when the opening fence is indented by N. indentCodeCompensation only stripped lines indented at least N, so a line indented less kept all of its indentation. Fixes CommonMark example 133. The spec suite did not catch it because htmlIsEqual leaves html-differ's ignoreWhitespaces at its default of true.
|
@Kjubikstronk is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
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.
Marked version: 18.0.11 (53cb13f)
Markdown flavor: CommonMark
Description
CommonMark removes up to N spaces from each line of a fenced code block when the opening fence is indented by N.
indentCodeCompensationonly strips a line that is indented at least N:So a line indented less than the fence keeps all of its indentation instead of losing what it has.
Expectation
For example 133, a fence indented by three spaces:
aaa
aaa
aaa
gives
aaa\n aaa\naaa\n. The four space line keeps one, the two space line keeps none.Result
aaa\n aaa\n aaa\n. The four space line is handled correctly, the two space line keeps both of its spaces.What was attempted
node.slice(Math.min(indentInNode.length, indentToCode.length))covers both directions, so the branch goes away rather than gaining a case.I diffed the exact-match failure set across the whole spec before and after: 133 moves to passing and nothing else changes, in both the CommonMark and GFM runs.
This is the same blind spot as #4073.
htmlIsEqualbuilds@markedjs/html-differwith onlyignoreSelfClosingSlashandignoreComments, soignoreWhitespacesstays at its default oftrue. Whitespace insidepre/codeis significant, so 133 was already reported as passing and carries noshouldFailflag. The spec suite therefore cannot protect this fix either, which is why the tests below assert exact output.A correction to something I wrote in #4073 and in #4050, since it is wrong and has been repeated since: I described example 318 as a lexer bug. It is not. Code token text has no single convention,
'```\nb\n```'gives"b"while' b\n'gives"b\n", andRenderer.code's strip-then-append exists to reconcile that. The token is correct under marked's own convention and the renderer is where the trailing blank lines are lost. I tried the obvious fix, moving the newline inside the fences capture group and reducing the renderer toconst code = text. It fixes 126, 130, 144, 237 and 318, and breaks 117, 128, 211, 236, 253 and 272, because indented code and fenced code inside a blockquote genuinely have no trailing newline in the token and rely on the renderer supplying it. Fixing 318 properly means agreeing one convention across every code token path, which changestoken.textfor fenced code and is a decision for the team rather than a drive-by PR.Contributor
Two tests in
test/unit/marked.test.jsunderfenced code block indentation. The first fails without the change; the second, a line indented more than the fence, passes either way as a control. Full spec suite (1789) and unit suite (193) pass.Committer
In most cases, this should be a different person than the contributor.