fix(v2): handle non existent blog, docs, pages (#1459)

* fix(v2): handle non existent blog, docs, pages

* nits
This commit is contained in:
Endi 2019-05-15 13:55:07 +07:00 committed by GitHub
parent f84abfe2d1
commit 55d7920825
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 12 deletions

View file

@ -56,6 +56,10 @@ class DocusaurusPluginContentBlog {
const {siteConfig} = this.context; const {siteConfig} = this.context;
const blogDir = this.contentPath; const blogDir = this.contentPath;
if (!fs.existsSync(blogDir)) {
return null;
}
const {baseUrl} = siteConfig; const {baseUrl} = siteConfig;
const blogFiles = await globby(include, { const blogFiles = await globby(include, {
cwd: blogDir, cwd: blogDir,
@ -158,6 +162,10 @@ class DocusaurusPluginContentBlog {
} }
async contentLoaded({content: blogContents, actions}) { async contentLoaded({content: blogContents, actions}) {
if (!blogContents) {
return;
}
const { const {
blogListComponent, blogListComponent,
blogPostComponent, blogPostComponent,

View file

@ -12,18 +12,18 @@ import loadSidebars from '../sidebars';
describe('loadSidebars', () => { describe('loadSidebars', () => {
test('normal site with sidebars', async () => { test('normal site with sidebars', async () => {
const sidebar = require(path.join( const sidebarPath = path.join(
__dirname, __dirname,
'__fixtures__', '__fixtures__',
'website', 'website',
'sidebars.json', 'sidebars.json',
)); );
const result = loadSidebars({sidebar}); const result = loadSidebars(sidebarPath);
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
test('site without sidebars', () => { test('site without sidebars', () => {
const result = loadSidebars({sidebar: {}}); const result = loadSidebars(null);
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
}); });

View file

@ -6,7 +6,7 @@
*/ */
const globby = require('globby'); const globby = require('globby');
const importFresh = require('import-fresh'); const fs = require('fs');
const path = require('path'); const path = require('path');
const {idx, normalizeUrl, docuHash} = require('@docusaurus/utils'); const {idx, normalizeUrl, docuHash} = require('@docusaurus/utils');
@ -48,14 +48,16 @@ class DocusaurusPluginContentDocs {
// Fetches blog contents and returns metadata for the contents. // Fetches blog contents and returns metadata for the contents.
async loadContent() { async loadContent() {
const {include, routeBasePath, sidebarPath} = this.options; const {include, routeBasePath, sidebarPath} = this.options;
const {siteDir, siteConfig} = this.context; const {siteConfig} = this.context;
const docsDir = this.contentPath; const docsDir = this.contentPath;
// We don't want sidebars to be cached because of hotreloading. if (!fs.existsSync(docsDir)) {
const sidebar = importFresh(sidebarPath); return null;
const docsSidebars = loadSidebars({siteDir, sidebar}); }
// @tested - build the docs ordering such as next, previous, category and sidebar const docsSidebars = loadSidebars(sidebarPath);
// Build the docs ordering such as next, previous, category and sidebar
const order = createOrder(docsSidebars); const order = createOrder(docsSidebars);
// Prepare metadata container. // Prepare metadata container.
@ -111,6 +113,9 @@ class DocusaurusPluginContentDocs {
} }
async contentLoaded({content, actions}) { async contentLoaded({content, actions}) {
if (!content) {
return;
}
const {docLayoutComponent, docItemComponent, routeBasePath} = this.options; const {docLayoutComponent, docItemComponent, routeBasePath} = this.options;
const {addRoute, createData} = actions; const {addRoute, createData} = actions;

View file

@ -5,6 +5,9 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
const fs = require('fs');
const importFresh = require('import-fresh');
/** /**
* Check that item contains only allowed keys * Check that item contains only allowed keys
* *
@ -109,7 +112,11 @@ function normalizeSidebar(sidebars) {
}, {}); }, {});
} }
module.exports = function loadSidebars({sidebar}) { module.exports = function loadSidebars(sidebarPath) {
const allSidebars = sidebar; // We don't want sidebars to be cached because of hotreloading.
let allSidebars = {};
if (sidebarPath && fs.existsSync(sidebarPath)) {
allSidebars = importFresh(sidebarPath);
}
return normalizeSidebar(allSidebars); return normalizeSidebar(allSidebars);
}; };

View file

@ -7,6 +7,7 @@
const globby = require('globby'); const globby = require('globby');
const path = require('path'); const path = require('path');
const fs = require('fs');
const {encodePath, fileToPath, docuHash} = require('@docusaurus/utils'); const {encodePath, fileToPath, docuHash} = require('@docusaurus/utils');
const DEFAULT_OPTIONS = { const DEFAULT_OPTIONS = {
@ -39,6 +40,10 @@ class DocusaurusPluginContentPages {
const {siteConfig} = this.context; const {siteConfig} = this.context;
const pagesDir = this.contentPath; const pagesDir = this.contentPath;
if (!fs.existsSync(pagesDir)) {
return null;
}
const {baseUrl} = siteConfig; const {baseUrl} = siteConfig;
const pagesFiles = await globby(include, { const pagesFiles = await globby(include, {
cwd: pagesDir, cwd: pagesDir,
@ -64,6 +69,10 @@ class DocusaurusPluginContentPages {
} }
async contentLoaded({content, actions}) { async contentLoaded({content, actions}) {
if (!content) {
return;
}
const {addRoute, createData} = actions; const {addRoute, createData} = actions;
await Promise.all( await Promise.all(