mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-09 15:17:23 +02:00
refactor(v2): simplify code by removing wip i18n & translation (#1431)
* refactor(v2): remove i18n & versioning + cleaner code * totally remove i18n and versioning from plugin
This commit is contained in:
parent
0813920349
commit
373d17ee72
123 changed files with 170 additions and 4162 deletions
|
@ -31,87 +31,4 @@ Object {
|
|||
}
|
||||
`;
|
||||
|
||||
exports[`loadSidebars site with sidebars & versioned sidebars 1`] = `
|
||||
Object {
|
||||
"docs": Array [
|
||||
Object {
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "foo/bar",
|
||||
"type": "doc",
|
||||
},
|
||||
Object {
|
||||
"id": "foo/baz",
|
||||
"type": "doc",
|
||||
},
|
||||
],
|
||||
"label": "Test",
|
||||
"type": "category",
|
||||
},
|
||||
Object {
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "hello",
|
||||
"type": "doc",
|
||||
},
|
||||
],
|
||||
"label": "Guides",
|
||||
"type": "category",
|
||||
},
|
||||
],
|
||||
"version-1.0.0-docs": Array [
|
||||
Object {
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "version-1.0.0-foo/bar",
|
||||
"type": "doc",
|
||||
},
|
||||
Object {
|
||||
"id": "version-1.0.0-foo/baz",
|
||||
"type": "doc",
|
||||
},
|
||||
],
|
||||
"label": "Test",
|
||||
"type": "category",
|
||||
},
|
||||
Object {
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "version-1.0.0-hello",
|
||||
"type": "doc",
|
||||
},
|
||||
],
|
||||
"label": "Guides",
|
||||
"type": "category",
|
||||
},
|
||||
],
|
||||
"version-1.0.1-docs": Array [
|
||||
Object {
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "version-1.0.1-foo/bar",
|
||||
"type": "doc",
|
||||
},
|
||||
Object {
|
||||
"id": "version-1.0.1-foo/baz",
|
||||
"type": "doc",
|
||||
},
|
||||
],
|
||||
"label": "Test",
|
||||
"type": "category",
|
||||
},
|
||||
Object {
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "version-1.0.1-hello",
|
||||
"type": "doc",
|
||||
},
|
||||
],
|
||||
"label": "Guides",
|
||||
"type": "category",
|
||||
},
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`loadSidebars site without sidebars 1`] = `Object {}`;
|
||||
|
|
|
@ -7,16 +7,15 @@
|
|||
|
||||
import '@babel/polyfill';
|
||||
import path from 'path';
|
||||
import loadSetup from '../../../docusaurus/test/loadSetup';
|
||||
import loadSetup from '../../../docusaurus/lib/server/load/__tests__/loadSetup';
|
||||
import DocusaurusPluginContentDocs from '../index';
|
||||
|
||||
describe('loadDocs', () => {
|
||||
test('simple website', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('simple');
|
||||
const {siteDir, siteConfig} = await loadSetup('simple');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
|
@ -31,229 +30,22 @@ describe('loadDocs', () => {
|
|||
expect(docsMetadata.hello).toEqual({
|
||||
category: 'Guides',
|
||||
id: 'hello',
|
||||
language: null,
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/hello',
|
||||
previous: 'foo/baz',
|
||||
previous_id: 'foo/baz',
|
||||
previous_title: 'baz',
|
||||
sidebar: 'docs',
|
||||
source: path.join(docsDir, 'hello.md'),
|
||||
title: 'Hello, World !',
|
||||
version: null,
|
||||
});
|
||||
expect(docsMetadata['foo/bar']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'foo/bar',
|
||||
language: null,
|
||||
localized_id: 'foo/bar',
|
||||
next: 'foo/baz',
|
||||
next_id: 'foo/baz',
|
||||
next_title: 'baz',
|
||||
permalink: '/docs/foo/bar',
|
||||
sidebar: 'docs',
|
||||
source: path.join(docsDir, 'foo', 'bar.md'),
|
||||
title: 'Bar',
|
||||
version: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('versioned website', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('versioned');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata, versionedDir} = await plugin.loadContent();
|
||||
const docsDir = plugin.contentPath;
|
||||
|
||||
expect(docsMetadata['version-1.0.0-foo/bar']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'version-1.0.0-foo/bar',
|
||||
language: null,
|
||||
localized_id: 'version-1.0.0-foo/bar',
|
||||
next: 'version-1.0.0-foo/baz',
|
||||
next_id: 'version-1.0.0-foo/baz',
|
||||
next_title: 'Baz',
|
||||
permalink: '/docs/1.0.0/foo/bar',
|
||||
sidebar: 'version-1.0.0-docs',
|
||||
source: path.join(versionedDir, 'version-1.0.0/foo/bar.md'),
|
||||
title: 'Bar',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(docsMetadata['foo/bar']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'foo/bar',
|
||||
language: null,
|
||||
localized_id: 'foo/bar',
|
||||
next: 'foo/baz',
|
||||
next_id: 'foo/baz',
|
||||
next_title: 'baz',
|
||||
permalink: '/docs/next/foo/bar',
|
||||
sidebar: 'docs',
|
||||
source: path.join(docsDir, 'foo/bar.md'),
|
||||
title: 'Bar',
|
||||
version: 'next',
|
||||
});
|
||||
});
|
||||
|
||||
test('versioned & translated website', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('transversioned');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
);
|
||||
const {
|
||||
docs: docsMetadata,
|
||||
translatedDir,
|
||||
versionedDir,
|
||||
} = await plugin.loadContent();
|
||||
const docsDir = plugin.contentPath;
|
||||
|
||||
expect(docsMetadata['ko-version-1.0.0-foo/bar']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'ko-version-1.0.0-foo/bar',
|
||||
language: 'ko',
|
||||
localized_id: 'version-1.0.0-foo/bar',
|
||||
next: 'ko-version-1.0.0-foo/baz',
|
||||
next_id: 'version-1.0.0-foo/baz',
|
||||
next_title: 'baz',
|
||||
permalink: '/docs/ko/1.0.0/foo/bar',
|
||||
sidebar: 'version-1.0.0-docs',
|
||||
source: path.join(translatedDir, '/ko/version-1.0.0/foo/bar.md'),
|
||||
title: 'Bar',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(docsMetadata['en-version-1.0.0-foo/baz']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'en-version-1.0.0-foo/baz',
|
||||
language: 'en',
|
||||
localized_id: 'version-1.0.0-foo/baz',
|
||||
next: 'en-version-1.0.0-hello',
|
||||
next_id: 'version-1.0.0-hello',
|
||||
next_title: 'Hello, World !',
|
||||
permalink: '/docs/en/1.0.0/foo/baz',
|
||||
previous: 'en-version-1.0.0-foo/bar',
|
||||
previous_id: 'version-1.0.0-foo/bar',
|
||||
previous_title: 'Bar',
|
||||
sidebar: 'version-1.0.0-docs',
|
||||
source: path.join(versionedDir, 'version-1.0.0/foo/baz.md'),
|
||||
title: 'Baz',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(docsMetadata['en-hello']).toEqual({
|
||||
category: 'Guides',
|
||||
id: 'en-hello',
|
||||
language: 'en',
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/en/next/hello',
|
||||
previous: 'en-foo/baz',
|
||||
previous_id: 'foo/baz',
|
||||
previous_title: 'baz',
|
||||
sidebar: 'docs',
|
||||
source: path.join(docsDir, 'hello.md'),
|
||||
title: 'Hello, World !',
|
||||
version: 'next',
|
||||
});
|
||||
});
|
||||
|
||||
test('translated website', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('translated');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata, translatedDir} = await plugin.loadContent();
|
||||
const docsDir = plugin.contentPath;
|
||||
|
||||
expect(docsMetadata['ko-foo/baz']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'ko-foo/baz',
|
||||
language: 'ko',
|
||||
localized_id: 'foo/baz',
|
||||
next: 'ko-hello',
|
||||
next_id: 'hello',
|
||||
next_title: 'Hello, World !',
|
||||
permalink: '/docs/ko/foo/baz',
|
||||
previous: 'ko-foo/bar',
|
||||
previous_id: 'foo/bar',
|
||||
previous_title: 'Bar',
|
||||
sidebar: 'docs',
|
||||
source: path.join(translatedDir, 'ko', 'foo', 'baz.md'),
|
||||
title: 'baz',
|
||||
version: null,
|
||||
});
|
||||
expect(docsMetadata['en-foo/bar']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'en-foo/bar',
|
||||
language: 'en',
|
||||
localized_id: 'foo/bar',
|
||||
next: 'en-foo/baz',
|
||||
next_id: 'foo/baz',
|
||||
next_title: 'baz',
|
||||
permalink: '/docs/en/foo/bar',
|
||||
sidebar: 'docs',
|
||||
source: path.join(docsDir, 'foo', 'bar.md'),
|
||||
title: 'Bar',
|
||||
version: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('versioned website with skip next release', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('versioned');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
skipNextRelease: true,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata, versionedDir} = await plugin.loadContent();
|
||||
|
||||
expect(docsMetadata['version-1.0.0-foo/bar']).toEqual({
|
||||
category: 'Test',
|
||||
id: 'version-1.0.0-foo/bar',
|
||||
language: null,
|
||||
localized_id: 'version-1.0.0-foo/bar',
|
||||
next: 'version-1.0.0-foo/baz',
|
||||
next_id: 'version-1.0.0-foo/baz',
|
||||
next_title: 'Baz',
|
||||
permalink: '/docs/1.0.0/foo/bar',
|
||||
sidebar: 'version-1.0.0-docs',
|
||||
source: path.join(versionedDir, 'version-1.0.0/foo/bar.md'),
|
||||
title: 'Bar',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(docsMetadata['foo/bar']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -8,19 +8,18 @@
|
|||
import '@babel/polyfill';
|
||||
import path from 'path';
|
||||
import processMetadata from '../metadata';
|
||||
import loadSetup from '../../../docusaurus/test/loadSetup';
|
||||
import loadSetup from '../../../docusaurus/lib/server/load/__tests__/loadSetup';
|
||||
|
||||
describe('processMetadata', () => {
|
||||
test('normal docs', async () => {
|
||||
const props = await loadSetup('simple');
|
||||
const {siteDir, env, siteConfig} = props;
|
||||
const {siteDir, siteConfig} = props;
|
||||
const docsDir = path.resolve(siteDir, '..', 'docs');
|
||||
const sourceA = path.join('foo', 'bar.md');
|
||||
const sourceB = path.join('hello.md');
|
||||
const dataA = await processMetadata(
|
||||
sourceA,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
|
@ -28,362 +27,35 @@ describe('processMetadata', () => {
|
|||
const dataB = await processMetadata(
|
||||
sourceB,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
expect(dataA).toEqual({
|
||||
id: 'foo/bar',
|
||||
language: null,
|
||||
localized_id: 'foo/bar',
|
||||
permalink: '/docs/foo/bar',
|
||||
source: path.join(docsDir, sourceA),
|
||||
title: 'Bar',
|
||||
version: null,
|
||||
});
|
||||
expect(dataB).toEqual({
|
||||
id: 'hello',
|
||||
language: null,
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/hello',
|
||||
source: path.join(docsDir, sourceB),
|
||||
title: 'Hello, World !',
|
||||
version: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('docs with custom permalink', async () => {
|
||||
const props = await loadSetup('simple');
|
||||
const {siteDir, env, siteConfig} = props;
|
||||
const {siteDir, siteConfig} = props;
|
||||
const docsDir = path.resolve(siteDir, '..', 'docs');
|
||||
const source = path.join('permalink.md');
|
||||
const data = await processMetadata(
|
||||
source,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const data = await processMetadata(source, docsDir, {}, siteConfig, 'docs');
|
||||
expect(data).toEqual({
|
||||
id: 'permalink',
|
||||
language: null,
|
||||
localized_id: 'permalink',
|
||||
permalink: '/docs/endiliey/permalink',
|
||||
source: path.join(docsDir, source),
|
||||
title: 'Permalink',
|
||||
version: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('versioned docs (without translation)', async () => {
|
||||
const props = await loadSetup('versioned');
|
||||
const {siteDir, env, siteConfig} = props;
|
||||
const docsDir = path.resolve(siteDir, '..', 'docs');
|
||||
const versionedDir = path.join(siteDir, 'versioned_docs');
|
||||
const sourceA = path.join('version-1.0.0', 'foo', 'bar.md');
|
||||
const sourceB = path.join('version-1.0.0', 'hello.md');
|
||||
const sourceC = path.join('foo', 'bar.md');
|
||||
const sourceD = path.join('hello.md');
|
||||
const dataA = await processMetadata(
|
||||
sourceA,
|
||||
versionedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataB = await processMetadata(
|
||||
sourceB,
|
||||
versionedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataC = await processMetadata(
|
||||
sourceC,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataD = await processMetadata(
|
||||
sourceD,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
expect(dataA).toEqual({
|
||||
id: 'version-1.0.0-foo/bar',
|
||||
language: null,
|
||||
localized_id: 'version-1.0.0-foo/bar',
|
||||
permalink: '/docs/1.0.0/foo/bar',
|
||||
source: path.join(versionedDir, sourceA),
|
||||
title: 'Bar',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(dataB).toEqual({
|
||||
id: 'version-1.0.0-hello',
|
||||
language: null,
|
||||
localized_id: 'version-1.0.0-hello',
|
||||
permalink: '/docs/1.0.0/hello',
|
||||
source: path.join(versionedDir, sourceB),
|
||||
title: 'Hello, World !',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(dataC).toEqual({
|
||||
id: 'foo/bar',
|
||||
language: null,
|
||||
localized_id: 'foo/bar',
|
||||
permalink: '/docs/next/foo/bar',
|
||||
source: path.join(docsDir, sourceC),
|
||||
title: 'Bar',
|
||||
version: 'next',
|
||||
});
|
||||
expect(dataD).toEqual({
|
||||
id: 'hello',
|
||||
language: null,
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/next/hello',
|
||||
source: path.join(docsDir, sourceD),
|
||||
title: 'Hello, World !',
|
||||
version: 'next',
|
||||
});
|
||||
});
|
||||
|
||||
test('translated versioned docs', async () => {
|
||||
const props = await loadSetup('transversioned');
|
||||
const {siteDir, env, siteConfig} = props;
|
||||
const docsDir = path.resolve(siteDir, '..', 'docs');
|
||||
const versionedDir = path.join(siteDir, 'versioned_docs');
|
||||
const translatedDir = path.join(siteDir, 'translated_docs');
|
||||
const sourceA = path.join('ko', 'version-1.0.0', 'foo', 'bar.md');
|
||||
const sourceB = path.join('ko', 'version-1.0.0', 'hello.md');
|
||||
const sourceC = path.join('ko', 'version-1.0.1', 'foo', 'bar.md');
|
||||
const sourceD = path.join('ko', 'version-1.0.1', 'hello.md');
|
||||
const sourceE = path.join('foo', 'bar.md');
|
||||
const sourceF = path.join('hello.md');
|
||||
const sourceG = path.join('version-1.0.0', 'foo', 'bar.md');
|
||||
const sourceH = path.join('version-1.0.0', 'hello.md');
|
||||
const dataA = await processMetadata(
|
||||
sourceA,
|
||||
translatedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataB = await processMetadata(
|
||||
sourceB,
|
||||
translatedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataC = await processMetadata(
|
||||
sourceC,
|
||||
translatedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataD = await processMetadata(
|
||||
sourceD,
|
||||
translatedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataE = await processMetadata(
|
||||
sourceE,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataF = await processMetadata(
|
||||
sourceF,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataG = await processMetadata(
|
||||
sourceG,
|
||||
versionedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataH = await processMetadata(
|
||||
sourceH,
|
||||
versionedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
expect(dataA).toEqual({
|
||||
id: 'ko-version-1.0.0-foo/bar',
|
||||
language: 'ko',
|
||||
localized_id: 'version-1.0.0-foo/bar',
|
||||
permalink: '/docs/ko/1.0.0/foo/bar',
|
||||
source: path.join(translatedDir, sourceA),
|
||||
title: 'Bar',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(dataB).toEqual({
|
||||
id: 'ko-version-1.0.0-hello',
|
||||
language: 'ko',
|
||||
localized_id: 'version-1.0.0-hello',
|
||||
permalink: '/docs/ko/1.0.0/hello',
|
||||
source: path.join(translatedDir, sourceB),
|
||||
title: 'Hello, World !',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(dataC).toEqual({
|
||||
id: 'ko-version-1.0.1-foo/bar',
|
||||
language: 'ko',
|
||||
localized_id: 'version-1.0.1-foo/bar',
|
||||
permalink: '/docs/ko/foo/bar',
|
||||
source: path.join(translatedDir, sourceC),
|
||||
title: 'Bar',
|
||||
version: '1.0.1',
|
||||
});
|
||||
expect(dataD).toEqual({
|
||||
id: 'ko-version-1.0.1-hello',
|
||||
language: 'ko',
|
||||
localized_id: 'version-1.0.1-hello',
|
||||
permalink: '/docs/ko/hello',
|
||||
source: path.join(translatedDir, sourceD),
|
||||
title: 'Hello, World !',
|
||||
version: '1.0.1',
|
||||
});
|
||||
expect(dataE).toEqual({
|
||||
id: 'en-foo/bar',
|
||||
language: 'en',
|
||||
localized_id: 'foo/bar',
|
||||
permalink: '/docs/en/next/foo/bar',
|
||||
source: path.join(docsDir, sourceE),
|
||||
title: 'Bar',
|
||||
version: 'next',
|
||||
});
|
||||
expect(dataF).toEqual({
|
||||
id: 'en-hello',
|
||||
language: 'en',
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/en/next/hello',
|
||||
source: path.join(docsDir, sourceF),
|
||||
title: 'Hello, World !',
|
||||
version: 'next',
|
||||
});
|
||||
expect(dataG).toEqual({
|
||||
id: 'en-version-1.0.0-foo/bar',
|
||||
language: 'en',
|
||||
localized_id: 'version-1.0.0-foo/bar',
|
||||
permalink: '/docs/en/1.0.0/foo/bar',
|
||||
source: path.join(versionedDir, sourceG),
|
||||
title: 'Bar',
|
||||
version: '1.0.0',
|
||||
});
|
||||
expect(dataH).toEqual({
|
||||
id: 'en-version-1.0.0-hello',
|
||||
language: 'en',
|
||||
localized_id: 'version-1.0.0-hello',
|
||||
permalink: '/docs/en/1.0.0/hello',
|
||||
source: path.join(versionedDir, sourceH),
|
||||
title: 'Hello, World !',
|
||||
version: '1.0.0',
|
||||
});
|
||||
});
|
||||
|
||||
test('translated docs only', async () => {
|
||||
const props = await loadSetup('translated');
|
||||
const {siteDir, env, siteConfig} = props;
|
||||
const docsDir = path.resolve(siteDir, '..', 'docs');
|
||||
const translatedDir = path.join(siteDir, 'translated_docs');
|
||||
const sourceA = path.join('ko', 'foo', 'bar.md');
|
||||
const sourceB = path.join('ko', 'hello.md');
|
||||
const sourceC = path.join('foo', 'bar.md');
|
||||
const sourceD = path.join('hello.md');
|
||||
const dataA = await processMetadata(
|
||||
sourceA,
|
||||
translatedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataB = await processMetadata(
|
||||
sourceB,
|
||||
translatedDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataC = await processMetadata(
|
||||
sourceC,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
const dataD = await processMetadata(
|
||||
sourceD,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
expect(dataA).toEqual({
|
||||
id: 'ko-foo/bar',
|
||||
language: 'ko',
|
||||
localized_id: 'foo/bar',
|
||||
permalink: '/docs/ko/foo/bar',
|
||||
source: path.join(translatedDir, sourceA),
|
||||
title: 'Bar',
|
||||
version: null,
|
||||
});
|
||||
expect(dataB).toEqual({
|
||||
id: 'ko-hello',
|
||||
language: 'ko',
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/ko/hello',
|
||||
source: path.join(translatedDir, sourceB),
|
||||
title: 'Hello, World !',
|
||||
version: null,
|
||||
});
|
||||
expect(dataC).toEqual({
|
||||
id: 'en-foo/bar',
|
||||
language: 'en',
|
||||
localized_id: 'foo/bar',
|
||||
permalink: '/docs/en/foo/bar',
|
||||
source: path.join(docsDir, sourceC),
|
||||
title: 'Bar',
|
||||
version: null,
|
||||
});
|
||||
expect(dataD).toEqual({
|
||||
id: 'en-hello',
|
||||
language: 'en',
|
||||
localized_id: 'hello',
|
||||
permalink: '/docs/en/hello',
|
||||
source: path.join(docsDir, sourceD),
|
||||
title: 'Hello, World !',
|
||||
version: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import path from 'path';
|
||||
import loadSidebars from '../sidebars';
|
||||
import loadSetup from '../../../docusaurus/test/loadSetup';
|
||||
import loadSetup from '../../../docusaurus/lib/server/load/__tests__/loadSetup';
|
||||
|
||||
/* eslint-disable global-require, import/no-dynamic-require */
|
||||
|
||||
|
@ -15,38 +15,15 @@ describe('loadSidebars', () => {
|
|||
const fixtures = path.join(__dirname, '..', '__fixtures__');
|
||||
|
||||
test('normal site with sidebars', async () => {
|
||||
const {env, siteDir} = await loadSetup('simple');
|
||||
const {siteDir} = await loadSetup('simple');
|
||||
const sidebar = require(path.join(siteDir, 'sidebars.json'));
|
||||
const result = loadSidebars({siteDir, env, sidebar});
|
||||
const result = loadSidebars({siteDir, sidebar});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('site without sidebars', () => {
|
||||
const env = {};
|
||||
const siteDir = path.join(fixtures, 'bad-site');
|
||||
const result = loadSidebars({siteDir, env, sidebar: {}});
|
||||
const result = loadSidebars({siteDir, sidebar: {}});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('site with sidebars & versioned sidebars', async () => {
|
||||
const {env, siteDir} = await loadSetup('versioned');
|
||||
const sidebar = require(path.join(siteDir, 'sidebars.json'));
|
||||
const result = loadSidebars({siteDir, env, sidebar});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('site with missing versioned sidebars', async () => {
|
||||
const env = {
|
||||
versioning: {
|
||||
enabled: true,
|
||||
versions: ['2.0.0'],
|
||||
},
|
||||
};
|
||||
const {siteDir} = await loadSetup('versioned');
|
||||
expect(() => {
|
||||
loadSidebars({siteDir, env, sidebar: {}});
|
||||
}).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Failed to load versioned_sidebars/version-2.0.0-sidebars.json. It does not exist."`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -8,12 +8,7 @@
|
|||
const globby = require('globby');
|
||||
const importFresh = require('import-fresh');
|
||||
const path = require('path');
|
||||
const {
|
||||
getSubFolder,
|
||||
idx,
|
||||
normalizeUrl,
|
||||
docuHash,
|
||||
} = require('@docusaurus/utils');
|
||||
const {idx, normalizeUrl, docuHash} = require('@docusaurus/utils');
|
||||
|
||||
const createOrder = require('./order');
|
||||
const loadSidebars = require('./sidebars');
|
||||
|
@ -28,7 +23,6 @@ const DEFAULT_OPTIONS = {
|
|||
// TODO: Settle themeing.
|
||||
docLayoutComponent: '@theme/Doc',
|
||||
docItemComponent: '@theme/DocBody',
|
||||
skipNextRelease: false, // Skip documents from next release (default = false)
|
||||
};
|
||||
|
||||
class DocusaurusPluginContentDocs {
|
||||
|
@ -53,123 +47,36 @@ class DocusaurusPluginContentDocs {
|
|||
|
||||
// Fetches blog contents and returns metadata for the contents.
|
||||
async loadContent() {
|
||||
const {include, routeBasePath, sidebarPath, skipNextRelease} = this.options;
|
||||
const {siteDir, env, siteConfig} = this.context;
|
||||
const {include, routeBasePath, sidebarPath} = this.options;
|
||||
const {siteDir, siteConfig} = this.context;
|
||||
const docsDir = this.contentPath;
|
||||
|
||||
// We don't want sidebars to be cached because of hotreloading.
|
||||
const sidebar = importFresh(sidebarPath);
|
||||
const docsSidebars = loadSidebars({siteDir, env, sidebar});
|
||||
const docsSidebars = loadSidebars({siteDir, sidebar});
|
||||
|
||||
// @tested - build the docs ordering such as next, previous, category and sidebar
|
||||
const order = createOrder(docsSidebars);
|
||||
|
||||
// Settle versions & translations from environment.
|
||||
const translationEnabled = idx(env, ['translation', 'enabled']);
|
||||
const enabledLanguages =
|
||||
translationEnabled && idx(env, ['translation', 'enabledLanguages']);
|
||||
const enabledLangTags =
|
||||
(enabledLanguages && enabledLanguages.map(lang => lang.tag)) || [];
|
||||
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
|
||||
const versioningEnabled = idx(env, ['versioning', 'enabled']);
|
||||
const versions =
|
||||
(versioningEnabled && idx(env, ['versioning', 'versions'])) || [];
|
||||
|
||||
// Prepare metadata container.
|
||||
const docs = {};
|
||||
|
||||
if (!(versioningEnabled && skipNextRelease)) {
|
||||
// Metadata for default docs files.
|
||||
const docsFiles = await globby(include, {
|
||||
cwd: docsDir,
|
||||
});
|
||||
await Promise.all(
|
||||
docsFiles.map(async source => {
|
||||
// Do not allow reserved version/ translated folder name in 'docs'
|
||||
// e.g: 'docs/version-1.0.0/' should not be allowed as it can cause unwanted bug
|
||||
const subFolder = getSubFolder(
|
||||
path.resolve(docsDir, source),
|
||||
docsDir,
|
||||
);
|
||||
const versionsFolders = versions.map(version => `version-${version}`);
|
||||
if ([...enabledLangTags, ...versionsFolders].includes(subFolder)) {
|
||||
throw new Error(
|
||||
`You cannot have a folder named 'docs/${subFolder}/'`,
|
||||
);
|
||||
}
|
||||
|
||||
const metadata = await processMetadata(
|
||||
source,
|
||||
docsDir,
|
||||
env,
|
||||
order,
|
||||
siteConfig,
|
||||
routeBasePath,
|
||||
);
|
||||
docs[metadata.id] = metadata;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Metadata for non-default-language docs.
|
||||
let translatedDir = null;
|
||||
if (translationEnabled) {
|
||||
translatedDir = path.join(siteDir, 'translated_docs');
|
||||
const translatedFiles = await globby(include, {
|
||||
cwd: translatedDir,
|
||||
});
|
||||
await Promise.all(
|
||||
translatedFiles.map(async source => {
|
||||
/*
|
||||
Do not process disabled & default languages folder in `translated_docs`
|
||||
e.g: 'translated_docs/ja/**' should not be processed if lang 'ja' is disabled
|
||||
*/
|
||||
const translatedFilePath = path.resolve(translatedDir, source);
|
||||
const detectedLangTag = getSubFolder(
|
||||
translatedFilePath,
|
||||
translatedDir,
|
||||
);
|
||||
if (
|
||||
detectedLangTag === defaultLangTag ||
|
||||
!enabledLangTags.includes(detectedLangTag)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const metadata = await processMetadata(
|
||||
source,
|
||||
translatedDir,
|
||||
env,
|
||||
order,
|
||||
siteConfig,
|
||||
routeBasePath,
|
||||
);
|
||||
docs[metadata.id] = metadata;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Metadata for versioned docs.
|
||||
let versionedDir = null;
|
||||
if (versioningEnabled) {
|
||||
versionedDir = path.join(siteDir, 'versioned_docs');
|
||||
const versionedFiles = await globby(include, {
|
||||
cwd: versionedDir,
|
||||
});
|
||||
await Promise.all(
|
||||
versionedFiles.map(async source => {
|
||||
const metadata = await processMetadata(
|
||||
source,
|
||||
versionedDir,
|
||||
env,
|
||||
order,
|
||||
siteConfig,
|
||||
routeBasePath,
|
||||
);
|
||||
docs[metadata.id] = metadata;
|
||||
}),
|
||||
);
|
||||
}
|
||||
// Metadata for default docs files.
|
||||
const docsFiles = await globby(include, {
|
||||
cwd: docsDir,
|
||||
});
|
||||
await Promise.all(
|
||||
docsFiles.map(async source => {
|
||||
const metadata = await processMetadata(
|
||||
source,
|
||||
docsDir,
|
||||
order,
|
||||
siteConfig,
|
||||
routeBasePath,
|
||||
);
|
||||
docs[metadata.id] = metadata;
|
||||
}),
|
||||
);
|
||||
|
||||
// Get the titles of the previous and next ids so that we can use them.
|
||||
Object.keys(docs).forEach(currentID => {
|
||||
|
@ -185,28 +92,19 @@ class DocusaurusPluginContentDocs {
|
|||
}
|
||||
});
|
||||
|
||||
const sourceToMetadata = {};
|
||||
const sourceToPermalink = {};
|
||||
const permalinkToId = {};
|
||||
Object.values(docs).forEach(
|
||||
({id, source, version, permalink, language}) => {
|
||||
sourceToMetadata[source] = {
|
||||
version,
|
||||
permalink,
|
||||
language,
|
||||
};
|
||||
|
||||
permalinkToId[permalink] = id;
|
||||
},
|
||||
);
|
||||
Object.values(docs).forEach(({id, source, permalink}) => {
|
||||
sourceToPermalink[source] = permalink;
|
||||
permalinkToId[permalink] = id;
|
||||
});
|
||||
|
||||
this.content = {
|
||||
docs,
|
||||
docsDir,
|
||||
docsSidebars,
|
||||
sourceToMetadata,
|
||||
sourceToPermalink,
|
||||
permalinkToId,
|
||||
translatedDir,
|
||||
versionedDir,
|
||||
};
|
||||
|
||||
return this.content;
|
||||
|
@ -254,9 +152,6 @@ class DocusaurusPluginContentDocs {
|
|||
}
|
||||
|
||||
configureWebpack(config, isServer, {getBabelLoader, getCacheLoader}) {
|
||||
const versionedDir = path.join(this.context.siteDir, 'versioned_docs');
|
||||
const translatedDir = path.join(this.context.siteDir, 'translated_docs');
|
||||
|
||||
return {
|
||||
module: {
|
||||
rules: [
|
||||
|
@ -271,10 +166,8 @@ class DocusaurusPluginContentDocs {
|
|||
loader: path.resolve(__dirname, './markdown/index.js'),
|
||||
options: {
|
||||
siteConfig: this.context.siteConfig,
|
||||
versionedDir,
|
||||
translatedDir,
|
||||
docsDir: this.content.docsDir,
|
||||
sourceToMetadata: this.content.sourceToMetadata,
|
||||
sourceToPermalink: this.content.sourceToPermalink,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
const fm = require('front-matter');
|
||||
const {getOptions} = require('loader-utils');
|
||||
const path = require('path');
|
||||
const {resolve} = require('url');
|
||||
|
||||
module.exports = async function(fileString) {
|
||||
|
@ -15,7 +14,7 @@ module.exports = async function(fileString) {
|
|||
const options = Object.assign({}, getOptions(this), {
|
||||
filepath: this.resourcePath,
|
||||
});
|
||||
const {versionedDir, docsDir, translatedDir, sourceToMetadata} = options;
|
||||
const {docsDir, sourceToPermalink} = options;
|
||||
|
||||
// Extract content of markdown (without frontmatter).
|
||||
const {body} = fm(fileString);
|
||||
|
@ -23,19 +22,7 @@ module.exports = async function(fileString) {
|
|||
// Determine the source dir. e.g: /docs, /website/versioned_docs/version-1.0.0
|
||||
let sourceDir;
|
||||
const thisSource = this.resourcePath;
|
||||
if (thisSource.startsWith(translatedDir)) {
|
||||
const {language, version} = sourceToMetadata[thisSource] || {};
|
||||
if (language && version && version !== 'next') {
|
||||
sourceDir = path.join(translatedDir, language, `version-${version}`);
|
||||
} else if (language && (!version || version === 'next')) {
|
||||
sourceDir = path.join(translatedDir, language);
|
||||
}
|
||||
} else if (thisSource.startsWith(versionedDir)) {
|
||||
const {version} = sourceToMetadata[thisSource] || {};
|
||||
if (version) {
|
||||
sourceDir = path.join(versionedDir, `version-${version}`);
|
||||
}
|
||||
} else if (thisSource.startsWith(docsDir)) {
|
||||
if (thisSource.startsWith(docsDir)) {
|
||||
sourceDir = docsDir;
|
||||
}
|
||||
|
||||
|
@ -59,10 +46,9 @@ module.exports = async function(fileString) {
|
|||
// Replace it to correct html link.
|
||||
const mdLink = mdMatch[1];
|
||||
const targetSource = `${sourceDir}/${mdLink}`;
|
||||
const {permalink} =
|
||||
sourceToMetadata[resolve(thisSource, mdLink)] ||
|
||||
sourceToMetadata[targetSource] ||
|
||||
{};
|
||||
const permalink =
|
||||
sourceToPermalink[resolve(thisSource, mdLink)] ||
|
||||
sourceToPermalink[targetSource];
|
||||
if (permalink) {
|
||||
modifiedLine = modifiedLine.replace(mdLink, permalink);
|
||||
}
|
||||
|
|
|
@ -7,53 +7,11 @@
|
|||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const {getSubFolder, idx, parse, normalizeUrl} = require('@docusaurus/utils');
|
||||
|
||||
function getLanguage(filepath, refDir, env) {
|
||||
const translationEnabled = idx(env, ['translation', 'enabled']);
|
||||
|
||||
if (translationEnabled) {
|
||||
const detectedLangTag = getSubFolder(filepath, refDir);
|
||||
const enabledLanguages = idx(env, ['translation', 'enabledLanguages']);
|
||||
const langTags =
|
||||
(enabledLanguages && enabledLanguages.map(lang => lang.tag)) || [];
|
||||
if (langTags.includes(detectedLangTag)) {
|
||||
return detectedLangTag;
|
||||
}
|
||||
|
||||
const defaultLanguage = idx(env, ['translation', 'defaultLanguage']);
|
||||
if (defaultLanguage && defaultLanguage.tag) {
|
||||
return defaultLanguage.tag;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getVersion(filepath, refDir, env) {
|
||||
const versioningEnabled = idx(env, ['versioning', 'enabled']);
|
||||
|
||||
if (versioningEnabled) {
|
||||
const subFolder = getSubFolder(filepath, refDir);
|
||||
|
||||
if (subFolder) {
|
||||
const detectedVersion = subFolder.replace(/^version-/, '');
|
||||
const versions = idx(env, ['versioning', 'versions']) || [];
|
||||
if (versions.includes(detectedVersion)) {
|
||||
return detectedVersion;
|
||||
}
|
||||
}
|
||||
|
||||
return 'next';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
const {parse, normalizeUrl} = require('@docusaurus/utils');
|
||||
|
||||
module.exports = async function processMetadata(
|
||||
source,
|
||||
refDir,
|
||||
env,
|
||||
order,
|
||||
siteConfig,
|
||||
docsBasePath,
|
||||
|
@ -75,44 +33,9 @@ module.exports = async function processMetadata(
|
|||
metadata.title = metadata.id;
|
||||
}
|
||||
|
||||
// Language.
|
||||
const language = getLanguage(filepath, refDir, env);
|
||||
metadata.language = language;
|
||||
const langPart = (language && `${language}/`) || '';
|
||||
|
||||
// Version.
|
||||
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
|
||||
let versionRefDir = refDir;
|
||||
if (language && language !== defaultLangTag) {
|
||||
versionRefDir = path.join(refDir, language);
|
||||
}
|
||||
const version = getVersion(filepath, versionRefDir, env);
|
||||
metadata.version = version;
|
||||
const latestVersion = idx(env, ['versioning', 'latestVersion']);
|
||||
const versionPart =
|
||||
(version && version !== latestVersion && `${version}/`) || '';
|
||||
|
||||
// Convert temporarily metadata.id to the form of dirname/id without version/lang prefix.
|
||||
// e.g.: file `versioned_docs/version-1.0.0/en/foo/bar.md` with id `version-1.0.0-bar` => `foo/bar`
|
||||
if (language) {
|
||||
metadata.id = metadata.id.replace(new RegExp(`^${language}-`), '');
|
||||
}
|
||||
|
||||
if (version) {
|
||||
metadata.id = metadata.id.replace(new RegExp(`^version-${version}-`), '');
|
||||
}
|
||||
|
||||
const dirName = path.dirname(source);
|
||||
if (dirName !== '.') {
|
||||
let prefix = dirName;
|
||||
if (language) {
|
||||
prefix = prefix.replace(new RegExp(`^${language}`), '');
|
||||
}
|
||||
prefix = prefix.replace(/^\//, '');
|
||||
if (version) {
|
||||
prefix = prefix.replace(new RegExp(`^version-${version}`), '');
|
||||
}
|
||||
prefix = prefix.replace(/^\//, '');
|
||||
const prefix = dirName;
|
||||
if (prefix) {
|
||||
metadata.id = `${prefix}/${metadata.id}`;
|
||||
}
|
||||
|
@ -132,46 +55,23 @@ module.exports = async function processMetadata(
|
|||
metadata.permalink
|
||||
.replace(/:baseUrl/, baseUrl)
|
||||
.replace(/:docsUrl/, docsBasePath)
|
||||
.replace(/:langPart/, langPart)
|
||||
.replace(/:versionPart/, versionPart)
|
||||
.replace(/:id/, metadata.id),
|
||||
);
|
||||
} else {
|
||||
metadata.permalink = normalizeUrl([
|
||||
baseUrl,
|
||||
docsBasePath,
|
||||
langPart,
|
||||
versionPart,
|
||||
metadata.id,
|
||||
]);
|
||||
}
|
||||
|
||||
// If version.
|
||||
if (version && version !== 'next') {
|
||||
metadata.id = `version-${version}-${metadata.id}`;
|
||||
}
|
||||
|
||||
// Save localized id before adding language on it.
|
||||
metadata.localized_id = metadata.id;
|
||||
|
||||
// If language.
|
||||
if (language) {
|
||||
metadata.id = `${language}-${metadata.id}`;
|
||||
metadata.permalink = normalizeUrl([baseUrl, docsBasePath, metadata.id]);
|
||||
}
|
||||
|
||||
// Determine order.
|
||||
const id = metadata.localized_id;
|
||||
const {id} = metadata;
|
||||
if (order[id]) {
|
||||
metadata.sidebar = order[id].sidebar;
|
||||
metadata.category = order[id].category;
|
||||
metadata.subCategory = order[id].subCategory;
|
||||
if (order[id].next) {
|
||||
metadata.next_id = order[id].next;
|
||||
metadata.next = (language ? `${language}-` : '') + order[id].next;
|
||||
metadata.next = order[id].next;
|
||||
}
|
||||
if (order[id].previous) {
|
||||
metadata.previous_id = order[id].previous;
|
||||
metadata.previous = (language ? `${language}-` : '') + order[id].previous;
|
||||
metadata.previous = order[id].previous;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,10 +5,6 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const {idx} = require('@docusaurus/utils');
|
||||
|
||||
/**
|
||||
* Check that item contains only allowed keys
|
||||
*
|
||||
|
@ -113,29 +109,7 @@ function normalizeSidebar(sidebars) {
|
|||
}, {});
|
||||
}
|
||||
|
||||
module.exports = function loadSidebars({siteDir, env, sidebar}) {
|
||||
module.exports = function loadSidebars({sidebar}) {
|
||||
const allSidebars = sidebar;
|
||||
|
||||
// Versioned sidebars.
|
||||
if (idx(env, ['versioning', 'enabled'])) {
|
||||
const versions = idx(env, ['versioning', 'versions']);
|
||||
if (Array.isArray(versions)) {
|
||||
versions.forEach(version => {
|
||||
const versionedSidebarsJSONFile = path.join(
|
||||
siteDir,
|
||||
'versioned_sidebars',
|
||||
`version-${version}-sidebars.json`,
|
||||
);
|
||||
if (fs.existsSync(versionedSidebarsJSONFile)) {
|
||||
const sidebar = require(versionedSidebarsJSONFile); // eslint-disable-line
|
||||
Object.assign(allSidebars, sidebar);
|
||||
} else {
|
||||
const missingFile = path.relative(siteDir, versionedSidebarsJSONFile);
|
||||
throw new Error(`Failed to load ${missingFile}. It does not exist.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return normalizeSidebar(allSidebars);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue