Skip to content

Commit

Permalink
feat(v2): new docs edit options: editCurrentVersion + editLocalizedDo…
Browse files Browse the repository at this point in the history
…cs (#3949)

* editCurrentVersion initial poc

* ensure edit url allows to edit localized docs

* Add editLocalizedDocs option

* keep editing current version in dev (more convenient)
  • Loading branch information
slorber committed Dec 28, 2020
1 parent 2791ccc commit b5c46bd
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ module.exports = {
url: 'https://your-docusaurus-test-site.com',
baseUrl: '/',
favicon: 'img/favicon.ico',
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr'],
},
};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Hello `1.0.0` ! (translated)
Hello `1.0.0` ! (translated en)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello `1.0.0` ! (translated fr)
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ Object {
\\"id\\": \\"version-1.0.0/hello\\",
\\"isDocsHomePage\\": true,
\\"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\\",
\\"slug\\": \\"/\\",
\\"permalink\\": \\"/docs/1.0.0/\\",
Expand Down
183 changes: 147 additions & 36 deletions packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DocMetadataBase,
MetadataOptions,
VersionMetadata,
PluginOptions,
} from '../types';
import {LoadContext} from '@docusaurus/types';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
Expand Down Expand Up @@ -42,6 +43,7 @@ ${markdown}
source,
content,
lastUpdate: {},
docsDirPath: 'docs',
filePath: source,
};
};
Expand Down Expand Up @@ -111,19 +113,19 @@ function createTestUtils({
}

describe('simple site', () => {
async function loadSite() {
async function loadSite(
loadSiteOptions: {options: Partial<PluginOptions>} = {options: {}},
) {
const siteDir = path.join(fixtureDir, 'simple-site');
const context = await loadContext(siteDir);
const options = {
id: DEFAULT_PLUGIN_ID,
...DEFAULT_OPTIONS,
...loadSiteOptions.options,
};
const versionsMetadata = readVersionsMetadata({
context,
options: {
id: DEFAULT_PLUGIN_ID,
...DEFAULT_OPTIONS,
},
options,
});
expect(versionsMetadata.length).toEqual(1);
const [currentVersion] = versionsMetadata;
Expand Down Expand Up @@ -191,12 +193,14 @@ describe('simple site', () => {
});

test('homePageId doc', async () => {
const {siteDir, context, options, currentVersion} = await loadSite();
const {siteDir, context, options, currentVersion} = await loadSite({
options: {homePageId: 'hello'},
});

const testUtilsLocal = createTestUtils({
siteDir,
context,
options: {...options, homePageId: 'hello'},
options,
versionMetadata: currentVersion,
});

Expand All @@ -213,12 +217,14 @@ describe('simple site', () => {
});

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({
siteDir,
context,
options: {...options, homePageId: 'foo/bar'},
options,
versionMetadata: currentVersion,
});

Expand All @@ -235,15 +241,16 @@ describe('simple site', () => {
});

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({
siteDir,
context,
options: {
...options,
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
},
options,
versionMetadata: currentVersion,
});

Expand Down Expand Up @@ -278,16 +285,17 @@ describe('simple site', () => {
});

test('docs with last update time and author', async () => {
const {siteDir, context, options, currentVersion} = await loadSite();

const testUtilsLocal = createTestUtils({
siteDir,
context,
const {siteDir, context, options, currentVersion} = await loadSite({
options: {
...options,
showLastUpdateAuthor: true,
showLastUpdateTime: true,
},
});

const testUtilsLocal = createTestUtils({
siteDir,
context,
options,
versionMetadata: currentVersion,
});

Expand Down Expand Up @@ -361,15 +369,16 @@ describe('simple site', () => {
});

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({
siteDir,
context,
options: {
...options,
homePageId: 'homePageId',
},
options,
versionMetadata: currentVersion,
});
expect(() => {
Expand All @@ -388,19 +397,23 @@ describe('simple 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 context = await loadContext(siteDir);
const context = await loadContext(siteDir, {
locale: loadSiteOptions.locale,
});
const options = {
id: DEFAULT_PLUGIN_ID,
...DEFAULT_OPTIONS,
...loadSiteOptions.options,
};
const versionsMetadata = readVersionsMetadata({
context,
options: {
id: DEFAULT_PLUGIN_ID,
...DEFAULT_OPTIONS,
},
options,
});
expect(versionsMetadata.length).toEqual(4);
const [
Expand Down Expand Up @@ -495,7 +508,7 @@ describe('versioned site', () => {
permalink: '/docs/1.0.0/hello',
slug: '/hello',
title: 'hello',
description: 'Hello 1.0.0 ! (translated)',
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',
Expand Down Expand Up @@ -582,15 +595,48 @@ describe('versioned site', () => {
});

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({
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/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: {
...options,
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website',
editCurrentVersion: true,
},
});

const testUtilsLocal = createTestUtils({
siteDir,
context,
options,
versionMetadata: version100,
});

Expand All @@ -601,12 +647,77 @@ describe('versioned site', () => {
permalink: '/docs/1.0.0/hello',
slug: '/hello',
title: 'hello',
description: 'Hello 1.0.0 ! (translated)',
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/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md',
'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',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('normalizeDocsPluginOptions', () => {
excludeNextVersionDocs: true,
includeCurrentVersion: false,
disableVersioning: true,
editCurrentVersion: true,
editLocalizedDocs: true,
versions: {
current: {
path: 'next',
Expand Down
Loading

0 comments on commit b5c46bd

Please sign in to comment.