Skip to content

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

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

etrepum merged 4 commits into
meta:mainfrom
lytion:lytion/refactor-link-regex

Conversation

@lytion

@lytion lytion commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

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

vercel Bot commented Aug 4, 2025 •

Copy link
Copy Markdown

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

@etrepum etrepum left a comment

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.

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

lytion commented Aug 4, 2025

Copy link
Copy Markdown
Contributor Author

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

@etrepum etrepum left a comment

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.

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

lytion commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

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.

@etrepum etrepum left a comment

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 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 meta:main with commit 8fd8048 Sep 3, 2025
@etrepum etrepum mentioned this pull request Sep 5, 2025

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 63465aa6 Deployed Sep 3, 2025 by vercel[bot]
Preview – lexical — 63465aa6 Deployed Sep 3, 2025 by vercel[bot]
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