Skip to content

Commit

Permalink
fix(v2): handle non existent blog, docs, pages (#1459)
Browse files Browse the repository at this point in the history
* fix(v2): handle non existent blog, docs, pages

* nits
  • Loading branch information
endiliey authored May 15, 2019
1 parent f84abfe commit 55d7920
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
8 changes: 8 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class DocusaurusPluginContentBlog {
const {siteConfig} = this.context;
const blogDir = this.contentPath;

if (!fs.existsSync(blogDir)) {
return null;
}

const {baseUrl} = siteConfig;
const blogFiles = await globby(include, {
cwd: blogDir,
Expand Down Expand Up @@ -158,6 +162,10 @@ class DocusaurusPluginContentBlog {
}

async contentLoaded({content: blogContents, actions}) {
if (!blogContents) {
return;
}

const {
blogListComponent,
blogPostComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import loadSidebars from '../sidebars';

describe('loadSidebars', () => {
test('normal site with sidebars', async () => {
const sidebar = require(path.join(
const sidebarPath = path.join(
__dirname,
'__fixtures__',
'website',
'sidebars.json',
));
const result = loadSidebars({sidebar});
);
const result = loadSidebars(sidebarPath);
expect(result).toMatchSnapshot();
});

test('site without sidebars', () => {
const result = loadSidebars({sidebar: {}});
const result = loadSidebars(null);
expect(result).toMatchSnapshot();
});
});
17 changes: 11 additions & 6 deletions packages/docusaurus-plugin-content-docs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

const globby = require('globby');
const importFresh = require('import-fresh');
const fs = require('fs');
const path = require('path');
const {idx, normalizeUrl, docuHash} = require('@docusaurus/utils');

Expand Down Expand Up @@ -48,14 +48,16 @@ class DocusaurusPluginContentDocs {
// Fetches blog contents and returns metadata for the contents.
async loadContent() {
const {include, routeBasePath, sidebarPath} = this.options;
const {siteDir, siteConfig} = this.context;
const {siteConfig} = this.context;
const docsDir = this.contentPath;

// We don't want sidebars to be cached because of hotreloading.
const sidebar = importFresh(sidebarPath);
const docsSidebars = loadSidebars({siteDir, sidebar});
if (!fs.existsSync(docsDir)) {
return null;
}

// @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);

// Prepare metadata container.
Expand Down Expand Up @@ -111,6 +113,9 @@ class DocusaurusPluginContentDocs {
}

async contentLoaded({content, actions}) {
if (!content) {
return;
}
const {docLayoutComponent, docItemComponent, routeBasePath} = this.options;
const {addRoute, createData} = actions;

Expand Down
11 changes: 9 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* 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
*
Expand Down Expand Up @@ -109,7 +112,11 @@ function normalizeSidebar(sidebars) {
}, {});
}

module.exports = function loadSidebars({sidebar}) {
const allSidebars = sidebar;
module.exports = function loadSidebars(sidebarPath) {
// We don't want sidebars to be cached because of hotreloading.
let allSidebars = {};
if (sidebarPath && fs.existsSync(sidebarPath)) {
allSidebars = importFresh(sidebarPath);
}
return normalizeSidebar(allSidebars);
};
9 changes: 9 additions & 0 deletions packages/docusaurus-plugin-content-pages/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const globby = require('globby');
const path = require('path');
const fs = require('fs');
const {encodePath, fileToPath, docuHash} = require('@docusaurus/utils');

const DEFAULT_OPTIONS = {
Expand Down Expand Up @@ -39,6 +40,10 @@ class DocusaurusPluginContentPages {
const {siteConfig} = this.context;
const pagesDir = this.contentPath;

if (!fs.existsSync(pagesDir)) {
return null;
}

const {baseUrl} = siteConfig;
const pagesFiles = await globby(include, {
cwd: pagesDir,
Expand All @@ -64,6 +69,10 @@ class DocusaurusPluginContentPages {
}

async contentLoaded({content, actions}) {
if (!content) {
return;
}

const {addRoute, createData} = actions;

await Promise.all(
Expand Down

0 comments on commit 55d7920

Please sign in to comment.