feat(v2): new docs edit options: editCurrentVersion + editLocalizedDocs (#3949)

* editCurrentVersion initial poc

* ensure edit url allows to edit localized docs

* Add editLocalizedDocs option

* keep editing current version in dev (more convenient)
This commit is contained in:
Sébastien Lorber 2020-12-28 10:25:47 +01:00 committed by GitHub
parent 2791ccc4cf
commit b5c46bd1d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 394 additions and 59 deletions

View file

@ -22,7 +22,7 @@ import {
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
import {LoadContext} from '@docusaurus/types';
import {getPluginI18nPath, normalizeUrl} from '@docusaurus/utils';
import {getPluginI18nPath, normalizeUrl, posixPath} from '@docusaurus/utils';
import {difference} from 'lodash';
import chalk from 'chalk';
@ -131,6 +131,30 @@ function readVersionNames(
return versions;
}
function getDocsDirPathLocalized({
siteDir,
locale,
pluginId,
versionName,
}: {
siteDir: string;
locale: string;
pluginId: string;
versionName: string;
}) {
return getPluginI18nPath({
siteDir,
locale,
pluginName: 'docusaurus-plugin-content-docs',
pluginId,
subPaths: [
versionName === CURRENT_VERSION_NAME
? CURRENT_VERSION_NAME
: `version-${versionName}`,
],
});
}
function getVersionMetadataPaths({
versionName,
context,
@ -152,16 +176,11 @@ function getVersionMetadataPaths({
`version-${versionName}`,
);
const docsDirPathLocalized = getPluginI18nPath({
const docsDirPathLocalized = getDocsDirPathLocalized({
siteDir: context.siteDir,
locale: context.i18n.currentLocale,
pluginName: 'docusaurus-plugin-content-docs',
pluginId: options.id,
subPaths: [
versionName === CURRENT_VERSION_NAME
? CURRENT_VERSION_NAME
: `version-${versionName}`,
],
versionName,
});
const sidebarFilePath = isCurrentVersion
@ -174,6 +193,54 @@ function getVersionMetadataPaths({
return {docsDirPath, docsDirPathLocalized, sidebarFilePath};
}
function getVersionEditUrls({
docsDirPath,
docsDirPathLocalized,
context: {siteDir, i18n},
options: {id, path: currentVersionPath, editUrl, editCurrentVersion},
}: {
docsDirPath: string;
docsDirPathLocalized: string;
context: Pick<LoadContext, 'siteDir' | 'i18n'>;
options: Pick<
PluginOptions,
'id' | 'path' | 'editUrl' | 'editCurrentVersion'
>;
}): {versionEditUrl: string; versionEditUrlLocalized: string} | undefined {
if (!editUrl) {
return undefined;
}
const editDirPath = editCurrentVersion ? currentVersionPath : docsDirPath;
const editDirPathLocalized = editCurrentVersion
? getDocsDirPathLocalized({
siteDir,
locale: i18n.currentLocale,
versionName: CURRENT_VERSION_NAME,
pluginId: id,
})
: docsDirPathLocalized;
const versionPathSegment = posixPath(
path.relative(siteDir, path.resolve(siteDir, editDirPath)),
);
const versionPathSegmentLocalized = posixPath(
path.relative(siteDir, path.resolve(siteDir, editDirPathLocalized)),
);
const versionEditUrl = normalizeUrl([editUrl, versionPathSegment]);
const versionEditUrlLocalized = normalizeUrl([
editUrl,
versionPathSegmentLocalized,
]);
return {
versionEditUrl,
versionEditUrlLocalized,
};
}
function createVersionMetadata({
versionName,
isLast,
@ -185,7 +252,13 @@ function createVersionMetadata({
context: Pick<LoadContext, 'siteDir' | 'baseUrl' | 'i18n'>;
options: Pick<
PluginOptions,
'id' | 'path' | 'sidebarPath' | 'routeBasePath' | 'versions'
| 'id'
| 'path'
| 'sidebarPath'
| 'routeBasePath'
| 'versions'
| 'editUrl'
| 'editCurrentVersion'
>;
}): VersionMetadata {
const {
@ -218,6 +291,13 @@ function createVersionMetadata({
versionPathPart,
]);
const versionEditUrls = getVersionEditUrls({
docsDirPath,
docsDirPathLocalized,
context,
options,
});
// Because /docs/:route` should always be after `/docs/versionName/:route`.
const routePriority = versionPathPart === '' ? -1 : undefined;
@ -225,6 +305,8 @@ function createVersionMetadata({
versionName,
versionLabel,
versionPath,
versionEditUrl: versionEditUrls?.versionEditUrl,
versionEditUrlLocalized: versionEditUrls?.versionEditUrlLocalized,
isLast,
routePriority,
sidebarFilePath,
@ -354,6 +436,8 @@ export function readVersionsMetadata({
| 'lastVersion'
| 'versions'
| 'onlyIncludeVersions'
| 'editUrl'
| 'editCurrentVersion'
>;
}): VersionMetadata[] {
const versionNamesUnfiltered = readVersionNames(context.siteDir, options);