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): better handling of code blocks in link replacement #9046

Merged
merged 1 commit into from
Jun 8, 2023
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 @@ -47,6 +47,22 @@ exports[`replaceMarkdownLinks handles stray spaces 1`] = `
}
`;

exports[`replaceMarkdownLinks handles unpaired fences 1`] = `
{
"brokenMarkdownLinks": [],
"newContent": "
\`\`\`foo
hello

\`\`\`foo
hello
\`\`\`

A [link](/docs/file)
",
}
`;

exports[`replaceMarkdownLinks ignores links in HTML comments 1`] = `
{
"brokenMarkdownLinks": [
Expand Down
26 changes: 26 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,32 @@ The following operations are defined for [URI]s:
[URL](./file.md?foo=bar#baz)
[URL](./file.md#a)
[URL](./file.md?c)
`,
}),
).toMatchSnapshot();
});

it('handles unpaired fences', () => {
expect(
replaceMarkdownLinks({
siteDir: '.',
filePath: 'docs/file.md',
contentPaths: {
contentPath: 'docs',
contentPathLocalized: 'i18n/docs-localized',
},
sourceToPermalink: {
'@site/docs/file.md': '/docs/file',
},
fileString: `
\`\`\`foo
hello

\`\`\`foo
hello
\`\`\`

A [link](./file.md)
`,
}),
).toMatchSnapshot();
Expand Down
40 changes: 31 additions & 9 deletions packages/docusaurus-utils/src/markdownLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ export type BrokenMarkdownLink<T extends ContentPaths> = {
link: string;
};

type CodeFence = {
type: '`' | '~';
definitelyOpen: boolean;
count: number;
};

function parseCodeFence(line: string): CodeFence | null {
const match = line.trim().match(/^(?<fence>`{3,}|~{3,})(?<rest>.*)/);
if (!match) {
return null;
}
return {
type: match.groups!.fence![0]! as '`' | '~',
definitelyOpen: !!match.groups!.rest!,
count: match.groups!.fence!.length,
};
}

/**
* Takes a Markdown file and replaces relative file references with their URL
* counterparts, e.g. `[link](./intro.md)` => `[link](/docs/intro)`, preserving
Expand Down Expand Up @@ -82,19 +100,23 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
const brokenMarkdownLinks: BrokenMarkdownLink<T>[] = [];

// Replace internal markdown linking (except in fenced blocks).
let lastCodeFence: string | null = null;
let lastOpenCodeFence: CodeFence | null = null;
const lines = fileString.split('\n').map((line) => {
const codeFence = line.trimStart().match(/^`{3,}|^~{3,}/)?.[0];
const codeFence = parseCodeFence(line);
if (codeFence) {
if (!lastCodeFence) {
lastCodeFence = codeFence;
// If we are in a ````-fenced block, all ``` would be plain text instead
// of fences
} else if (codeFence.startsWith(lastCodeFence)) {
lastCodeFence = null;
if (!lastOpenCodeFence) {
lastOpenCodeFence = codeFence;
} else if (
!codeFence.definitelyOpen &&
lastOpenCodeFence.type === codeFence.type &&
lastOpenCodeFence.count <= codeFence.count
) {
// All three conditions must be met in order for this to be considered
// a closing fence.
lastOpenCodeFence = null;
}
}
if (lastCodeFence) {
if (lastOpenCodeFence) {
return line;
}

Expand Down