Skip to content

Commit

Permalink
fix(core): remove hash/query when filtering existing files for broken…
Browse files Browse the repository at this point in the history
… link check (#6812)

* fix(core): remove hash/query when filtering existing files for broken link check

* fix tests
  • Loading branch information
Josh-Cena committed Mar 2, 2022
1 parent ea8590f commit 954e4d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ describe('brokenLinks', () => {
const linkToJavadoc1 = '/javadoc';
const linkToJavadoc2 = '/javadoc/';
const linkToJavadoc3 = '/javadoc/index.html';
const linkToJavadoc4 = '/javadoc/index.html#foo';

const linkToZipFile = '/files/file.zip';
const linkToHtmlFile1 = '/files/hey.html';
Expand All @@ -168,6 +169,7 @@ describe('brokenLinks', () => {
linkToJavadoc1,
linkToHtmlFile2,
linkToJavadoc3,
linkToJavadoc4,
linkToEmptyFolder1,
],
'/page2': [
Expand Down
47 changes: 27 additions & 20 deletions packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import {getAllFinalRoutes} from './utils';
import path from 'path';
import combinePromises from 'combine-promises';
import logger from '@docusaurus/logger';

function toReactRouterRoutes(routes: RouteConfig[]): RRRouteConfig[] {
// @ts-expect-error: types incompatible???
Expand Down Expand Up @@ -111,9 +112,11 @@ export function getBrokenLinksErrorMessage(
pagePath: string,
brokenLinks: BrokenLink[],
): string {
return `\n- On source page path = ${pagePath}:\n -> linking to ${brokenLinks
.map(brokenLinkMessage)
.join('\n -> linking to ')}`;
return `
- On source page path = ${pagePath}:
-> linking to ${brokenLinks
.map(brokenLinkMessage)
.join('\n -> linking to ')}`;
}

/**
Expand Down Expand Up @@ -141,22 +144,27 @@ export function getBrokenLinksErrorMessage(
return '';
}

return `\n\nIt looks like some of the broken links we found appear in many pages of your site.\nMaybe those broken links appear on all pages through your site layout?\nWe recommend that you check your theme configuration for such links (particularly, theme navbar and footer).\nFrequent broken links are linking to:\n- ${frequentLinks.join(
`\n- `,
)}\n`;
return logger.interpolate`
It looks like some of the broken links we found appear in many pages of your site.
Maybe those broken links appear on all pages through your site layout?
We recommend that you check your theme configuration for such links (particularly, theme navbar and footer).
Frequent broken links are linking to:${frequentLinks}
`;
}

return (
`Docusaurus found broken links!\n\nPlease check the pages of your site in the list below, and make sure you don't reference any path that does not exist.\nNote: it's possible to ignore broken links with the 'onBrokenLinks' Docusaurus configuration, and let the build pass.${getLayoutBrokenLinksHelpMessage()}` +
`\n\nExhaustive list of all broken links found:\n${Object.entries(
allBrokenLinks,
)
.map(([pagePath, brokenLinks]) =>
pageBrokenLinksMessage(pagePath, brokenLinks),
)
.join('\n')}
`
);
return `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.${getLayoutBrokenLinksHelpMessage()}
Exhaustive list of all broken links found:
${Object.entries(allBrokenLinks)
.map(([pagePath, brokenLinks]) =>
pageBrokenLinksMessage(pagePath, brokenLinks),
)
.join('\n')}
`;
}

async function isExistingFile(filePath: string) {
Expand All @@ -180,9 +188,8 @@ export async function filterExistingFileLinks({
}): Promise<Record<string, string[]>> {
async function linkFileExists(link: string) {
// /baseUrl/javadoc/ -> /outDir/javadoc
const baseFilePath = removeSuffix(
`${outDir}/${removePrefix(link, baseUrl)}`,
'/',
const baseFilePath = onlyPathname(
removeSuffix(`${outDir}/${removePrefix(link, baseUrl)}`, '/'),
);

// -> /outDir/javadoc
Expand Down

0 comments on commit 954e4d4

Please sign in to comment.