mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-01 19:27:48 +02:00
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:
parent
2791ccc4cf
commit
b5c46bd1d9
13 changed files with 394 additions and 59 deletions
|
@ -11,4 +11,8 @@ module.exports = {
|
||||||
url: 'https://your-docusaurus-test-site.com',
|
url: 'https://your-docusaurus-test-site.com',
|
||||||
baseUrl: '/',
|
baseUrl: '/',
|
||||||
favicon: 'img/favicon.ico',
|
favicon: 'img/favicon.ico',
|
||||||
|
i18n: {
|
||||||
|
defaultLocale: 'en',
|
||||||
|
locales: ['en', 'fr'],
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Hello `1.0.0` ! (translated)
|
Hello `1.0.0` ! (translated en)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Hello `1.0.0` ! (translated fr)
|
|
@ -936,7 +936,7 @@ Object {
|
||||||
\\"id\\": \\"version-1.0.0/hello\\",
|
\\"id\\": \\"version-1.0.0/hello\\",
|
||||||
\\"isDocsHomePage\\": true,
|
\\"isDocsHomePage\\": true,
|
||||||
\\"title\\": \\"hello\\",
|
\\"title\\": \\"hello\\",
|
||||||
\\"description\\": \\"Hello 1.0.0 ! (translated)\\",
|
\\"description\\": \\"Hello 1.0.0 ! (translated en)\\",
|
||||||
\\"source\\": \\"@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md\\",
|
\\"source\\": \\"@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md\\",
|
||||||
\\"slug\\": \\"/\\",
|
\\"slug\\": \\"/\\",
|
||||||
\\"permalink\\": \\"/docs/1.0.0/\\",
|
\\"permalink\\": \\"/docs/1.0.0/\\",
|
||||||
|
|
|
@ -14,6 +14,7 @@ import {
|
||||||
DocMetadataBase,
|
DocMetadataBase,
|
||||||
MetadataOptions,
|
MetadataOptions,
|
||||||
VersionMetadata,
|
VersionMetadata,
|
||||||
|
PluginOptions,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
import {LoadContext} from '@docusaurus/types';
|
import {LoadContext} from '@docusaurus/types';
|
||||||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
||||||
|
@ -42,6 +43,7 @@ ${markdown}
|
||||||
source,
|
source,
|
||||||
content,
|
content,
|
||||||
lastUpdate: {},
|
lastUpdate: {},
|
||||||
|
docsDirPath: 'docs',
|
||||||
filePath: source,
|
filePath: source,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -111,19 +113,19 @@ function createTestUtils({
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('simple site', () => {
|
describe('simple site', () => {
|
||||||
async function loadSite() {
|
async function loadSite(
|
||||||
|
loadSiteOptions: {options: Partial<PluginOptions>} = {options: {}},
|
||||||
|
) {
|
||||||
const siteDir = path.join(fixtureDir, 'simple-site');
|
const siteDir = path.join(fixtureDir, 'simple-site');
|
||||||
const context = await loadContext(siteDir);
|
const context = await loadContext(siteDir);
|
||||||
const options = {
|
const options = {
|
||||||
id: DEFAULT_PLUGIN_ID,
|
id: DEFAULT_PLUGIN_ID,
|
||||||
...DEFAULT_OPTIONS,
|
...DEFAULT_OPTIONS,
|
||||||
|
...loadSiteOptions.options,
|
||||||
};
|
};
|
||||||
const versionsMetadata = readVersionsMetadata({
|
const versionsMetadata = readVersionsMetadata({
|
||||||
context,
|
context,
|
||||||
options: {
|
options,
|
||||||
id: DEFAULT_PLUGIN_ID,
|
|
||||||
...DEFAULT_OPTIONS,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
expect(versionsMetadata.length).toEqual(1);
|
expect(versionsMetadata.length).toEqual(1);
|
||||||
const [currentVersion] = versionsMetadata;
|
const [currentVersion] = versionsMetadata;
|
||||||
|
@ -191,12 +193,14 @@ describe('simple site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('homePageId doc', async () => {
|
test('homePageId doc', async () => {
|
||||||
const {siteDir, context, options, currentVersion} = await loadSite();
|
const {siteDir, context, options, currentVersion} = await loadSite({
|
||||||
|
options: {homePageId: 'hello'},
|
||||||
|
});
|
||||||
|
|
||||||
const testUtilsLocal = createTestUtils({
|
const testUtilsLocal = createTestUtils({
|
||||||
siteDir,
|
siteDir,
|
||||||
context,
|
context,
|
||||||
options: {...options, homePageId: 'hello'},
|
options,
|
||||||
versionMetadata: currentVersion,
|
versionMetadata: currentVersion,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -213,12 +217,14 @@ describe('simple site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('homePageId doc nested', async () => {
|
test('homePageId doc nested', async () => {
|
||||||
const {siteDir, context, options, currentVersion} = await loadSite();
|
const {siteDir, context, options, currentVersion} = await loadSite({
|
||||||
|
options: {homePageId: 'foo/bar'},
|
||||||
|
});
|
||||||
|
|
||||||
const testUtilsLocal = createTestUtils({
|
const testUtilsLocal = createTestUtils({
|
||||||
siteDir,
|
siteDir,
|
||||||
context,
|
context,
|
||||||
options: {...options, homePageId: 'foo/bar'},
|
options,
|
||||||
versionMetadata: currentVersion,
|
versionMetadata: currentVersion,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -235,15 +241,16 @@ describe('simple site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('docs with editUrl', async () => {
|
test('docs with editUrl', async () => {
|
||||||
const {siteDir, context, options, currentVersion} = await loadSite();
|
const {siteDir, context, options, currentVersion} = await loadSite({
|
||||||
|
options: {
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const testUtilsLocal = createTestUtils({
|
const testUtilsLocal = createTestUtils({
|
||||||
siteDir,
|
siteDir,
|
||||||
context,
|
context,
|
||||||
options: {
|
options,
|
||||||
...options,
|
|
||||||
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
|
||||||
},
|
|
||||||
versionMetadata: currentVersion,
|
versionMetadata: currentVersion,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -278,16 +285,17 @@ describe('simple site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('docs with last update time and author', async () => {
|
test('docs with last update time and author', async () => {
|
||||||
const {siteDir, context, options, currentVersion} = await loadSite();
|
const {siteDir, context, options, currentVersion} = await loadSite({
|
||||||
|
options: {
|
||||||
|
showLastUpdateAuthor: true,
|
||||||
|
showLastUpdateTime: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const testUtilsLocal = createTestUtils({
|
const testUtilsLocal = createTestUtils({
|
||||||
siteDir,
|
siteDir,
|
||||||
context,
|
context,
|
||||||
options: {
|
options,
|
||||||
...options,
|
|
||||||
showLastUpdateAuthor: true,
|
|
||||||
showLastUpdateTime: true,
|
|
||||||
},
|
|
||||||
versionMetadata: currentVersion,
|
versionMetadata: currentVersion,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -361,15 +369,16 @@ describe('simple site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('docs with slug on doc home', async () => {
|
test('docs with slug on doc home', async () => {
|
||||||
const {siteDir, context, options, currentVersion} = await loadSite();
|
const {siteDir, context, options, currentVersion} = await loadSite({
|
||||||
|
options: {
|
||||||
|
homePageId: 'homePageId',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const testUtilsLocal = createTestUtils({
|
const testUtilsLocal = createTestUtils({
|
||||||
siteDir,
|
siteDir,
|
||||||
context,
|
context,
|
||||||
options: {
|
options,
|
||||||
...options,
|
|
||||||
homePageId: 'homePageId',
|
|
||||||
},
|
|
||||||
versionMetadata: currentVersion,
|
versionMetadata: currentVersion,
|
||||||
});
|
});
|
||||||
expect(() => {
|
expect(() => {
|
||||||
|
@ -388,19 +397,23 @@ describe('simple site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('versioned site', () => {
|
describe('versioned site', () => {
|
||||||
async function loadSite() {
|
async function loadSite(
|
||||||
|
loadSiteOptions: {options: Partial<PluginOptions>; locale?: string} = {
|
||||||
|
options: {},
|
||||||
|
},
|
||||||
|
) {
|
||||||
const siteDir = path.join(fixtureDir, 'versioned-site');
|
const siteDir = path.join(fixtureDir, 'versioned-site');
|
||||||
const context = await loadContext(siteDir);
|
const context = await loadContext(siteDir, {
|
||||||
|
locale: loadSiteOptions.locale,
|
||||||
|
});
|
||||||
const options = {
|
const options = {
|
||||||
id: DEFAULT_PLUGIN_ID,
|
id: DEFAULT_PLUGIN_ID,
|
||||||
...DEFAULT_OPTIONS,
|
...DEFAULT_OPTIONS,
|
||||||
|
...loadSiteOptions.options,
|
||||||
};
|
};
|
||||||
const versionsMetadata = readVersionsMetadata({
|
const versionsMetadata = readVersionsMetadata({
|
||||||
context,
|
context,
|
||||||
options: {
|
options,
|
||||||
id: DEFAULT_PLUGIN_ID,
|
|
||||||
...DEFAULT_OPTIONS,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
expect(versionsMetadata.length).toEqual(4);
|
expect(versionsMetadata.length).toEqual(4);
|
||||||
const [
|
const [
|
||||||
|
@ -495,7 +508,7 @@ describe('versioned site', () => {
|
||||||
permalink: '/docs/1.0.0/hello',
|
permalink: '/docs/1.0.0/hello',
|
||||||
slug: '/hello',
|
slug: '/hello',
|
||||||
title: 'hello',
|
title: 'hello',
|
||||||
description: 'Hello 1.0.0 ! (translated)',
|
description: 'Hello 1.0.0 ! (translated en)',
|
||||||
version: '1.0.0',
|
version: '1.0.0',
|
||||||
source:
|
source:
|
||||||
'@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
'@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
||||||
|
@ -582,15 +595,17 @@ describe('versioned site', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('translated doc with editUrl', async () => {
|
test('translated doc with editUrl', async () => {
|
||||||
const {siteDir, context, options, version100} = await loadSite();
|
const {siteDir, context, options, version100} = await loadSite({
|
||||||
|
options: {
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
||||||
|
// editCurrentVersion: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const testUtilsLocal = createTestUtils({
|
const testUtilsLocal = createTestUtils({
|
||||||
siteDir,
|
siteDir,
|
||||||
context,
|
context,
|
||||||
options: {
|
options,
|
||||||
...options,
|
|
||||||
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
|
||||||
},
|
|
||||||
versionMetadata: version100,
|
versionMetadata: version100,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -601,12 +616,108 @@ describe('versioned site', () => {
|
||||||
permalink: '/docs/1.0.0/hello',
|
permalink: '/docs/1.0.0/hello',
|
||||||
slug: '/hello',
|
slug: '/hello',
|
||||||
title: 'hello',
|
title: 'hello',
|
||||||
description: 'Hello 1.0.0 ! (translated)',
|
description: 'Hello 1.0.0 ! (translated en)',
|
||||||
version: '1.0.0',
|
version: '1.0.0',
|
||||||
source:
|
source:
|
||||||
'@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
'@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
||||||
editUrl:
|
editUrl:
|
||||||
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-1.0.0/hello.md',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('translated en doc with editUrl and editCurrentVersion=true', async () => {
|
||||||
|
const {siteDir, context, options, version100} = await loadSite({
|
||||||
|
options: {
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
||||||
|
editCurrentVersion: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const testUtilsLocal = createTestUtils({
|
||||||
|
siteDir,
|
||||||
|
context,
|
||||||
|
options,
|
||||||
|
versionMetadata: version100,
|
||||||
|
});
|
||||||
|
|
||||||
|
await testUtilsLocal.testMeta(path.join('hello.md'), {
|
||||||
|
id: 'version-1.0.0/hello',
|
||||||
|
unversionedId: 'hello',
|
||||||
|
isDocsHomePage: false,
|
||||||
|
permalink: '/docs/1.0.0/hello',
|
||||||
|
slug: '/hello',
|
||||||
|
title: 'hello',
|
||||||
|
description: 'Hello 1.0.0 ! (translated en)',
|
||||||
|
version: '1.0.0',
|
||||||
|
source:
|
||||||
|
'@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
||||||
|
editUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/docs/hello.md',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('translated fr doc with editUrl and editLocalizedDocs=true', async () => {
|
||||||
|
const {siteDir, context, options, version100} = await loadSite({
|
||||||
|
options: {
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
||||||
|
editLocalizedDocs: true,
|
||||||
|
},
|
||||||
|
locale: 'fr',
|
||||||
|
});
|
||||||
|
|
||||||
|
const testUtilsLocal = createTestUtils({
|
||||||
|
siteDir,
|
||||||
|
context,
|
||||||
|
options,
|
||||||
|
versionMetadata: version100,
|
||||||
|
});
|
||||||
|
|
||||||
|
await testUtilsLocal.testMeta(path.join('hello.md'), {
|
||||||
|
id: 'version-1.0.0/hello',
|
||||||
|
unversionedId: 'hello',
|
||||||
|
isDocsHomePage: false,
|
||||||
|
permalink: '/fr/docs/1.0.0/hello',
|
||||||
|
slug: '/hello',
|
||||||
|
title: 'hello',
|
||||||
|
description: 'Hello 1.0.0 ! (translated fr)',
|
||||||
|
version: '1.0.0',
|
||||||
|
source:
|
||||||
|
'@site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
||||||
|
editUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('translated fr doc with editUrl and editLocalizedDocs=true + editCurrentVersion=true', async () => {
|
||||||
|
const {siteDir, context, options, version100} = await loadSite({
|
||||||
|
options: {
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
|
||||||
|
editCurrentVersion: true,
|
||||||
|
editLocalizedDocs: true,
|
||||||
|
},
|
||||||
|
locale: 'fr',
|
||||||
|
});
|
||||||
|
|
||||||
|
const testUtilsLocal = createTestUtils({
|
||||||
|
siteDir,
|
||||||
|
context,
|
||||||
|
options,
|
||||||
|
versionMetadata: version100,
|
||||||
|
});
|
||||||
|
|
||||||
|
await testUtilsLocal.testMeta(path.join('hello.md'), {
|
||||||
|
id: 'version-1.0.0/hello',
|
||||||
|
unversionedId: 'hello',
|
||||||
|
isDocsHomePage: false,
|
||||||
|
permalink: '/fr/docs/1.0.0/hello',
|
||||||
|
slug: '/hello',
|
||||||
|
title: 'hello',
|
||||||
|
description: 'Hello 1.0.0 ! (translated fr)',
|
||||||
|
version: '1.0.0',
|
||||||
|
source:
|
||||||
|
'@site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
|
||||||
|
editUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/fr/docusaurus-plugin-content-docs/current/hello.md',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -38,6 +38,8 @@ describe('normalizeDocsPluginOptions', () => {
|
||||||
excludeNextVersionDocs: true,
|
excludeNextVersionDocs: true,
|
||||||
includeCurrentVersion: false,
|
includeCurrentVersion: false,
|
||||||
disableVersioning: true,
|
disableVersioning: true,
|
||||||
|
editCurrentVersion: true,
|
||||||
|
editLocalizedDocs: true,
|
||||||
versions: {
|
versions: {
|
||||||
current: {
|
current: {
|
||||||
path: 'next',
|
path: 'next',
|
||||||
|
|
|
@ -381,6 +381,107 @@ describe('versioned site, pluginId=default', () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('readVersionsMetadata versioned site with editUrl', async () => {
|
||||||
|
const {
|
||||||
|
defaultOptions,
|
||||||
|
defaultContext,
|
||||||
|
vCurrent,
|
||||||
|
v101,
|
||||||
|
v100,
|
||||||
|
vwithSlugs,
|
||||||
|
} = await loadSite();
|
||||||
|
|
||||||
|
const versionsMetadata = readVersionsMetadata({
|
||||||
|
options: {
|
||||||
|
...defaultOptions,
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/',
|
||||||
|
},
|
||||||
|
context: defaultContext,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(versionsMetadata).toEqual([
|
||||||
|
{
|
||||||
|
...vCurrent,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/docs',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...v101,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-1.0.1',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-1.0.1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...v100,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-1.0.0',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-1.0.0',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...vwithSlugs,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-withSlugs',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-withSlugs',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('readVersionsMetadata versioned site with editUrl and editCurrentVersion=true', async () => {
|
||||||
|
const {
|
||||||
|
defaultOptions,
|
||||||
|
defaultContext,
|
||||||
|
vCurrent,
|
||||||
|
v101,
|
||||||
|
v100,
|
||||||
|
vwithSlugs,
|
||||||
|
} = await loadSite();
|
||||||
|
|
||||||
|
const versionsMetadata = readVersionsMetadata({
|
||||||
|
options: {
|
||||||
|
...defaultOptions,
|
||||||
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/',
|
||||||
|
editCurrentVersion: true,
|
||||||
|
},
|
||||||
|
context: defaultContext,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(versionsMetadata).toEqual([
|
||||||
|
{
|
||||||
|
...vCurrent,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/docs',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...v101,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/docs',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...v100,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/docs',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...vwithSlugs,
|
||||||
|
versionEditUrl:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/docs',
|
||||||
|
versionEditUrlLocalized:
|
||||||
|
'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
test('readVersionsMetadata versioned site with onlyIncludeVersions option', async () => {
|
test('readVersionsMetadata versioned site with onlyIncludeVersions option', async () => {
|
||||||
const {defaultOptions, defaultContext, v101, vwithSlugs} = await loadSite();
|
const {defaultOptions, defaultContext, v101, vwithSlugs} = await loadSite();
|
||||||
|
|
||||||
|
|
|
@ -70,18 +70,18 @@ export async function readDocFile(
|
||||||
source: string,
|
source: string,
|
||||||
options: LastUpdateOptions,
|
options: LastUpdateOptions,
|
||||||
): Promise<DocFile> {
|
): Promise<DocFile> {
|
||||||
const folderPath = await getFolderContainingFile(
|
const docsDirPath = await getFolderContainingFile(
|
||||||
getDocsDirPaths(versionMetadata),
|
getDocsDirPaths(versionMetadata),
|
||||||
source,
|
source,
|
||||||
);
|
);
|
||||||
|
|
||||||
const filePath = path.join(folderPath, source);
|
const filePath = path.join(docsDirPath, source);
|
||||||
|
|
||||||
const [content, lastUpdate] = await Promise.all([
|
const [content, lastUpdate] = await Promise.all([
|
||||||
fs.readFile(filePath, 'utf-8'),
|
fs.readFile(filePath, 'utf-8'),
|
||||||
readLastUpdateData(filePath, options),
|
readLastUpdateData(filePath, options),
|
||||||
]);
|
]);
|
||||||
return {source, content, lastUpdate, filePath};
|
return {source, content, lastUpdate, docsDirPath, filePath};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readVersionDocs(
|
export async function readVersionDocs(
|
||||||
|
@ -110,15 +110,24 @@ export function processDocMetadata({
|
||||||
context: LoadContext;
|
context: LoadContext;
|
||||||
options: MetadataOptions;
|
options: MetadataOptions;
|
||||||
}): DocMetadataBase {
|
}): DocMetadataBase {
|
||||||
const {source, content, lastUpdate, filePath} = docFile;
|
const {source, content, lastUpdate, docsDirPath, filePath} = docFile;
|
||||||
const {editUrl, homePageId} = options;
|
const {homePageId} = options;
|
||||||
const {siteDir} = context;
|
const {siteDir} = context;
|
||||||
|
|
||||||
// ex: api/myDoc -> api
|
// ex: api/myDoc -> api
|
||||||
// ex: myDoc -> .
|
// ex: myDoc -> .
|
||||||
const docsFileDirName = path.dirname(source);
|
const docsFileDirName = path.dirname(source);
|
||||||
|
|
||||||
const docsEditUrl = getEditUrl(path.relative(siteDir, filePath), editUrl);
|
const relativeFilePath = path.relative(docsDirPath, filePath);
|
||||||
|
|
||||||
|
const isLocalized = docsDirPath === versionMetadata.docsDirPathLocalized;
|
||||||
|
|
||||||
|
const versionEditUrl =
|
||||||
|
isLocalized && options.editLocalizedDocs
|
||||||
|
? versionMetadata.versionEditUrlLocalized
|
||||||
|
: versionMetadata.versionEditUrl;
|
||||||
|
|
||||||
|
const docsEditUrl = getEditUrl(relativeFilePath, versionEditUrl);
|
||||||
|
|
||||||
const {frontMatter = {}, excerpt} = parseMarkdownString(content);
|
const {frontMatter = {}, excerpt} = parseMarkdownString(content);
|
||||||
const {sidebar_label, custom_edit_url} = frontMatter;
|
const {sidebar_label, custom_edit_url} = frontMatter;
|
||||||
|
|
|
@ -37,6 +37,8 @@ export const DEFAULT_OPTIONS: Omit<PluginOptions, 'id'> = {
|
||||||
disableVersioning: false,
|
disableVersioning: false,
|
||||||
lastVersion: undefined,
|
lastVersion: undefined,
|
||||||
versions: {},
|
versions: {},
|
||||||
|
editCurrentVersion: false,
|
||||||
|
editLocalizedDocs: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const VersionOptionsSchema = Joi.object({
|
const VersionOptionsSchema = Joi.object({
|
||||||
|
@ -51,6 +53,8 @@ const VersionsOptionsSchema = Joi.object()
|
||||||
export const OptionsSchema = Joi.object({
|
export const OptionsSchema = Joi.object({
|
||||||
path: Joi.string().default(DEFAULT_OPTIONS.path),
|
path: Joi.string().default(DEFAULT_OPTIONS.path),
|
||||||
editUrl: URISchema,
|
editUrl: URISchema,
|
||||||
|
editCurrentVersion: Joi.boolean().default(DEFAULT_OPTIONS.editCurrentVersion),
|
||||||
|
editLocalizedDocs: Joi.boolean().default(DEFAULT_OPTIONS.editLocalizedDocs),
|
||||||
routeBasePath: Joi.string()
|
routeBasePath: Joi.string()
|
||||||
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
|
// '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
|
||||||
// .allow('') ""
|
// .allow('') ""
|
||||||
|
|
|
@ -9,7 +9,8 @@
|
||||||
/// <reference types="@docusaurus/module-type-aliases" />
|
/// <reference types="@docusaurus/module-type-aliases" />
|
||||||
|
|
||||||
export type DocFile = {
|
export type DocFile = {
|
||||||
filePath: string;
|
docsDirPath: string; // /!\ may be localized
|
||||||
|
filePath: string; // /!\ may be localized
|
||||||
source: string;
|
source: string;
|
||||||
content: string;
|
content: string;
|
||||||
lastUpdate: LastUpdateData;
|
lastUpdate: LastUpdateData;
|
||||||
|
@ -21,6 +22,8 @@ export type VersionMetadata = {
|
||||||
versionName: VersionName; // 1.0.0
|
versionName: VersionName; // 1.0.0
|
||||||
versionLabel: string; // Version 1.0.0
|
versionLabel: string; // Version 1.0.0
|
||||||
versionPath: string; // /baseUrl/docs/1.0.0
|
versionPath: string; // /baseUrl/docs/1.0.0
|
||||||
|
versionEditUrl?: string | undefined;
|
||||||
|
versionEditUrlLocalized?: string | undefined;
|
||||||
isLast: boolean;
|
isLast: boolean;
|
||||||
docsDirPath: string; // "versioned_docs/version-1.0.0"
|
docsDirPath: string; // "versioned_docs/version-1.0.0"
|
||||||
docsDirPathLocalized: string; // "i18n/fr/version-1.0.0/default"
|
docsDirPathLocalized: string; // "i18n/fr/version-1.0.0/default"
|
||||||
|
@ -32,6 +35,8 @@ export type MetadataOptions = {
|
||||||
routeBasePath: string;
|
routeBasePath: string;
|
||||||
homePageId?: string;
|
homePageId?: string;
|
||||||
editUrl?: string;
|
editUrl?: string;
|
||||||
|
editCurrentVersion: boolean;
|
||||||
|
editLocalizedDocs: boolean;
|
||||||
showLastUpdateTime?: boolean;
|
showLastUpdateTime?: boolean;
|
||||||
showLastUpdateAuthor?: boolean;
|
showLastUpdateAuthor?: boolean;
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,7 +22,7 @@ import {
|
||||||
|
|
||||||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
||||||
import {LoadContext} from '@docusaurus/types';
|
import {LoadContext} from '@docusaurus/types';
|
||||||
import {getPluginI18nPath, normalizeUrl} from '@docusaurus/utils';
|
import {getPluginI18nPath, normalizeUrl, posixPath} from '@docusaurus/utils';
|
||||||
import {difference} from 'lodash';
|
import {difference} from 'lodash';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
|
|
||||||
|
@ -131,6 +131,30 @@ function readVersionNames(
|
||||||
return versions;
|
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({
|
function getVersionMetadataPaths({
|
||||||
versionName,
|
versionName,
|
||||||
context,
|
context,
|
||||||
|
@ -152,16 +176,11 @@ function getVersionMetadataPaths({
|
||||||
`version-${versionName}`,
|
`version-${versionName}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const docsDirPathLocalized = getPluginI18nPath({
|
const docsDirPathLocalized = getDocsDirPathLocalized({
|
||||||
siteDir: context.siteDir,
|
siteDir: context.siteDir,
|
||||||
locale: context.i18n.currentLocale,
|
locale: context.i18n.currentLocale,
|
||||||
pluginName: 'docusaurus-plugin-content-docs',
|
|
||||||
pluginId: options.id,
|
pluginId: options.id,
|
||||||
subPaths: [
|
versionName,
|
||||||
versionName === CURRENT_VERSION_NAME
|
|
||||||
? CURRENT_VERSION_NAME
|
|
||||||
: `version-${versionName}`,
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const sidebarFilePath = isCurrentVersion
|
const sidebarFilePath = isCurrentVersion
|
||||||
|
@ -174,6 +193,54 @@ function getVersionMetadataPaths({
|
||||||
return {docsDirPath, docsDirPathLocalized, sidebarFilePath};
|
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({
|
function createVersionMetadata({
|
||||||
versionName,
|
versionName,
|
||||||
isLast,
|
isLast,
|
||||||
|
@ -185,7 +252,13 @@ function createVersionMetadata({
|
||||||
context: Pick<LoadContext, 'siteDir' | 'baseUrl' | 'i18n'>;
|
context: Pick<LoadContext, 'siteDir' | 'baseUrl' | 'i18n'>;
|
||||||
options: Pick<
|
options: Pick<
|
||||||
PluginOptions,
|
PluginOptions,
|
||||||
'id' | 'path' | 'sidebarPath' | 'routeBasePath' | 'versions'
|
| 'id'
|
||||||
|
| 'path'
|
||||||
|
| 'sidebarPath'
|
||||||
|
| 'routeBasePath'
|
||||||
|
| 'versions'
|
||||||
|
| 'editUrl'
|
||||||
|
| 'editCurrentVersion'
|
||||||
>;
|
>;
|
||||||
}): VersionMetadata {
|
}): VersionMetadata {
|
||||||
const {
|
const {
|
||||||
|
@ -218,6 +291,13 @@ function createVersionMetadata({
|
||||||
versionPathPart,
|
versionPathPart,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const versionEditUrls = getVersionEditUrls({
|
||||||
|
docsDirPath,
|
||||||
|
docsDirPathLocalized,
|
||||||
|
context,
|
||||||
|
options,
|
||||||
|
});
|
||||||
|
|
||||||
// Because /docs/:route` should always be after `/docs/versionName/:route`.
|
// Because /docs/:route` should always be after `/docs/versionName/:route`.
|
||||||
const routePriority = versionPathPart === '' ? -1 : undefined;
|
const routePriority = versionPathPart === '' ? -1 : undefined;
|
||||||
|
|
||||||
|
@ -225,6 +305,8 @@ function createVersionMetadata({
|
||||||
versionName,
|
versionName,
|
||||||
versionLabel,
|
versionLabel,
|
||||||
versionPath,
|
versionPath,
|
||||||
|
versionEditUrl: versionEditUrls?.versionEditUrl,
|
||||||
|
versionEditUrlLocalized: versionEditUrls?.versionEditUrlLocalized,
|
||||||
isLast,
|
isLast,
|
||||||
routePriority,
|
routePriority,
|
||||||
sidebarFilePath,
|
sidebarFilePath,
|
||||||
|
@ -354,6 +436,8 @@ export function readVersionsMetadata({
|
||||||
| 'lastVersion'
|
| 'lastVersion'
|
||||||
| 'versions'
|
| 'versions'
|
||||||
| 'onlyIncludeVersions'
|
| 'onlyIncludeVersions'
|
||||||
|
| 'editUrl'
|
||||||
|
| 'editCurrentVersion'
|
||||||
>;
|
>;
|
||||||
}): VersionMetadata[] {
|
}): VersionMetadata[] {
|
||||||
const versionNamesUnfiltered = readVersionNames(context.siteDir, options);
|
const versionNamesUnfiltered = readVersionNames(context.siteDir, options);
|
||||||
|
|
|
@ -35,6 +35,18 @@ module.exports = {
|
||||||
* Example: 'https://github.com/facebook/docusaurus/edit/master/website/'
|
* Example: 'https://github.com/facebook/docusaurus/edit/master/website/'
|
||||||
*/
|
*/
|
||||||
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/',
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/',
|
||||||
|
/**
|
||||||
|
* When docs are versioned, the edit url will link to the doc
|
||||||
|
* in current version, instead of the versioned doc.
|
||||||
|
* Useful if you don't want users to submit doc pull-requests to older versions.
|
||||||
|
*/
|
||||||
|
editCurrentVersion: false,
|
||||||
|
/**
|
||||||
|
* When docs are localized, the edit url will target the localized doc,
|
||||||
|
* instead of the original unlocalized doc.
|
||||||
|
* Useful if you commit localized docs to git, instead of using a translation service.
|
||||||
|
*/
|
||||||
|
editLocalizedDocs: false,
|
||||||
/**
|
/**
|
||||||
* URL route for the docs section of your site.
|
* URL route for the docs section of your site.
|
||||||
* *DO NOT* include a trailing slash.
|
* *DO NOT* include a trailing slash.
|
||||||
|
|
|
@ -77,6 +77,7 @@ module.exports = {
|
||||||
id: 'community',
|
id: 'community',
|
||||||
path: 'community',
|
path: 'community',
|
||||||
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/',
|
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/',
|
||||||
|
editCurrentVersion: true,
|
||||||
routeBasePath: 'community',
|
routeBasePath: 'community',
|
||||||
sidebarPath: require.resolve('./sidebarsCommunity.js'),
|
sidebarPath: require.resolve('./sidebarsCommunity.js'),
|
||||||
showLastUpdateAuthor: true,
|
showLastUpdateAuthor: true,
|
||||||
|
@ -205,20 +206,21 @@ module.exports = {
|
||||||
sidebarPath: require.resolve('./sidebars.js'),
|
sidebarPath: require.resolve('./sidebars.js'),
|
||||||
editUrl:
|
editUrl:
|
||||||
'https://github.com/facebook/docusaurus/edit/master/website/',
|
'https://github.com/facebook/docusaurus/edit/master/website/',
|
||||||
|
editCurrentVersion: true,
|
||||||
showLastUpdateAuthor: true,
|
showLastUpdateAuthor: true,
|
||||||
showLastUpdateTime: true,
|
showLastUpdateTime: true,
|
||||||
remarkPlugins: [
|
remarkPlugins: [
|
||||||
[require('@docusaurus/remark-plugin-npm2yarn'), {sync: true}],
|
[require('@docusaurus/remark-plugin-npm2yarn'), {sync: true}],
|
||||||
],
|
],
|
||||||
disableVersioning: isVersioningDisabled,
|
disableVersioning: isVersioningDisabled,
|
||||||
lastVersion: 'current',
|
lastVersion: isDev ? 'current' : undefined,
|
||||||
onlyIncludeVersions:
|
onlyIncludeVersions:
|
||||||
!isVersioningDisabled && (isDev || isDeployPreview)
|
!isVersioningDisabled && (isDev || isDeployPreview)
|
||||||
? ['current', ...versions.slice(0, 2)]
|
? ['current', ...versions.slice(0, 2)]
|
||||||
: undefined,
|
: undefined,
|
||||||
versions: {
|
versions: {
|
||||||
current: {
|
current: {
|
||||||
label: `${getNextAlphaVersionName()} (unreleased)`,
|
label: `${getNextAlphaVersionName()} 🚧`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue