mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
fix(v2): allow relative sidebar path resolution in docs:version command (#4861)
* fix(v2): allow relative sidebar path resolution in docs:version command * factorize sidebarPath option resolution logic + dogfood Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
65cf8dacee
commit
35bdde3409
4 changed files with 26 additions and 6 deletions
|
@ -17,7 +17,7 @@ import {
|
||||||
UnprocessedSidebarItem,
|
UnprocessedSidebarItem,
|
||||||
UnprocessedSidebars,
|
UnprocessedSidebars,
|
||||||
} from './types';
|
} from './types';
|
||||||
import {loadSidebars} from './sidebars';
|
import {loadSidebars, resolveSidebarPathOption} from './sidebars';
|
||||||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
||||||
|
|
||||||
function createVersionedSidebarFile({
|
function createVersionedSidebarFile({
|
||||||
|
@ -145,6 +145,7 @@ export function cliDocsVersionCommand(
|
||||||
|
|
||||||
// Copy docs files.
|
// Copy docs files.
|
||||||
const docsDir = path.join(siteDir, docsPath);
|
const docsDir = path.join(siteDir, docsPath);
|
||||||
|
|
||||||
if (fs.existsSync(docsDir) && fs.readdirSync(docsDir).length > 0) {
|
if (fs.existsSync(docsDir) && fs.readdirSync(docsDir).length > 0) {
|
||||||
const versionedDir = getVersionedDocsDirPath(siteDir, pluginId);
|
const versionedDir = getVersionedDocsDirPath(siteDir, pluginId);
|
||||||
const newVersionDir = path.join(versionedDir, `version-${version}`);
|
const newVersionDir = path.join(versionedDir, `version-${version}`);
|
||||||
|
@ -153,7 +154,12 @@ export function cliDocsVersionCommand(
|
||||||
throw new Error(`${pluginIdLogPrefix}There is no docs to version !`);
|
throw new Error(`${pluginIdLogPrefix}There is no docs to version !`);
|
||||||
}
|
}
|
||||||
|
|
||||||
createVersionedSidebarFile({siteDir, pluginId, version, sidebarPath});
|
createVersionedSidebarFile({
|
||||||
|
siteDir,
|
||||||
|
pluginId,
|
||||||
|
version,
|
||||||
|
sidebarPath: resolveSidebarPathOption(siteDir, sidebarPath),
|
||||||
|
});
|
||||||
|
|
||||||
// Update versions.json file.
|
// Update versions.json file.
|
||||||
versions.unshift(version);
|
versions.unshift(version);
|
||||||
|
|
|
@ -25,11 +25,13 @@ import {
|
||||||
SidebarItemsGeneratorVersion,
|
SidebarItemsGeneratorVersion,
|
||||||
NumberPrefixParser,
|
NumberPrefixParser,
|
||||||
SidebarItemsGeneratorOption,
|
SidebarItemsGeneratorOption,
|
||||||
|
PluginOptions,
|
||||||
} from './types';
|
} from './types';
|
||||||
import {mapValues, flatten, flatMap, difference, pick, memoize} from 'lodash';
|
import {mapValues, flatten, flatMap, difference, pick, memoize} from 'lodash';
|
||||||
import {getElementsAround} from '@docusaurus/utils';
|
import {getElementsAround} from '@docusaurus/utils';
|
||||||
import combinePromises from 'combine-promises';
|
import combinePromises from 'combine-promises';
|
||||||
import {DefaultSidebarItemsGenerator} from './sidebarItemsGenerator';
|
import {DefaultSidebarItemsGenerator} from './sidebarItemsGenerator';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
type SidebarItemCategoryJSON = SidebarItemBase & {
|
type SidebarItemCategoryJSON = SidebarItemBase & {
|
||||||
type: 'category';
|
type: 'category';
|
||||||
|
@ -256,7 +258,19 @@ export const DefaultSidebars: UnprocessedSidebars = {
|
||||||
|
|
||||||
export const DisabledSidebars: UnprocessedSidebars = {};
|
export const DisabledSidebars: UnprocessedSidebars = {};
|
||||||
|
|
||||||
|
// If a path is provided, make it absolute
|
||||||
|
// use this before loadSidebars()
|
||||||
|
export function resolveSidebarPathOption(
|
||||||
|
siteDir: string,
|
||||||
|
sidebarPathOption: PluginOptions['sidebarPath'],
|
||||||
|
): PluginOptions['sidebarPath'] {
|
||||||
|
return sidebarPathOption
|
||||||
|
? path.resolve(siteDir, sidebarPathOption)
|
||||||
|
: sidebarPathOption;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO refactor: make async
|
// TODO refactor: make async
|
||||||
|
// Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
|
||||||
export function loadSidebars(
|
export function loadSidebars(
|
||||||
sidebarFilePath: string | false | undefined,
|
sidebarFilePath: string | false | undefined,
|
||||||
): UnprocessedSidebars {
|
): UnprocessedSidebars {
|
||||||
|
@ -279,6 +293,7 @@ export function loadSidebars(
|
||||||
|
|
||||||
// We don't want sidebars to be cached because of hot reloading.
|
// We don't want sidebars to be cached because of hot reloading.
|
||||||
const sidebarJson = importFresh(sidebarFilePath) as SidebarsJSON;
|
const sidebarJson = importFresh(sidebarFilePath) as SidebarsJSON;
|
||||||
|
|
||||||
return normalizeSidebars(sidebarJson);
|
return normalizeSidebars(sidebarJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
||||||
import {LoadContext} from '@docusaurus/types';
|
import {LoadContext} from '@docusaurus/types';
|
||||||
import {getPluginI18nPath, normalizeUrl, posixPath} from '@docusaurus/utils';
|
import {getPluginI18nPath, normalizeUrl, posixPath} from '@docusaurus/utils';
|
||||||
import {difference} from 'lodash';
|
import {difference} from 'lodash';
|
||||||
|
import {resolveSidebarPathOption} from './sidebars';
|
||||||
|
|
||||||
// retro-compatibility: no prefix for the default plugin id
|
// retro-compatibility: no prefix for the default plugin id
|
||||||
function addPluginIdPrefix(fileOrDir: string, pluginId: string): string {
|
function addPluginIdPrefix(fileOrDir: string, pluginId: string): string {
|
||||||
|
@ -184,9 +185,7 @@ function getVersionMetadataPaths({
|
||||||
|
|
||||||
function getSidebarFilePath() {
|
function getSidebarFilePath() {
|
||||||
if (isCurrentVersion) {
|
if (isCurrentVersion) {
|
||||||
return options.sidebarPath
|
return resolveSidebarPathOption(context.siteDir, options.sidebarPath);
|
||||||
? path.resolve(context.siteDir, options.sidebarPath)
|
|
||||||
: options.sidebarPath;
|
|
||||||
} else {
|
} else {
|
||||||
return path.join(
|
return path.join(
|
||||||
getVersionedSidebarsDirPath(context.siteDir, options.id),
|
getVersionedSidebarsDirPath(context.siteDir, options.id),
|
||||||
|
|
|
@ -235,7 +235,7 @@ const isVersioningDisabled = !!process.env.DISABLE_VERSIONING || isI18nStaging;
|
||||||
docs: {
|
docs: {
|
||||||
// routeBasePath: '/',
|
// routeBasePath: '/',
|
||||||
path: 'docs',
|
path: 'docs',
|
||||||
sidebarPath: require.resolve('./sidebars.js'),
|
sidebarPath: 'sidebars.js',
|
||||||
editUrl: ({locale, docPath}) => {
|
editUrl: ({locale, docPath}) => {
|
||||||
if (locale !== 'en') {
|
if (locale !== 'en') {
|
||||||
return `https://crowdin.com/project/docusaurus-v2/${locale}`;
|
return `https://crowdin.com/project/docusaurus-v2/${locale}`;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue