Skip to content

Commit

Permalink
fix(utils): handle Markdown links with spaces to route correctly (#8874)
Browse files Browse the repository at this point in the history
  • Loading branch information
morsko1 authored Apr 12, 2023
1 parent 3d31c55 commit 2b99426
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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](</docs/doc%20a>)
[doc a](/docs/doc%20a)
[doc b](/docs/my%20docs/doc%20b)
[doc b](</docs/my%20docs/doc%20b>)
[doc b](/docs/my%20docs/doc%20b)
",
}
`;
exports[`replaceMarkdownLinks replaces links with same title as URL 1`] = `
{
"brokenMarkdownLinks": [],
Expand Down
25 changes: 25 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-utils/src/markdownLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
// This is [Document 1](doc1.md)
// [doc1]: doc1.md
const mdRegex =
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)<?(?<filename>[^'"\]\s>]+(?:\s[^'"\]\s>]+)*\.mdx?)>?/g;
let mdMatch = mdRegex.exec(modifiedLine);
while (mdMatch !== null) {
// Replace it to correct html link.
Expand Down

0 comments on commit 2b99426

Please sign in to comment.