Skip to content

Commit

Permalink
fix(utils): recognize ~~~ as code fences in link replacement (faceboo…
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored and dpang314 committed Jul 24, 2022
1 parent c3fbc21 commit 24b6343
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Expand Up @@ -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)
~~~
",
}
`;
Expand Down
12 changes: 12 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts
Expand Up @@ -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();
Expand Down
16 changes: 7 additions & 9 deletions packages/docusaurus-utils/src/markdownLinks.ts
Expand Up @@ -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;
}

Expand Down

0 comments on commit 24b6343

Please sign in to comment.