fix(v2): broken link checker should not report false positives when using 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.
This commit is contained in:
Harvtronix 2021-03-12 09:50:40 -05:00 committed by GitHub
parent 2f53b1a895
commit 735b3b3cc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

View file

@ -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,
);
});
});
});

View file

@ -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;
}