mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(utils): make Markdown link replacement much more rigorous (#8927)
This commit is contained in:
parent
41f1c1e3c2
commit
9e0b62fe6c
3 changed files with 149 additions and 14 deletions
|
@ -277,10 +277,100 @@ The following operations are defined for [URI]s:
|
|||
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)
|
||||
[doc]: <./my docs/doc b.md>
|
||||
`,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('does not replace non-Markdown links', () => {
|
||||
const input = `
|
||||
[asset](./file.md_asset/1.png)
|
||||
[URL](<https://example.com/file_(1).md>)
|
||||
[not a link]((foo)
|
||||
[not a link](foo bar)
|
||||
[not a link]: foo bar
|
||||
[not a link]: (foo
|
||||
[not a link]: bar)
|
||||
`;
|
||||
expect(
|
||||
replaceMarkdownLinks({
|
||||
siteDir: '.',
|
||||
filePath: 'docs/file.md',
|
||||
contentPaths: {
|
||||
contentPath: 'docs',
|
||||
contentPathLocalized: 'i18n/docs-localized',
|
||||
},
|
||||
sourceToPermalink: {
|
||||
'@site/docs/file.md': '/docs/file',
|
||||
},
|
||||
fileString: input,
|
||||
}),
|
||||
).toEqual({
|
||||
newContent: input,
|
||||
brokenMarkdownLinks: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('handles stray spaces', () => {
|
||||
expect(
|
||||
replaceMarkdownLinks({
|
||||
siteDir: '.',
|
||||
filePath: 'docs/file.md',
|
||||
contentPaths: {
|
||||
contentPath: 'docs',
|
||||
contentPathLocalized: 'i18n/docs-localized',
|
||||
},
|
||||
sourceToPermalink: {
|
||||
'@site/docs/file.md': '/docs/file',
|
||||
},
|
||||
fileString: `
|
||||
[URL]( ./file.md )
|
||||
[ref]: ./file.md
|
||||
`,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('handles link titles', () => {
|
||||
expect(
|
||||
replaceMarkdownLinks({
|
||||
siteDir: '.',
|
||||
filePath: 'docs/file.md',
|
||||
contentPaths: {
|
||||
contentPath: 'docs',
|
||||
contentPathLocalized: 'i18n/docs-localized',
|
||||
},
|
||||
sourceToPermalink: {
|
||||
'@site/docs/file.md': '/docs/file',
|
||||
},
|
||||
fileString: `
|
||||
[URL](./file.md "title")
|
||||
[URL](./file.md 'title')
|
||||
[URL](./file.md (title))
|
||||
`,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('preserves query/hash', () => {
|
||||
expect(
|
||||
replaceMarkdownLinks({
|
||||
siteDir: '.',
|
||||
filePath: 'docs/file.md',
|
||||
contentPaths: {
|
||||
contentPath: 'docs',
|
||||
contentPathLocalized: 'i18n/docs-localized',
|
||||
},
|
||||
sourceToPermalink: {
|
||||
'@site/docs/file.md': '/docs/file',
|
||||
},
|
||||
fileString: `
|
||||
[URL](./file.md?foo=bar#baz)
|
||||
[URL](./file.md#a)
|
||||
[URL](./file.md?c)
|
||||
`,
|
||||
}),
|
||||
).toMatchSnapshot();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue