Skip to content

Commit

Permalink
allow sidebars file to not exist: fallback to empty sidebars
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 31, 2020
1 parent 58a3f5a commit 66d7bbf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ describe('loadSidebars', () => {
});

test('unexisting path', () => {
/*
expect(() => loadSidebars('badpath')).toThrowErrorMatchingInlineSnapshot(
`"No sidebar file exist at path: badpath"`,
);
*/
// See https://github.com/facebook/docusaurus/issues/3366
expect(loadSidebars('badpath')).toEqual({});
});

test('undefined path', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,14 @@ export function loadSidebars(sidebarFilePath: string): Sidebars {
if (!sidebarFilePath) {
throw new Error(`sidebarFilePath not provided: ${sidebarFilePath}`);
}

// sidebars file is optional, some users use docs without sidebars!
// See https://github.com/facebook/docusaurus/issues/3366
if (!fs.existsSync(sidebarFilePath)) {
throw new Error(`No sidebar file exist at path: ${sidebarFilePath}`);
// throw new Error(`No sidebar file exist at path: ${sidebarFilePath}`);
return {};
}

// We don't want sidebars to be cached because of hot reloading.
const sidebarJson = importFresh(sidebarFilePath) as SidebarsJSON;
return normalizeSidebars(sidebarJson);
Expand Down
9 changes: 7 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
import {LoadContext} from '@docusaurus/types';
import {normalizeUrl} from '@docusaurus/utils';
import {difference} from 'lodash';
import chalk from 'chalk';

// retro-compatibility: no prefix for the default plugin id
function addPluginIdPrefix(fileOrDir: string, pluginId: string): string {
Expand Down Expand Up @@ -222,9 +223,13 @@ function checkVersionMetadataPaths({
`The docs folder does not exist for version [${versionName}]. A docs folder is expected to be found at ${docsDirPath}`,
);
}

// See https://github.com/facebook/docusaurus/issues/3366
if (!fs.existsSync(sidebarFilePath)) {
throw new Error(
`The sidebar file does not exist for version [${versionName}]. A sidebar file is expected to be found at ${sidebarFilePath}`,
console.log(
chalk.yellow(
`The sidebar file of docs version [${versionName}] does not exist. It is optional, but should rather be provided at ${sidebarFilePath}`,
),
);
}
}
Expand Down

0 comments on commit 66d7bbf

Please sign in to comment.