Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6fae23b

Browse files
morsko1slorber
authored andcommittedMay 15, 2023
fix(utils): handle Markdown links with spaces to route correctly (#8874)
1 parent d20e9c4 commit 6fae23b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
 

‎packages/docusaurus-utils/src/__tests__/__snapshots__/markdownLinks.test.ts.snap

+14
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ exports[`replaceMarkdownLinks ignores links in inline code 1`] = `
9898
}
9999
`;
100100
101+
exports[`replaceMarkdownLinks replaces Markdown links with spaces 1`] = `
102+
{
103+
"brokenMarkdownLinks": [],
104+
"newContent": "
105+
[doc a](/docs/doc%20a)
106+
[doc a](</docs/doc%20a>)
107+
[doc a](/docs/doc%20a)
108+
[doc b](/docs/my%20docs/doc%20b)
109+
[doc b](</docs/my%20docs/doc%20b>)
110+
[doc b](/docs/my%20docs/doc%20b)
111+
",
112+
}
113+
`;
114+
101115
exports[`replaceMarkdownLinks replaces links with same title as URL 1`] = `
102116
{
103117
"brokenMarkdownLinks": [],

‎packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,31 @@ The following operations are defined for [URI]s:
244244
},
245245
fileString: `
246246
[a](a.md), [a](a.md), [b](b.md), [c](c.md)
247+
`,
248+
}),
249+
).toMatchSnapshot();
250+
});
251+
252+
it('replaces Markdown links with spaces', () => {
253+
expect(
254+
replaceMarkdownLinks({
255+
siteDir: '.',
256+
filePath: 'docs/intro.md',
257+
contentPaths: {
258+
contentPath: 'docs',
259+
contentPathLocalized: 'i18n/docs-localized',
260+
},
261+
sourceToPermalink: {
262+
'@site/docs/doc a.md': '/docs/doc%20a',
263+
'@site/docs/my docs/doc b.md': '/docs/my%20docs/doc%20b',
264+
},
265+
fileString: `
266+
[doc a](./doc%20a.md)
267+
[doc a](<./doc a.md>)
268+
[doc a](./doc a.md)
269+
[doc b](./my%20docs/doc%20b.md)
270+
[doc b](<./my docs/doc b.md>)
271+
[doc b](./my docs/doc b.md)
247272
`,
248273
}),
249274
).toMatchSnapshot();

‎packages/docusaurus-utils/src/markdownLinks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
105105
// This is [Document 1](doc1.md)
106106
// [doc1]: doc1.md
107107
const mdRegex =
108-
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
108+
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)<?(?<filename>[^'"\]\s>]+(?:\s[^'"\]\s>]+)*\.mdx?)>?/g;
109109
let mdMatch = mdRegex.exec(modifiedLine);
110110
while (mdMatch !== null) {
111111
// Replace it to correct html link.

0 commit comments

Comments
 (0)
Please sign in to comment.