Skip to content

Commit

Permalink
wip: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Nov 23, 2023
1 parent f60242a commit ca33fb8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
8 changes: 5 additions & 3 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,23 @@ describe('handleBrokenLinks', () => {
},
'/docs/goodDoc': {
links: [
'#someHash',
'/community#anchorFromGoodDoc',
'./anotherGoodDoc#someHash',
'/docs/anotherGoodDoc?someQueryString=true#someHash',
'../docs/anotherGoodDoc?someQueryString=true',
'../docs/anotherGoodDoc#someHash',
],
anchors: ['someHash'],
anchors: ['someHash'], // anchors here are anchors of the page itself (/docs/goodDoc) not the links in it
},
'/community': {
links: [
'/docs/goodDoc',
'/docs/anotherGoodDoc#someHash',
'/docs/anotherGoodDoc#someHash', // anchor here is an anchor of an other page, it should be checked against the anchors of the other page
'./docs/goodDoc#someHash',
'./docs/anotherGoodDoc',
],
anchors: [],
anchors: ['anchorFromGoodDoc'],
},
// '/page1': {
// links: [
Expand Down
64 changes: 48 additions & 16 deletions packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,46 @@ type BrokenLink = {
anchor: boolean;
};

type CollectedLinks = {
[location: string]: {links: string[]; anchors: string[]};
};

// matchRoutes does not support qs/anchors, so we remove it!
function onlyPathname(link: string) {
return link.split('#')[0]!.split('?')[0]!;
}

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

Object.entries(allCollectedCorrectLinks).forEach(([key, value]) => {
const brokenLinks = value.links.flatMap((link) => {
const [route, anchor] = link.split('#');
if (route !== '' && anchor !== undefined) {
const targetRoute = allCollectedCorrectLinks[route!];
if (targetRoute && !targetRoute.anchors.includes(anchor)) {
return [
{
link: `${route}#${anchor}`,
resolvedLink: route!,
anchor: true,
},
];
}
}
return [];
});

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

return brokenLinksByLocation;
}

function getPageBrokenLinks({
pagePath,
pageLinks,
Expand Down Expand Up @@ -56,18 +91,14 @@ function getPageBrokenLinks({
}

function isAnchorBrokenLink(link: string) {
const urlHash = link.split('#')[1];
const [urlPath, urlHash] = link.split('#');

if (urlHash === undefined || pageAnchors.length === 0) {
// ignore anchors that are not on the current page
if (urlHash === undefined || pageAnchors.length === 0 || urlPath !== '') {
return false;
}

const brokenAnchors = pageAnchors.flatMap((anchor) => {
if (anchor === urlHash) {
return [];
}
return [anchor];
});
const brokenAnchors = pageAnchors.filter((anchor) => anchor !== urlHash);

return brokenAnchors.length > 0;
}
Expand Down Expand Up @@ -100,7 +131,7 @@ function getAllBrokenLinks({
allCollectedLinks,
routes,
}: {
allCollectedLinks: {[location: string]: {links: string[]; anchors: string[]}};
allCollectedLinks: CollectedLinks;
routes: RouteConfig[];
}): {[location: string]: BrokenLink[]} {
const filteredRoutes = filterIntermediateRoutes(routes);
Expand All @@ -116,7 +147,11 @@ function getAllBrokenLinks({
}),
);

return _.pickBy(allBrokenLinks, (brokenLinks) => brokenLinks.length > 0);
const allBrokenAnchors = checkAnchorsInOtherRoutes(allCollectedLinks);

const brokenCollection = _.merge(allBrokenLinks, allBrokenAnchors);

return _.pickBy(brokenCollection, (brokenLinks) => brokenLinks.length > 0);
}

function getBrokenLinksErrorMessage(allBrokenLinks: {
Expand Down Expand Up @@ -209,7 +244,7 @@ ${Object.entries(allBrokenLinks)
}

export async function handleBrokenLinks(params: {
allCollectedLinks: {[location: string]: {links: string[]; anchors: string[]}};
allCollectedLinks: CollectedLinks;
onBrokenLinks: ReportingSeverity;
onBrokenAnchors: ReportingSeverity;
routes: RouteConfig[];
Expand All @@ -227,17 +262,14 @@ async function handlePathBrokenLinks({
baseUrl,
outDir,
}: {
allCollectedLinks: {[location: string]: {links: string[]; anchors: string[]}};
allCollectedLinks: CollectedLinks;
onBrokenLinks: ReportingSeverity;
onBrokenAnchors: ReportingSeverity;
routes: RouteConfig[];
baseUrl: string;
outDir: string;
}): Promise<void> {
if (onBrokenLinks === 'ignore') {
return;
}
if (onBrokenAnchors === 'ignore') {
if (onBrokenLinks === 'ignore' || onBrokenAnchors === 'ignore') {
return;
}

Expand Down

0 comments on commit ca33fb8

Please sign in to comment.