Skip to content

Commit

Permalink
fix(utils): recognize ~~~ as code fences in link replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Jul 18, 2022
1 parent fe3dfa7 commit c4315a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 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
6 changes: 3 additions & 3 deletions packages/docusaurus-utils/src/markdownLinks.ts
Expand Up @@ -85,14 +85,14 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
let fencedBlock = false;
let lastCodeFence = '';
const lines = fileString.split('\n').map((line) => {
if (line.trim().startsWith('```')) {
const codeFence = line.trim().match(/^`+/)![0]!;
const codeFence = line.trimStart().match(/^`{3,}|^~{3,}/)?.[0];
if (codeFence) {
if (!fencedBlock) {
fencedBlock = true;
lastCodeFence = codeFence;
// If we are in a ````-fenced block, all ``` would be plain text instead
// of fences
} else if (codeFence.length >= lastCodeFence.length) {
} else if (codeFence.startsWith(lastCodeFence)) {
fencedBlock = false;
}
}
Expand Down

0 comments on commit c4315a1

Please sign in to comment.