Skip to content

[lexical-markdown] Bug Fix: Allow any characters in markdown link text#7735

Merged
etrepum merged 4 commits intofacebook:mainfrom
lytion:lytion/refactor-link-regex
Sep 3, 2025
Merged

[lexical-markdown] Bug Fix: Allow any characters in markdown link text#7735
etrepum merged 4 commits intofacebook:mainfrom
lytion:lytion/refactor-link-regex

Conversation

@lytion
Copy link
Copy Markdown
Contributor

@lytion lytion commented Aug 4, 2025

Description

In markdown view, type
[[Title] description ](https://example.com) [test [squared] abc](https://example.com)
Switch off markdown to see how the link is formatted.

Before

Link text doesn't handle square brackets properly
image
Notice square bracket isn't part of the link for the first example and how it doesn't properly format the second one.

After

Link text can now accept any character in the text part
In this example the square bracket is now part of the link and everything is correctly formatted
image

@vercel
Copy link
Copy Markdown

vercel bot commented Aug 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
lexical Ready Ready Preview Comment Sep 3, 2025 0:19am
lexical-playground Ready Ready Preview Comment Sep 3, 2025 0:19am

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 4, 2025
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

In this sort of case it would be really nice to have some additional tests to show that this works as expected and also doesn't have edge cases, such as testing two independent links that are in the same line of text

@lytion
Copy link
Copy Markdown
Contributor Author

lytion commented Aug 4, 2025

Indeed, I will look into this

1. Basic case with opening and closing brackets
2. Closing bracket with no corresponding opening one
3. Opening bracket with no corresponding closing one
4. Opening bracket with a bracket combination in title
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

I think that maybe some revisions could be done here to more closely match how other commonmark parsers work, provided examples for the commonmark dingus and github

Comment on lines +656 to +657
html: '<p><a href="https://lexical.dev"><span style="white-space: pre-wrap;">hello]</span></a><a href="https://lexical.dev"><span style="white-space: pre-wrap;">world</span></a></p>',
md: '[hello]](https://lexical.dev)[world](https://lexical.dev)',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This one parses differently in commonmark, you can try it here https://spec.commonmark.org/dingus/

<p>[hello]](https://lexical.dev)<a href="https://lexical.dev">world</a></p>

This is how github parses it:

[hello]](https://lexical.dev)[world](https://lexical.dev)

<p dir="auto">[hello]](<a href="https://lexical.dev)%5Bworld%5D(https://lexical.dev" rel="nofollow">https://lexical.dev)[world](https://lexical.dev</a>)</p>

Comment on lines +660 to +661
html: '<p><a href="https://lexical.dev"><span style="white-space: pre-wrap;">hello[</span></a><a href="https://lexical.dev"><span style="white-space: pre-wrap;">world</span></a></p>',
md: '[hello[](https://lexical.dev)[world](https://lexical.dev)',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

also this one

<p>[hello<a href="https://lexical.dev"></a><a href="https://lexical.dev">world</a></p>

This is how github parses it:

[helloworld

<p dir="auto">[hello<a href="https://lexical.dev" rel="nofollow"></a><a href="https://lexical.dev" rel="nofollow">world</a></p>

Comment on lines +664 to +665
html: '<p><a href="https://lexical.dev" title="ti[t]le"><span style="white-space: pre-wrap;">hello[</span></a><a href="https://lexical.dev" title="tit]le"><span style="white-space: pre-wrap;">world[</span></a></p>',
md: '[hello[](https://lexical.dev "ti[t]le")[world[](https://lexical.dev "tit]le")',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

and this one

<p>[hello<a href="https://lexical.dev" title="ti[t]le"></a>[world<a href="https://lexical.dev" title="tit]le"></a></p>

this is how github parses it:

[hello[world

<p dir="auto">[hello<a href="https://lexical.dev" title="ti[t]le" rel="nofollow"></a>[world<a href="https://lexical.dev" title="tit]le" rel="nofollow"></a></p>

@lytion
Copy link
Copy Markdown
Contributor Author

lytion commented Aug 6, 2025

I totally understand the will to get close to those references. I'm also not omniscient in markdown so maybe I'm missing something but I believe the solution I'm suggesting allow to handle more edge cases and it seems to work quite well when looking at tests.
But if that's a no go I can look to only allow square brackets only if there's an opening one followed by a closing one, which is the rule dingus and github seem to follow.

…sponding closed one.

This follows commonmark rules:
- [h[ello](https://lexical.dev) -> ello will be linked
- [h]ello](https://lexical.dev) -> no link
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

This looks consistent with other Markdown implementations, nice work!

@etrepum etrepum added this pull request to the merge queue Sep 3, 2025
Merged via the queue into facebook:main with commit 8fd8048 Sep 3, 2025
39 checks passed
@etrepum etrepum mentioned this pull request Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants