Skip to content

fix: keep literal codespan content inside link text - #4189

Open
dyk1454683243-sudo wants to merge 1 commit into
markedjs:masterfrom
dyk1454683243-sudo:fix/4176-codespan-in-link-text
Open

dyk1454683243-sudo wants to merge 1 commit into
markedjs:masterfrom
dyk1454683243-sudo:fix/4176-codespan-in-link-text

Conversation

@dyk1454683243-sudo

Copy link
Copy Markdown
Contributor

Fixes #4176

Marked version: 18.1.2

Markdown flavor: CommonMark

Description

Code spans bind tighter than link brackets, and their contents are literal. Two mistakes in link-label handling broke that:

  1. outputLink ran cap[1].replace(/\\([\[\]])/g, '$1') over the whole label before inline lexing, so a backslash inside a code span was removed. Labels with no backtick still use that unescape. A label that also contains a backtick copies code spans through (same delimiter shape as inline code) and only unescapes brackets outside them.

  2. The code alternative in the inline label pattern (`+(?!`)[^`]*?`+(?!`)) does not require the opening and closing backtick runs to be the same length, so a code span can borrow a closer from a later link. A backreference in that pattern would renumber the public href and title groups, so the link regex is unchanged. When a non-pedantic match's label contains a backtick, Tokenizer.link measures the label with the same rules as code spans and, if the real ] is earlier, rebuilds the match from there. A ] that does not start an inline destination deactivates that opener, and the end is never moved past the original match.

Escaped brackets outside code spans are unchanged, including \\[ / \\].

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

New exact-output specs in test/specs/new/:

  • link_text_codespan_backslash — [`\]`](url) keeps the backslash
  • link_text_codespan_bracket — [`\[`](url) keeps the backslash
  • link_text_codespan_backtick — [ ``](url)[\](url) `` is two links
  • link_text_escaped_bracket — brackets escaped outside a code span still unescape, including a double backslash and an image alt
  • link_text_codespan_failed_closer — a ] that does not close the link is not traded for a later destination

Committer

@vercel

vercel Bot commented Nov 3, 2026

Copy link
Copy Markdown

@cursoragent is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

Code spans bind tighter than link brackets, so a backslash inside a
code span stays literal and a code span cannot borrow a closer from a
later backtick run of a different length.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
@cursor
cursor Bot force-pushed the fix/4176-codespan-in-link-text branch from 7e89e09 to 0b6fe19 Compare November 3, 2026 23:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code span inside link text drops the backslash or ends the link early (\], \[, backtick)

2 participants