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): avoid replacing Markdown links missing the directly next link #7458

Merged
merged 1 commit into from
May 21, 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 @@ -135,6 +135,13 @@ The following operations are defined for [URI]s:
}
`;

exports[`replaceMarkdownLinks replaces two links on the same line 1`] = `
{
"brokenMarkdownLinks": [],
"newContent": "[TypeScript](/programming-languages/typescript/) and [Go](/programming-languages/go/)",
}
`;

exports[`replaceMarkdownLinks resolves absolute and relative links differently 1`] = `
{
"brokenMarkdownLinks": [
Expand Down
33 changes: 31 additions & 2 deletions packages/docusaurus-utils/src/__tests__/markdownLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ describe('replaceMarkdownLinks', () => {
).toMatchSnapshot();
});

it('replaces two links on the same line', () => {
// cSpell:ignore Goooooooooo
// This is a very arcane bug: if we continue matching using the previous
// matching index (as is the behavior of RegExp#exec), it will go right over
// the next Markdown link and fail to match the "Go" link. This only happens
// when: (1) the replaced link is much shorter than the Markdown path, (2)
// the next link is very close to the current one (e.g. here if it's not
// "Go" but "Goooooooooo", or if every link has the /docs/ prefix, the bug
// will not trigger because it won't overshoot)
expect(
replaceMarkdownLinks({
siteDir: '.',
filePath: 'docs/intro.md',
contentPaths: {
contentPath: 'docs',
contentPathLocalized: 'i18n/docs-localized',
},
sourceToPermalink: {
'@site/docs/intro.md': '/',
'@site/docs/programming-languages/typescript/typescript.md':
'/programming-languages/typescript/',
'@site/docs/programming-languages/go/go.md':
'/programming-languages/go/',
},
fileString: `[TypeScript](programming-languages/typescript/typescript.md) and [Go](programming-languages/go/go.md)`,
}),
).toMatchSnapshot();
});

it('replaces reference style Markdown links', () => {
expect(
replaceMarkdownLinks({
Expand Down Expand Up @@ -155,7 +184,7 @@ The following operations are defined for [URI]s:
).toMatchSnapshot();
});

// TODO bad
// FIXME
it('ignores links in inline code', () => {
expect(
replaceMarkdownLinks({
Expand All @@ -175,7 +204,7 @@ The following operations are defined for [URI]s:
).toMatchSnapshot();
});

// TODO bad
// FIXME
it('replaces links with same title as URL', () => {
expect(
replaceMarkdownLinks({
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-utils/src/markdownLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
.map((part) => part.replace(/\s/g, '%20'))
.join('/');
modifiedLine = modifiedLine.replace(mdLink, encodedPermalink);
// Adjust the lastIndex to avoid passing over the next link if the
// newly replaced URL is shorter.
mdRegex.lastIndex += encodedPermalink.length - mdLink.length;
} else {
const brokenMarkdownLink: BrokenMarkdownLink<T> = {
contentPaths,
Expand Down