From 69ec1e821599f6b83d64047b85463149e686e076 Mon Sep 17 00:00:00 2001 From: Pavel Grinchenko Date: Thu, 19 Mar 2020 14:23:50 +0300 Subject: [PATCH] Allow to set empty slug in sidebar.json --- src/utils/sidebar.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/utils/sidebar.js b/src/utils/sidebar.js index 67456ebf65..b899541843 100644 --- a/src/utils/sidebar.js +++ b/src/utils/sidebar.js @@ -21,14 +21,14 @@ const startCase = require('lodash.startcase') const sidebar = require('../../content/docs/sidebar.json') -const PATH_ROOT = '/doc/' +const PATH_ROOT = '/doc' const FILE_ROOT = '/docs/' const FILE_EXTENSION = '.md' function validateRawItem({ slug, source, children }) { const isSourceDisabled = source === false - if (!slug) { + if (typeof slug !== 'string') { throw Error("'slug' field is required in objects in sidebar.json") } @@ -81,7 +81,7 @@ function normalizeItem({ rawItem, parentPath, resultRef, prevRef }) { const sourcePath = FILE_ROOT + parentPath + sourceFileName return { - path: PATH_ROOT + parentPath + slug, + path: PATH_ROOT + (parentPath || slug ? '/' : '') + parentPath + slug, source: source === false ? false : sourcePath, label: label ? label : startCase(slug), tutorials: tutorials || {}, @@ -152,7 +152,7 @@ function getFirstPage() { function getItemByPath(path) { const normalizedPath = path.replace(/\/$/, '') - const isRoot = normalizedPath === PATH_ROOT.slice(0, -1) + const isRoot = normalizedPath === PATH_ROOT const item = isRoot ? normalizedSidebar[0] : findItemByField(normalizedSidebar, 'path', normalizedPath) @@ -173,10 +173,14 @@ function getPathWithSoruce(path) { } function getParentsListFromPath(path) { - let currentPath = PATH_ROOT.slice(0, -1) + let currentPath = PATH_ROOT + + if (path === PATH_ROOT) { + return [PATH_ROOT] + } return path - .replace(PATH_ROOT, '') + .replace(`${PATH_ROOT}/`, '') .split('/') .map(part => { const path = `${currentPath}/${part}`