Skip to content

Commit

Permalink
fix(v2): broken link checker should not report false positives when u…
Browse files Browse the repository at this point in the history
…sing encoded chars (#4407)

In order to create markdown links to URIs containing spaces, an
encoded space (%20) must be used. These types of links were not
properly resolved to doc files. This fix allows them to be
resolved by calling `decodeURI` on the links found in markdown files to
unescape characters such as spaces.
  • Loading branch information
Harvtronix committed Mar 12, 2021
1 parent 2f53b1a commit 735b3b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
50 changes: 50 additions & 0 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,54 @@ describe('brokenLinks', () => {
});
expect(result).toEqual(allCollectedLinksFiltered);
});

describe('Encoded link', () => {
test('getAllBrokenLinks', async () => {
const routes: RouteConfig[] = [
{
path: '/docs',
component: '',
routes: [
{path: '/docs/some doc', component: ''},
{path: '/docs/some other doc', component: ''},
{path: '/docs/weird%20file%20name', component: ''},
],
},
{
path: '*',
component: '',
},
];

const allCollectedLinks = {
'/docs/some doc': [
// good - valid file with spaces in name
'./some%20other%20doc',
// good - valid file with percent-20 in its name
'./weird%20file%20name',
// bad - non-existant file with spaces in name
'./some%20other%20non-existant%20doc',
// evil - trying to use ../../ but '/' won't get decoded
'./break%2F..%2F..%2Fout',
],
};

const expectedBrokenLinks = {
'/docs/some doc': [
{
link: './some%20other%20non-existant%20doc',
resolvedLink: '/docs/some%20other%20non-existant%20doc',
},
{
link: './break%2F..%2F..%2Fout',
resolvedLink: '/docs/break%2F..%2F..%2Fout',
},
],
};

expect(getAllBrokenLinks({allCollectedLinks, routes})).toEqual(
expectedBrokenLinks,
);
});
});
});
4 changes: 3 additions & 1 deletion packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function getPageBrokenLinks({
}

function isBrokenLink(link: string) {
const matchedRoutes = matchRoutes(toReactRouterRoutes(routes), link);
const matchedRoutes = [link, decodeURI(link)]
.map((l) => matchRoutes(toReactRouterRoutes(routes), l))
.reduce((prev, cur) => prev.concat(cur));
return matchedRoutes.length === 0;
}

Expand Down

0 comments on commit 735b3b3

Please sign in to comment.