fix: strip whitespace when normalizing link reference labels - #4091
Merged
UziTech merged 1 commit intoSep 21, 2026
Merged
Conversation
CommonMark normalizes a link label by case-folding it, stripping leading and trailing whitespace, and collapsing internal runs. normalizeLabel does only the case fold, and the collapse is bolted onto one of its four call sites, so a definition and a reference that differ only in surrounding whitespace normalize to different keys and never match. Strip inside normalizeLabel, where all four call sites agree by construction.
|
@NgoQuocViet2001 is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
github-actions Bot
pushed a commit
that referenced
this pull request
Sep 22, 2026
## [18.0.14](v18.0.13...v18.0.14) (2026-09-22) ### Bug Fixes * allow indented lines in setext heading text ([#4095](#4095)) ([7d05530](7d05530)) * decode numeric character references in text ([#4076](#4076)) ([cead8de](cead8de)) * keep text after empty nested blockquote ([#4101](#4101)) ([1b89557](1b89557)), closes [#4098](#4098) * preserve internal tabs in list item content ([#4086](#4086)) ([e136da7](e136da7)) * strip whitespace when normalizing link reference labels ([#4091](#4091)) ([c7ee43a](c7ee43a)) * support GFM protocol autolinks ([#4067](#4067)) ([7f7a496](7f7a496))
This branch was successfully deployed
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.
Problem
CommonMark normalizes a link label in three steps — case-fold, strip leading and trailing whitespace, collapse internal runs.
normalizeLabeldoes only the first:The collapse is bolted onto one of its four call sites (
Tokenizer.ts:531, thedefrule);reflinkand the twoLexerlookups don't have it, and nothing strips anywhere. A definition and a reference that differ only in surrounding whitespace therefore normalize to different keys and never match.Compared against the
commonmark0.31.2 reference implementation already in devDependencies, normalizing away marked's<img>vs<img />difference:4 of 9 probe inputs diverge. The collapse cases (internal double space, tab, newline) all pass — the missing step is the strip.
The failure is silent: a valid reference link renders as literal bracket text, and in the masking case the surrounding emphasis is disturbed too.
Fix
Do the strip inside
normalizeLabel, so all four call sites agree by construction.Test plan
test/specs/new/link_reference_label_whitespace.{md,html}. The expected HTML is thecommonmarkreference implementation's own output for that input, not hand-written.node --test test/run-spec-tests.js→ 1815 passing (1813 before, plus the new pair).node --test test/unit/*.test.js→ 191 passing.helpers.tsfails the new spec. Worth noting that the existing 1813 pass identically with and without this change — the CommonMark spec files in the repo don't cover surrounding whitespace in a label, which is why this survived.eslint src/helpers.ts→ clean.