diff --git a/packages/docusaurus/src/server/__tests__/brokenLinks.test.ts b/packages/docusaurus/src/server/__tests__/brokenLinks.test.ts index a8253d3f41..1c15558591 100644 --- a/packages/docusaurus/src/server/__tests__/brokenLinks.test.ts +++ b/packages/docusaurus/src/server/__tests__/brokenLinks.test.ts @@ -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'; @@ -168,6 +169,7 @@ describe('brokenLinks', () => { linkToJavadoc1, linkToHtmlFile2, linkToJavadoc3, + linkToJavadoc4, linkToEmptyFolder1, ], '/page2': [ diff --git a/packages/docusaurus/src/server/brokenLinks.ts b/packages/docusaurus/src/server/brokenLinks.ts index 5bf04cb5c8..4f3f6b4f12 100644 --- a/packages/docusaurus/src/server/brokenLinks.ts +++ b/packages/docusaurus/src/server/brokenLinks.ts @@ -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??? @@ -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 ')}`; } /** @@ -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) { @@ -180,9 +188,8 @@ export async function filterExistingFileLinks({ }): Promise> { 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