fix(utils): recognize ~~~ as code fences in link replacement (#7801)

This commit is contained in:
Joshua Chen 2022-07-18 19:16:00 +08:00 committed by GitHub
parent fe3dfa720a
commit 1dd65eee50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 9 deletions

View file

@ -76,6 +76,18 @@ exports[`replaceMarkdownLinks ignores links in fenced blocks 1`] = `
\`\`\`
[foo](foo.md)
\`\`\`\`
~~~js
[foo](foo.md)
~~~
~~~js
[foo](foo.md)
\`\`\`
[foo](foo.md)
\`\`\`
[foo](foo.md)
~~~
",
}
`;

View file

@ -179,6 +179,18 @@ The following operations are defined for [URI]s:
\`\`\`
[foo](foo.md)
\`\`\`\`
~~~js
[foo](foo.md)
~~~
~~~js
[foo](foo.md)
\`\`\`
[foo](foo.md)
\`\`\`
[foo](foo.md)
~~~
`,
}),
).toMatchSnapshot();

View file

@ -82,21 +82,19 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
const brokenMarkdownLinks: BrokenMarkdownLink<T>[] = [];
// Replace internal markdown linking (except in fenced blocks).
let fencedBlock = false;
let lastCodeFence = '';
let lastCodeFence: string | null = null;
const lines = fileString.split('\n').map((line) => {
if (line.trim().startsWith('```')) {
const codeFence = line.trim().match(/^`+/)![0]!;
if (!fencedBlock) {
fencedBlock = true;
const codeFence = line.trimStart().match(/^`{3,}|^~{3,}/)?.[0];
if (codeFence) {
if (!lastCodeFence) {
lastCodeFence = codeFence;
// If we are in a ````-fenced block, all ``` would be plain text instead
// of fences
} else if (codeFence.length >= lastCodeFence.length) {
fencedBlock = false;
} else if (codeFence.startsWith(lastCodeFence)) {
lastCodeFence = null;
}
}
if (fencedBlock) {
if (lastCodeFence) {
return line;
}