Skip to content

fix: don't escape [ bracket in linktext#2894

Draft
Soxasora wants to merge 1 commit intostackernews:masterfrom
Soxasora:fix/unescape-brackets-linktext
Draft

fix: don't escape [ bracket in linktext#2894
Soxasora wants to merge 1 commit intostackernews:masterfrom
Soxasora:fix/unescape-brackets-linktext

Conversation

@Soxasora
Copy link
Copy Markdown
Member

@Soxasora Soxasora commented Mar 30, 2026

Description

Fixes #2879

gfm-footnote toMarkdown extension marks [ as unsafe in phrasing to protect from unwanted footnotes or nested links, but footnotes or nested links can't start anyway inside of linktext.
This PR unescapes [ square brackets inside of linktext (text node inside of a link node).

Screenshots

unescape.mov

Additional Context

One of the goals of future stages is to implement a better method for customizing escaping.

Checklist

Are your changes backward compatible? Please answer below:

For example, a change is not backward compatible if you removed a GraphQL field or dropped a database column.
Yes
On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
7

For frontend changes: Tested on mobile, light and dark mode? Please answer below:
n/a

Did you introduce any new environment variables? If so, call them out explicitly here:
n/a

Did you use AI for this? If so, how much did it assist you?
review, tests


Note

Low Risk
Low risk: narrow change to Markdown serialization that only alters escaping behavior inside link text; main risk is minor formatting regressions in edge-case link rendering.

Overview
Fixes Lexical-to-Markdown export so link text no longer keeps escaped \[ characters introduced by the gfm-footnote phrasing safety rules. The link toMarkdown handler now unescapes [ within link text while still emitting standard [text](url "title") syntax.

Written by Cursor Bugbot for commit c307919. This will update automatically on new commits. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

return `[${text}](${node.url || ''}${node.title ? ` "${node.title}"` : ''})`
// gfm-footnote extension marks [ as unsafe in phrasing,
// but [ can't start footnotes or nested links inside link text, so the escape is unnecessary
return `[${text.replace(/\\\[/g, '[')}](${node.url || ''}${node.title ? ` "${node.title}"` : ''})`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unescaping [ in link text breaks outer link parsing

Medium Severity

The .replace(/\\\[/g, '[') regex unescapes all \[ in link text, but an unescaped [ inside link text can combine with the outer link's ](url) to form an unintended inner link, breaking the outer link entirely. For example, a link with text click [here and URL url would serialize to [click [here](url), which CommonMark parses as literal [click plus a link hereurl — the intended outer link is destroyed. The escaping that state.safe() applies to [ is necessary to prevent inner bracket sequences from taking precedence over the outer link structure per the CommonMark spec.

Fix in Cursor Fix in Web

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, and it makes sense. Maybe escaped brackets don't cause issues, I'll think about this.

@Soxasora Soxasora marked this pull request as draft March 30, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Brackets [] in link text are incorrectly escaped when roundtripped through MDAST pipeline

1 participant