Skip to content

Commit

Permalink
fix(v2): brokenLinks should not report links that belong to an existi…
Browse files Browse the repository at this point in the history
…ng folder if folder/index.html exists (#3308)

* move pluginIds.test.ts

* improve brokenLinks checker and add tests for filterExistingFileLinks
  • Loading branch information
slorber authored Aug 19, 2020
1 parent c97dd65 commit f4ca61b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 10 deletions.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hey
69 changes: 68 additions & 1 deletion packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {getBrokenLinksErrorMessage, getAllBrokenLinks} from '../brokenLinks';
import path from 'path';
import {
getBrokenLinksErrorMessage,
getAllBrokenLinks,
filterExistingFileLinks,
} from '../brokenLinks';
import {RouteConfig} from '@docusaurus/types';

describe('brokenLinks', () => {
Expand Down Expand Up @@ -105,4 +110,66 @@ describe('brokenLinks', () => {
expectedBrokenLinks,
);
});

test('filterExistingFileLinks', async () => {
const link1 = '/link1';
const link2 = '/docs/link2';
const link3 = '/hey/link3';

const linkToJavadoc1 = '/javadoc';
const linkToJavadoc2 = '/javadoc/';
const linkToJavadoc3 = '/javadoc/index.html';

const linkToZipFile = '/files/file.zip';
const linkToHtmlFile1 = '/files/hey.html';
const linkToHtmlFile2 = '/files/hey';

const linkToEmptyFolder1 = '/emptyFolder';
const linkToEmptyFolder2 = '/emptyFolder/';

const allCollectedLinks = {
'/page1': [
link1,
linkToHtmlFile1,
linkToJavadoc1,
linkToHtmlFile2,
linkToJavadoc3,
linkToEmptyFolder1,
],
'/page2': [
link2,
linkToEmptyFolder2,
linkToJavadoc2,
link3,
linkToJavadoc3,
linkToZipFile,
],
};

const allCollectedLinksFiltered = {
'/page1': [
link1,
// linkToHtmlFile1,
// linkToJavadoc1,
// linkToHtmlFile2,
// linkToJavadoc3,
linkToEmptyFolder1, // not filtered !
],
'/page2': [
link2,
linkToEmptyFolder2, // not filtered !
// linkToJavadoc2,
link3,
// linkToJavadoc3,
// linkToZipFile,
],
};

const result = await filterExistingFileLinks({
baseUrl: '/',
outDir: path.resolve(__dirname, '__fixtures__/brokenLinks/outDir'),
allCollectedLinks,
});
expect(result).toEqual(allCollectedLinksFiltered);
});
});
33 changes: 26 additions & 7 deletions packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import resolvePathname from 'resolve-pathname';
import fs from 'fs-extra';
import {mapValues, pickBy} from 'lodash';
import {RouteConfig, ReportingSeverity} from '@docusaurus/types';
import {removePrefix} from '@docusaurus/utils';
import {removePrefix, removeSuffix} from '@docusaurus/utils';
import {getAllFinalRoutes, reportMessage} from './utils';
import path from 'path';

function toReactRouterRoutes(routes: RouteConfig[]): RRRouteConfig[] {
// @ts-expect-error: types incompatible???
Expand Down Expand Up @@ -114,9 +115,17 @@ export function getBrokenLinksErrorMessage(
);
}

function isExistingFile(filePath: string) {
try {
return fs.statSync(filePath).isFile();
} catch (e) {
return false;
}
}

// If a file actually exist on the file system, we know the link is valid
// even if docusaurus does not know about this file, so we don't report it
async function filterExistingFileLinks({
export async function filterExistingFileLinks({
baseUrl,
outDir,
allCollectedLinks,
Expand All @@ -127,12 +136,22 @@ async function filterExistingFileLinks({
}): Promise<Record<string, string[]>> {
// not easy to make this async :'(
function linkFileExists(link: string): boolean {
const filePath = `${outDir}/${removePrefix(link, baseUrl)}`;
try {
return fs.statSync(filePath).isFile(); // only consider files
} catch (e) {
return false;
// /baseUrl/javadoc/ -> /outDir/javadoc
const baseFilePath = removeSuffix(
`${outDir}/${removePrefix(link, baseUrl)}`,
'/',
);

// -> /outDir/javadoc
// -> /outDir/javadoc.html
// -> /outDir/javadoc/index.html
const filePathsToTry: string[] = [baseFilePath];
if (!path.extname(baseFilePath)) {
filePathsToTry.push(`${baseFilePath}.html`);
filePathsToTry.push(path.join(baseFilePath, 'index.html'));
}

return filePathsToTry.some(isExistingFile);
}

return mapValues(allCollectedLinks, (links) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import {ensureUniquePluginInstanceIds} from '../../plugins/pluginIds';
import {ensureUniquePluginInstanceIds} from '../pluginIds';

import {InitPlugin} from '../../plugins/init';
import {InitPlugin} from '../init';

function createTestPlugin(name: string, id?: string): InitPlugin {
// @ts-expect-error: good enough for tests
Expand Down

0 comments on commit f4ca61b

Please sign in to comment.