Skip to content

Commit 0a0afb0

Browse files
fix(richtext-lexical): link markdown regex should not match similar looking image markdown (#15713)
The current regex matches the link markdown correctly: ``` [This is a link](https://example.com/foo/bar) ``` But it also matches the image markdown: ``` ![This is an image](https://example.com/foo/image.jpg) ``` To fix this, a negative lookbehind is added which confirms that no `!` is present before the match. <!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> --------- Co-authored-by: German Jablonski <43938777+GermanJablo@users.noreply.github.com>
1 parent 4861fa1 commit 0a0afb0

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

packages/richtext-lexical/src/features/link/markdownTransformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export const LinkMarkdownTransformer: TextMatchTransformer = {
2929

3030
return linkContent
3131
},
32-
importRegExp: /\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)/,
33-
regExp: /\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)$/,
32+
importRegExp: /(?<!!)\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)/,
33+
regExp: /(?<!!)\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)$/,
3434
replace: (textNode, match) => {
3535
const [, linkText, linkUrl] = match
3636
const linkNode = $createLinkNode({

test/lexical-mdx/int.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ describe('Lexical MDX', () => {
248248
expect(text).toMatch(/alt|image\.jpg|\/uploads\//)
249249
})
250250
})
251+
252+
describe('link markdown: should not match image markdown', () => {
253+
it('should not parse image markdown as a link node', () => {
254+
const markdown = '![Alt text](https://example.com/image.jpg)'
255+
const result = mdxToEditorJSON({ mdxWithFrontmatter: markdown, editorConfig })
256+
const serialized = JSON.stringify(result.editorState)
257+
expect(serialized).toContain('"text":"![Alt text](https://example.com/image.jpg)"')
258+
259+
expect(serialized).not.toContain('"type":"link"')
260+
})
261+
})
251262
})
252263

253264
function removeUndefinedAndIDRecursively(obj: object) {

0 commit comments

Comments
 (0)