mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-01 03:08:17 +02:00
fix(utils): handle Markdown links with spaces to route correctly (#8874)
This commit is contained in:
parent
3d31c55fe7
commit
2b994262b0
3 changed files with 40 additions and 1 deletions
|
@ -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`] = `
|
exports[`replaceMarkdownLinks replaces links with same title as URL 1`] = `
|
||||||
{
|
{
|
||||||
"brokenMarkdownLinks": [],
|
"brokenMarkdownLinks": [],
|
||||||
|
|
|
@ -256,6 +256,31 @@ The following operations are defined for [URI]s:
|
||||||
},
|
},
|
||||||
fileString: `
|
fileString: `
|
||||||
[a](a.md), [a](a.md), [b](b.md), [c](c.md)
|
[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();
|
).toMatchSnapshot();
|
||||||
|
|
|
@ -103,7 +103,7 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
|
||||||
// This is [Document 1](doc1.md)
|
// This is [Document 1](doc1.md)
|
||||||
// [doc1]: doc1.md
|
// [doc1]: doc1.md
|
||||||
const mdRegex =
|
const mdRegex =
|
||||||
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
|
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)<?(?<filename>[^'"\]\s>]+(?:\s[^'"\]\s>]+)*\.mdx?)>?/g;
|
||||||
let mdMatch = mdRegex.exec(modifiedLine);
|
let mdMatch = mdRegex.exec(modifiedLine);
|
||||||
while (mdMatch !== null) {
|
while (mdMatch !== null) {
|
||||||
// Replace it to correct html link.
|
// Replace it to correct html link.
|
||||||
|
|
Loading…
Add table
Reference in a new issue