Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): brokenLinks should not report links that belong to an existing folder if folder/index.html exists #3308

Merged
merged 2 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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