Skip to content

Commit

Permalink
fix: handle query strings
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Dec 26, 2023
1 parent 5211d04 commit 87af79c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
34 changes: 31 additions & 3 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,17 @@ describe('handleBrokenLinks NEW TESTS', () => {
'/page2': {links: [], anchors: []},
},
}),
).rejects.toThrowErrorMatchingInlineSnapshot();
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Docusaurus found broken anchors!
Please check the pages of your site in the list below, and make sure you don't reference any anchor that does not exist.
Note: it's possible to ignore broken anchors with the 'onBrokenAnchors' Docusaurus configuration, and let the build pass.
Exhaustive list of all broken anchors found:
- Broken anchor on source page path = /page1:
-> linking to /page2#brokenAnchor (resolved as: /page2)
"
`);
});

it('rejects valid link with broken anchor to self', async () => {
Expand Down Expand Up @@ -305,7 +315,16 @@ describe('handleBrokenLinks NEW TESTS', () => {
// /page2 is absent on purpose: it doesn't contain any link/anchor
},
}),
).rejects.toThrowErrorMatchingInlineSnapshot();
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Docusaurus found broken links!
Please check the pages of your site in the list below, and make sure you don't reference any path that does not exist.
Note: it's possible to ignore broken links with the 'onBrokenLinks' Docusaurus configuration, and let the build pass.
Exhaustive list of all broken links found:
"
`);
});

// TODO it does not reject
Expand All @@ -321,7 +340,16 @@ describe('handleBrokenLinks NEW TESTS', () => {
// /page2 is absent on purpose: it doesn't contain any link/anchor
},
}),
).rejects.toThrowErrorMatchingInlineSnapshot();
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Docusaurus found broken links!
Please check the pages of your site in the list below, and make sure you don't reference any path that does not exist.
Note: it's possible to ignore broken links with the 'onBrokenLinks' Docusaurus configuration, and let the build pass.
Exhaustive list of all broken links found:
"
`);
});

it('can ignore broken links', async () => {
Expand Down
25 changes: 23 additions & 2 deletions packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,33 @@ function onlyPathname(link: string) {
return link.split('#')[0]!.split('?')[0]!;
}

function getRouteAndAnchor(link: string) {
const url = new URL(link, 'https://example.com');
const [, splitAnchor] = link.split('#');

const route = url.pathname;
const anchor = url.hash.slice(1) || undefined;

if (splitAnchor === '') {
// rejects valid link with empty broken anchor
// new URL will return an empty string /docs# and /docs
return {route, anchor: ''};
}

return {route, anchor};
}

function checkAnchorsInOtherRoutes(allCollectedCorrectLinks: CollectedLinks): {
[location: string]: BrokenLink[];
} {
const brokenLinksByLocation: BrokenLinksByLocation = {};

Object.entries(allCollectedCorrectLinks).forEach(([key, value]) => {
const linkEntries = Object.entries(allCollectedCorrectLinks);

linkEntries.forEach(([key, value]) => {
const brokenLinks = value.links.flatMap((link) => {
const [route, anchor] = link.split('#');
const {route, anchor} = getRouteAndAnchor(link);
// const [route, anchor] = link.split('#');
if (route !== '' && anchor !== undefined) {
const targetRoute = allCollectedCorrectLinks[route!];
if (targetRoute && !targetRoute.anchors.includes(anchor)) {
Expand Down Expand Up @@ -149,6 +168,8 @@ function getAllBrokenLinks({
const brokenLinks = Object.fromEntries(
Object.entries(allBrokenLinks).filter(([, value]) => value.length > 0),
);
// console.log('brokenLinks:', brokenLinks);
// console.log('allBrokenLinks:', allBrokenLinks);

const brokenAnchors = checkAnchorsInOtherRoutes(allCollectedLinks);

Expand Down

0 comments on commit 87af79c

Please sign in to comment.