fix(utils): convert Markdown links in reference-style links (#6617)

This commit is contained in:
Joshua Chen 2022-02-05 19:37:20 +08:00 committed by GitHub
parent d2574b287f
commit 74a645e612
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View file

@ -62,6 +62,49 @@ describe('replaceMarkdownLinks', () => {
`);
});
test('reference style Markdown links', () => {
expect(
replaceMarkdownLinks({
siteDir: '.',
filePath: 'docs/intro/intro.md',
contentPaths: {
contentPath: 'docs',
contentPathLocalized: 'i18n/docs-localized',
},
sourceToPermalink: {
'@site/docs/intro/intro.md': '/docs/intro',
'@site/docs/api/classes/divine_uri.URI.md': '/docs/api/classes/uri',
},
fileString: `
The following operations are defined for [URI]s:
* [info]: Returns metadata about the resource,
* [list]: Returns metadata about the resource's children (like getting the content of a local directory).
[URI]: ../api/classes/divine_uri.URI.md
[info]: ../api/classes/divine_uri.URI.md#info
[list]: ../api/classes/divine_uri.URI.md#list
`,
}),
).toMatchInlineSnapshot(`
Object {
"brokenMarkdownLinks": Array [],
"newContent": "
The following operations are defined for [URI]s:
* [info]: Returns metadata about the resource,
* [list]: Returns metadata about the resource's children (like getting the content of a local directory).
[URI]: /docs/api/classes/uri
[info]: /docs/api/classes/uri#info
[list]: /docs/api/classes/uri#list
",
}
`);
});
// TODO bad
test('links in HTML comments', () => {
expect(

View file

@ -67,7 +67,7 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
// ink
// [doc1]: doc1.md -> we replace this doc1.md with correct link
const mdRegex =
/(?:(?:\]\()|(?:\]:\s?))(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
/(?:(?:\]\()|(?:\]:\s*))(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
let mdMatch = mdRegex.exec(modifiedLine);
while (mdMatch !== null) {
// Replace it to correct html link.