Skip to content

Commit

Permalink
wip: fix todo, introduce side effect
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Dec 26, 2023
1 parent 87af79c commit 8473fbc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
25 changes: 12 additions & 13 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ describe('handleBrokenLinks NEW TESTS', () => {
`);
});

// TODO it does not reject
it('rejects valid link with broken anchor + query-string', async () => {
await expect(() =>
testBrokenLinks({
Expand Down Expand Up @@ -305,7 +304,6 @@ describe('handleBrokenLinks NEW TESTS', () => {
);
});

// TODO it does not reject
it('rejects valid link with broken anchor to uncollected page', async () => {
await expect(() =>
testBrokenLinks({
Expand All @@ -316,18 +314,18 @@ describe('handleBrokenLinks NEW TESTS', () => {
},
}),
).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.
"Docusaurus found broken anchors!
Exhaustive list of all broken links found:
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)
"
`);
});

// TODO it does not reject
it('rejects broken anchor with query-string to uncollected page', async () => {
await expect(() =>
testBrokenLinks({
Expand All @@ -341,13 +339,14 @@ describe('handleBrokenLinks NEW TESTS', () => {
},
}),
).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.
"Docusaurus found broken anchors!
Exhaustive list of all broken links found:
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)
"
`);
});
Expand Down
22 changes: 15 additions & 7 deletions packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function checkAnchorsInOtherRoutes(allCollectedCorrectLinks: CollectedLinks): {
} {
const brokenLinksByLocation: BrokenLinksByLocation = {};

const linkEntries = Object.entries(allCollectedCorrectLinks);
const linkCollection = Object.entries(allCollectedCorrectLinks);

linkEntries.forEach(([key, value]) => {
const brokenLinks = value.links.flatMap((link) => {
linkCollection.forEach(([path, collection]) => {
const brokenLinks = collection.links.flatMap((link) => {
const {route, anchor} = getRouteAndAnchor(link);
// const [route, anchor] = link.split('#');
if (route !== '' && anchor !== undefined) {
const targetRoute = allCollectedCorrectLinks[route!];
if (anchor !== undefined) {
const targetRoute = allCollectedCorrectLinks[route];

if (targetRoute && !targetRoute.anchors.includes(anchor)) {
return [
{
Expand All @@ -66,13 +66,21 @@ function checkAnchorsInOtherRoutes(allCollectedCorrectLinks: CollectedLinks): {
anchor: true,
},
];
} else if (!targetRoute) {
return [
{
link: `${route}#${anchor}`,
resolvedLink: route!,
anchor: true,
},
];
}
}
return [];
});

if (brokenLinks.length > 0) {
brokenLinksByLocation[key] = brokenLinks;
brokenLinksByLocation[path] = brokenLinks;
}
});

Expand Down

0 comments on commit 8473fbc

Please sign in to comment.