Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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