Skip to content

Commit

Permalink
fix(utils): avoid replacing Markdown links missing the directly next …
Browse files Browse the repository at this point in the history
…link (#7458)
  • Loading branch information
Josh-Cena authored May 21, 2022
1 parent 0f8f918 commit ad888b7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
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

0 comments on commit ad888b7

Please sign in to comment.