mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-03 08:49:51 +02:00
refactor(v2): load sidebars from docs plugin (#1328)
* refactor(v2): load sidebars from docs plugin * fix(v2): update tests * fix(v2): change to sidebarPath
This commit is contained in:
parent
8dd6bc1082
commit
029aa636a8
15 changed files with 71 additions and 42 deletions
|
@ -11,11 +11,13 @@ import loadSetup from '../../docusaurus/test/loadSetup';
|
|||
import DocusaurusPluginContentDocs from '../index';
|
||||
|
||||
describe('loadDocs', () => {
|
||||
test.only('simple website', async () => {
|
||||
test('simple website', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('simple');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
|
@ -60,9 +62,11 @@ describe('loadDocs', () => {
|
|||
const {env, siteDir, siteConfig, versionedDir} = await loadSetup(
|
||||
'versioned',
|
||||
);
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
|
@ -111,9 +115,11 @@ describe('loadDocs', () => {
|
|||
translatedDir,
|
||||
versionedDir,
|
||||
} = await loadSetup('transversioned');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
|
@ -175,9 +181,11 @@ describe('loadDocs', () => {
|
|||
const {env, siteDir, siteConfig, translatedDir} = await loadSetup(
|
||||
'translated',
|
||||
);
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
|
@ -225,15 +233,19 @@ describe('loadDocs', () => {
|
|||
const {env, siteDir, siteConfig, versionedDir} = await loadSetup(
|
||||
'versioned',
|
||||
);
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
cliOptions: {
|
||||
skipNextRelease: true,
|
||||
},
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
cliOptions: {skipNextRelease: true},
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata} = await plugin.loadContent();
|
||||
|
|
|
@ -9,24 +9,29 @@ import path from 'path';
|
|||
import loadSidebars from '../src/sidebars';
|
||||
import loadSetup from '../../docusaurus/test/loadSetup';
|
||||
|
||||
/* eslint-disable global-require, import/no-dynamic-require */
|
||||
|
||||
describe('loadSidebars', () => {
|
||||
const fixtures = path.join(__dirname, '..', '__fixtures__');
|
||||
|
||||
test('normal site with sidebars', async () => {
|
||||
const {env, siteDir} = await loadSetup('simple');
|
||||
const result = loadSidebars({siteDir, env});
|
||||
const sidebar = require(path.join(siteDir, 'sidebars.json'));
|
||||
const result = loadSidebars({siteDir, env, sidebar});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('site without sidebars', () => {
|
||||
const env = {};
|
||||
const siteDir = path.join(fixtures, 'bad-site');
|
||||
const result = loadSidebars({siteDir, env});
|
||||
const result = loadSidebars({siteDir, env, sidebar: {}});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('site with sidebars & versioned sidebars', async () => {
|
||||
const {env, siteDir} = await loadSetup('versioned');
|
||||
const result = loadSidebars({siteDir, env});
|
||||
const sidebar = require(path.join(siteDir, 'sidebars.json'));
|
||||
const result = loadSidebars({siteDir, env, sidebar});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
|
@ -39,7 +44,7 @@ describe('loadSidebars', () => {
|
|||
};
|
||||
const {siteDir} = await loadSetup('versioned');
|
||||
expect(() => {
|
||||
loadSidebars({siteDir, env});
|
||||
loadSidebars({siteDir, env, sidebar: {}});
|
||||
}).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Failed to load versioned_sidebars/version-2.0.0-sidebars.json. It does not exist."`,
|
||||
);
|
||||
|
|
|
@ -5,9 +5,11 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const globby = require('globby');
|
||||
const importFresh = require('import-fresh');
|
||||
const path = require('path');
|
||||
const {getSubFolder, idx, normalizeUrl} = require('@docusaurus/utils');
|
||||
|
||||
const createOrder = require('./src/order');
|
||||
const loadSidebars = require('./src/sidebars');
|
||||
const processMetadata = require('./src/metadata');
|
||||
|
@ -18,8 +20,8 @@ const DEFAULT_OPTIONS = {
|
|||
path: 'docs', // Path to data on filesystem, relative to site dir.
|
||||
routeBasePath: 'docs', // URL Route.
|
||||
include: ['**/*.md', '**/*.mdx'], // Extensions to include.
|
||||
// TODO: Read from props rather than hardcoded sidebar.json.
|
||||
sidebar: [], // Sidebar configuration for showing a list of documentation pages.
|
||||
// TODO: Change format to array.
|
||||
sidebarPath: '', // Path to sidebar configuration for showing a list of markdown pages.
|
||||
// TODO: Settle themeing.
|
||||
docLayoutComponent: '@theme/Doc',
|
||||
docItemComponent: '@theme/DocBody',
|
||||
|
@ -37,18 +39,19 @@ class DocusaurusPluginContentDocs {
|
|||
}
|
||||
|
||||
getPathsToWatch() {
|
||||
return [this.contentPath];
|
||||
return [this.contentPath, this.options.sidebarPath];
|
||||
}
|
||||
|
||||
// Fetches blog contents and returns metadata for the contents.
|
||||
async loadContent() {
|
||||
const {include, routeBasePath} = this.options;
|
||||
const {include, routeBasePath, sidebarPath} = this.options;
|
||||
const {siteDir, env, siteConfig, cliOptions = {}} = this.context;
|
||||
const {skipNextRelease} = cliOptions;
|
||||
const docsDir = this.contentPath;
|
||||
|
||||
// @tested - load all sidebars including versioned sidebars
|
||||
const docsSidebars = loadSidebars({siteDir, env});
|
||||
// We don't want sidebars to be cached because of hotreloading.
|
||||
const sidebar = importFresh(sidebarPath);
|
||||
const docsSidebars = loadSidebars({siteDir, env, sidebar});
|
||||
|
||||
// @tested - build the docs ordering such as next, previous, category and sidebar
|
||||
const order = createOrder(docsSidebars);
|
||||
|
|
|
@ -8,7 +8,8 @@
|
|||
"@babel/polyfill": "^7.4.0",
|
||||
"@docusaurus/utils": "^1.0.0",
|
||||
"fs-extra": "^7.0.1",
|
||||
"globby": "^9.1.0"
|
||||
"globby": "^9.1.0",
|
||||
"import-fresh": "^3.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@docusaurus/core": "^2.0.0"
|
||||
|
|
|
@ -113,19 +113,10 @@ function normalizeSidebar(sidebars) {
|
|||
}, {});
|
||||
}
|
||||
|
||||
module.exports = function loadSidebars({siteDir, env}, deleteCache = true) {
|
||||
let allSidebars = {};
|
||||
module.exports = function loadSidebars({siteDir, env, sidebar}) {
|
||||
const allSidebars = sidebar;
|
||||
|
||||
// current sidebars
|
||||
const sidebarsJSONFile = path.join(siteDir, 'sidebars.json');
|
||||
if (deleteCache) {
|
||||
delete require.cache[sidebarsJSONFile];
|
||||
}
|
||||
if (fs.existsSync(sidebarsJSONFile)) {
|
||||
allSidebars = require(sidebarsJSONFile); // eslint-disable-line
|
||||
}
|
||||
|
||||
// versioned sidebars
|
||||
// Versioned sidebars.
|
||||
if (idx(env, ['versioning', 'enabled'])) {
|
||||
const versions = idx(env, ['versioning', 'versions']);
|
||||
if (Array.isArray(versions)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue