mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
misc(v2): make packages dir more consistent
This commit is contained in:
parent
029aa636a8
commit
4ac4a7c671
16 changed files with 15 additions and 15 deletions
|
@ -0,0 +1,117 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`loadSidebars normal site with 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",
|
||||
},
|
||||
],
|
||||
}
|
||||
`;
|
||||
|
||||
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 {}`;
|
|
@ -0,0 +1,269 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import '@babel/polyfill';
|
||||
import path from 'path';
|
||||
import loadSetup from '../../../docusaurus/test/loadSetup';
|
||||
import DocusaurusPluginContentDocs from '../index';
|
||||
|
||||
describe('loadDocs', () => {
|
||||
test('simple website', async () => {
|
||||
const {env, siteDir, siteConfig} = await loadSetup('simple');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata} = await plugin.loadContent();
|
||||
const docsDir = plugin.contentPath;
|
||||
|
||||
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, versionedDir} = await loadSetup(
|
||||
'versioned',
|
||||
);
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata} = 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,
|
||||
translatedDir,
|
||||
versionedDir,
|
||||
} = await loadSetup('transversioned');
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata} = 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, translatedDir} = await loadSetup(
|
||||
'translated',
|
||||
);
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata} = 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, versionedDir} = await loadSetup(
|
||||
'versioned',
|
||||
);
|
||||
const sidebarPath = path.join(siteDir, 'sidebars.json');
|
||||
const plugin = new DocusaurusPluginContentDocs(
|
||||
{
|
||||
path: '../docs',
|
||||
sidebarPath,
|
||||
},
|
||||
{
|
||||
cliOptions: {
|
||||
skipNextRelease: true,
|
||||
},
|
||||
env,
|
||||
siteDir,
|
||||
siteConfig,
|
||||
},
|
||||
);
|
||||
const {docs: docsMetadata} = 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();
|
||||
});
|
||||
});
|
|
@ -0,0 +1,381 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import '@babel/polyfill';
|
||||
import path from 'path';
|
||||
import processMetadata from '../metadata';
|
||||
import loadSetup from '../../../docusaurus/test/loadSetup';
|
||||
|
||||
describe('processMetadata', () => {
|
||||
test('normal docs', async () => {
|
||||
const props = await loadSetup('simple');
|
||||
const {docsDir, env, siteConfig} = props;
|
||||
const sourceA = path.join('foo', 'bar.md');
|
||||
const sourceB = path.join('hello.md');
|
||||
const dataA = await processMetadata(
|
||||
sourceA,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
siteConfig,
|
||||
'docs',
|
||||
);
|
||||
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 {docsDir, env, siteConfig} = props;
|
||||
const source = path.join('permalink.md');
|
||||
const data = await processMetadata(
|
||||
source,
|
||||
docsDir,
|
||||
env,
|
||||
{},
|
||||
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, docsDir, env, siteConfig} = props;
|
||||
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 {docsDir, translatedDir, versionedDir, env, siteConfig} = props;
|
||||
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 {docsDir, translatedDir, env, siteConfig} = props;
|
||||
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,
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,260 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import createOrder from '../order';
|
||||
|
||||
describe('createOrder', () => {
|
||||
test('multiple sidebars with subcategory', () => {
|
||||
const result = createOrder({
|
||||
docs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Subcategory 1',
|
||||
items: [{type: 'doc', id: 'doc1'}],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Subcategory 2',
|
||||
items: [{type: 'doc', id: 'doc2'}],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category2',
|
||||
items: [{type: 'doc', id: 'doc3'}, {type: 'doc', id: 'doc4'}],
|
||||
},
|
||||
],
|
||||
otherDocs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [{type: 'doc', id: 'doc5'}],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result).toEqual({
|
||||
doc1: {
|
||||
category: 'Category1',
|
||||
subCategory: 'Subcategory 1',
|
||||
next: 'doc2',
|
||||
previous: undefined,
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc2: {
|
||||
category: 'Category1',
|
||||
subCategory: 'Subcategory 2',
|
||||
next: 'doc3',
|
||||
previous: 'doc1',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc3: {
|
||||
category: 'Category2',
|
||||
subCategory: undefined,
|
||||
next: 'doc4',
|
||||
previous: 'doc2',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc4: {
|
||||
category: 'Category2',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: 'doc3',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc5: {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: undefined,
|
||||
sidebar: 'otherDocs',
|
||||
},
|
||||
});
|
||||
});
|
||||
test('multiple sidebars without subcategory', () => {
|
||||
const result = createOrder({
|
||||
docs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [{type: 'doc', id: 'doc1'}, {type: 'doc', id: 'doc2'}],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category2',
|
||||
items: [{type: 'doc', id: 'doc3'}, {type: 'doc', id: 'doc4'}],
|
||||
},
|
||||
],
|
||||
otherDocs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [{type: 'doc', id: 'doc5'}],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result).toEqual({
|
||||
doc1: {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: 'doc2',
|
||||
previous: undefined,
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc2: {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: 'doc3',
|
||||
previous: 'doc1',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc3: {
|
||||
category: 'Category2',
|
||||
subCategory: undefined,
|
||||
next: 'doc4',
|
||||
previous: 'doc2',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc4: {
|
||||
category: 'Category2',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: 'doc3',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc5: {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: undefined,
|
||||
sidebar: 'otherDocs',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('versioned sidebars', () => {
|
||||
const result = createOrder({
|
||||
docs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [{type: 'doc', id: 'doc1'}],
|
||||
},
|
||||
],
|
||||
'version-1.2.3-docs': [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [{type: 'doc', id: 'version-1.2.3-doc2'}],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category2',
|
||||
items: [{type: 'doc', id: 'version-1.2.3-doc1'}],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result).toEqual({
|
||||
doc1: {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: undefined,
|
||||
sidebar: 'docs',
|
||||
},
|
||||
'version-1.2.3-doc1': {
|
||||
category: 'Category2',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: 'version-1.2.3-doc2',
|
||||
sidebar: 'version-1.2.3-docs',
|
||||
},
|
||||
'version-1.2.3-doc2': {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: 'version-1.2.3-doc1',
|
||||
previous: undefined,
|
||||
sidebar: 'version-1.2.3-docs',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('multiple sidebars with subcategories, refs and external links', () => {
|
||||
const result = createOrder({
|
||||
docs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Subcategory 1',
|
||||
items: [{type: 'link', href: '//example.com', label: 'bar'}],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Subcategory 2',
|
||||
items: [{type: 'doc', id: 'doc2'}],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Subcategory 1',
|
||||
items: [{type: 'link', href: '//example2.com', label: 'baz'}],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category2',
|
||||
items: [{type: 'doc', id: 'doc3'}, {type: 'ref', id: 'doc4'}],
|
||||
},
|
||||
],
|
||||
otherDocs: [
|
||||
{
|
||||
type: 'category',
|
||||
label: 'Category1',
|
||||
items: [{type: 'doc', id: 'doc5'}],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result).toEqual({
|
||||
doc2: {
|
||||
category: 'Category1',
|
||||
subCategory: 'Subcategory 2',
|
||||
next: 'doc3',
|
||||
previous: undefined,
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc3: {
|
||||
category: 'Category2',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: 'doc2',
|
||||
sidebar: 'docs',
|
||||
},
|
||||
doc5: {
|
||||
category: 'Category1',
|
||||
subCategory: undefined,
|
||||
next: undefined,
|
||||
previous: undefined,
|
||||
sidebar: 'otherDocs',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('edge cases', () => {
|
||||
expect(createOrder({})).toEqual({});
|
||||
expect(createOrder(undefined)).toEqual({});
|
||||
expect(() => createOrder(null)).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Cannot convert undefined or null to object"`,
|
||||
);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import loadSidebars from '../sidebars';
|
||||
import loadSetup from '../../../docusaurus/test/loadSetup';
|
||||
|
||||
/* eslint-disable global-require, import/no-dynamic-require */
|
||||
|
||||
describe('loadSidebars', () => {
|
||||
const fixtures = path.join(__dirname, '..', '__fixtures__');
|
||||
|
||||
test('normal site with sidebars', async () => {
|
||||
const {env, siteDir} = await loadSetup('simple');
|
||||
const sidebar = require(path.join(siteDir, 'sidebars.json'));
|
||||
const result = loadSidebars({siteDir, env, sidebar});
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('site without sidebars', () => {
|
||||
const env = {};
|
||||
const siteDir = path.join(fixtures, 'bad-site');
|
||||
const result = loadSidebars({siteDir, env, 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."`,
|
||||
);
|
||||
});
|
||||
});
|
213
packages/docusaurus-plugin-content-docs/src/index.js
Normal file
213
packages/docusaurus-plugin-content-docs/src/index.js
Normal file
|
@ -0,0 +1,213 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const globby = require('globby');
|
||||
const importFresh = require('import-fresh');
|
||||
const path = require('path');
|
||||
const {getSubFolder, idx, normalizeUrl} = require('@docusaurus/utils');
|
||||
|
||||
const createOrder = require('./order');
|
||||
const loadSidebars = require('./sidebars');
|
||||
const processMetadata = require('./metadata');
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
metadataKey: 'docsMetadata',
|
||||
metadataFileName: 'docsMetadata.json',
|
||||
path: 'docs', // Path to data on filesystem, relative to site dir.
|
||||
routeBasePath: 'docs', // URL Route.
|
||||
include: ['**/*.md', '**/*.mdx'], // Extensions to include.
|
||||
// TODO: Change format to array.
|
||||
sidebarPath: '', // Path to sidebar configuration for showing a list of markdown pages.
|
||||
// TODO: Settle themeing.
|
||||
docLayoutComponent: '@theme/Doc',
|
||||
docItemComponent: '@theme/DocBody',
|
||||
};
|
||||
|
||||
class DocusaurusPluginContentDocs {
|
||||
constructor(opts, context) {
|
||||
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||
this.context = context;
|
||||
this.contentPath = path.resolve(this.context.siteDir, this.options.path);
|
||||
}
|
||||
|
||||
getName() {
|
||||
return 'docusaurus-plugin-content-docs';
|
||||
}
|
||||
|
||||
getPathsToWatch() {
|
||||
return [this.contentPath, this.options.sidebarPath];
|
||||
}
|
||||
|
||||
// Fetches blog contents and returns metadata for the contents.
|
||||
async loadContent() {
|
||||
const {include, routeBasePath, sidebarPath} = this.options;
|
||||
const {siteDir, env, siteConfig, cliOptions = {}} = this.context;
|
||||
const {skipNextRelease} = cliOptions;
|
||||
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});
|
||||
|
||||
// @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.
|
||||
if (translationEnabled) {
|
||||
const 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.
|
||||
if (versioningEnabled) {
|
||||
const 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;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Get the titles of the previous and next ids so that we can use them.
|
||||
Object.keys(docs).forEach(currentID => {
|
||||
const previousID = idx(docs, [currentID, 'previous']);
|
||||
if (previousID) {
|
||||
const previousTitle = idx(docs, [previousID, 'title']);
|
||||
docs[currentID].previous_title = previousTitle || 'Previous';
|
||||
}
|
||||
const nextID = idx(docs, [currentID, 'next']);
|
||||
if (nextID) {
|
||||
const nextTitle = idx(docs, [nextID, 'title']);
|
||||
docs[currentID].next_title = nextTitle || 'Next';
|
||||
}
|
||||
});
|
||||
|
||||
// Create source to metadata mapping.
|
||||
const sourceToMetadata = {};
|
||||
Object.values(docs).forEach(({source, version, permalink, language}) => {
|
||||
sourceToMetadata[source] = {
|
||||
version,
|
||||
permalink,
|
||||
language,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
docs,
|
||||
docsDir,
|
||||
docsSidebars,
|
||||
sourceToMetadata,
|
||||
};
|
||||
}
|
||||
|
||||
async contentLoaded({content, actions}) {
|
||||
const {docLayoutComponent, docItemComponent, routeBasePath} = this.options;
|
||||
const {addRoute} = actions;
|
||||
|
||||
addRoute({
|
||||
path: normalizeUrl([this.context.siteConfig.baseUrl, routeBasePath]),
|
||||
component: docLayoutComponent,
|
||||
routes: Object.values(content.docs).map(metadataItem => ({
|
||||
path: metadataItem.permalink,
|
||||
component: docItemComponent,
|
||||
metadata: metadataItem,
|
||||
modules: [metadataItem.source],
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DocusaurusPluginContentDocs;
|
Loading…
Add table
Add a link
Reference in a new issue