diff --git a/packages/docusaurus-utils/src/__tests__/__snapshots__/markdownLinks.test.ts.snap b/packages/docusaurus-utils/src/__tests__/__snapshots__/markdownLinks.test.ts.snap index 18260d1b6051..afef584b1154 100644 --- a/packages/docusaurus-utils/src/__tests__/__snapshots__/markdownLinks.test.ts.snap +++ b/packages/docusaurus-utils/src/__tests__/__snapshots__/markdownLinks.test.ts.snap @@ -110,6 +110,20 @@ exports[`replaceMarkdownLinks ignores links in inline code 1`] = ` } `; +exports[`replaceMarkdownLinks replaces Markdown links with spaces 1`] = ` +{ + "brokenMarkdownLinks": [], + "newContent": " +[doc a](/docs/doc%20a) +[doc a]() +[doc a](/docs/doc%20a) +[doc b](/docs/my%20docs/doc%20b) +[doc b]() +[doc b](/docs/my%20docs/doc%20b) +", +} +`; + exports[`replaceMarkdownLinks replaces links with same title as URL 1`] = ` { "brokenMarkdownLinks": [], diff --git a/packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts b/packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts index 4a17e1140afb..eb311c481fe1 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts @@ -256,6 +256,31 @@ The following operations are defined for [URI]s: }, fileString: ` [a](a.md), [a](a.md), [b](b.md), [c](c.md) +`, + }), + ).toMatchSnapshot(); + }); + + it('replaces Markdown links with spaces', () => { + expect( + replaceMarkdownLinks({ + siteDir: '.', + filePath: 'docs/intro.md', + contentPaths: { + contentPath: 'docs', + contentPathLocalized: 'i18n/docs-localized', + }, + sourceToPermalink: { + '@site/docs/doc a.md': '/docs/doc%20a', + '@site/docs/my docs/doc b.md': '/docs/my%20docs/doc%20b', + }, + fileString: ` +[doc a](./doc%20a.md) +[doc a](<./doc a.md>) +[doc a](./doc a.md) +[doc b](./my%20docs/doc%20b.md) +[doc b](<./my docs/doc b.md>) +[doc b](./my docs/doc b.md) `, }), ).toMatchSnapshot(); diff --git a/packages/docusaurus-utils/src/markdownLinks.ts b/packages/docusaurus-utils/src/markdownLinks.ts index 6a34b275f3d4..1cb3cf5d85eb 100644 --- a/packages/docusaurus-utils/src/markdownLinks.ts +++ b/packages/docusaurus-utils/src/markdownLinks.ts @@ -103,7 +103,7 @@ export function replaceMarkdownLinks({ // This is [Document 1](doc1.md) // [doc1]: doc1.md const mdRegex = - /(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)(?[^'")\]\s>]+\.mdx?)/g; + /(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)[^'"\]\s>]+(?:\s[^'"\]\s>]+)*\.mdx?)>?/g; let mdMatch = mdRegex.exec(modifiedLine); while (mdMatch !== null) { // Replace it to correct html link.