misc(v2): make packages dir more consistent

This commit is contained in:
Yangshun Tay 2019-04-01 22:18:44 -07:00
parent 029aa636a8
commit 4ac4a7c671
16 changed files with 15 additions and 15 deletions

View file

@ -0,0 +1,125 @@
/**
* 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 loadSetup from '../../../docusaurus/test/loadSetup';
import DocusaurusPluginContentPages from '../index';
describe('docusaurus-plugin-content-pages', () => {
describe('loadContent', () => {
test.each([
[
'simple',
pagesDir => [
{
permalink: '/',
source: path.join(pagesDir, 'index.js'),
},
{
permalink: '/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
],
],
[
'versioned',
pagesDir => [
{
permalink: '/',
source: path.join(pagesDir, 'index.js'),
},
{
permalink: '/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
],
],
[
'translated',
pagesDir => [
{
language: 'en',
permalink: '/',
source: path.join(pagesDir, 'index.js'),
},
{
language: 'en',
permalink: '/en/',
source: path.join(pagesDir, 'index.js'),
},
{
language: 'ko',
permalink: '/ko/',
source: path.join(pagesDir, 'index.js'),
},
{
language: 'en',
permalink: '/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
{
language: 'en',
permalink: '/en/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
{
language: 'ko',
permalink: '/ko/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
],
[
'transversioned',
pagesDir => [
{
language: 'en',
permalink: '/',
source: path.join(pagesDir, 'index.js'),
},
{
language: 'en',
permalink: '/en/',
source: path.join(pagesDir, 'index.js'),
},
{
language: 'ko',
permalink: '/ko/',
source: path.join(pagesDir, 'index.js'),
},
{
language: 'en',
permalink: '/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
{
language: 'en',
permalink: '/en/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
{
language: 'ko',
permalink: '/ko/hello/world',
source: path.join(pagesDir, 'hello', 'world.js'),
},
],
],
],
])('%s website', async (type, expected) => {
const {env, siteDir, siteConfig} = await loadSetup(type);
const plugin = new DocusaurusPluginContentPages(null, {
env,
siteDir,
siteConfig,
});
const pagesMetadatas = await plugin.loadContent();
const pagesDir = plugin.contentPath;
expect(pagesMetadatas).toEqual(expected(pagesDir));
});
});
});

View file

@ -0,0 +1,109 @@
/**
* 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 path = require('path');
const {encodePath, fileToPath, idx} = require('@docusaurus/utils');
const DEFAULT_OPTIONS = {
metadataKey: 'pagesMetadata',
metadataFileName: 'pagesMetadata.json',
path: 'pages', // Path to data on filesystem, relative to site dir.
routeBasePath: '', // URL Route.
include: ['**/*.{js,jsx}'], // Extensions to include.
component: '@theme/Pages',
};
class DocusaurusPluginContentPages {
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-pages';
}
getPathsToWatch() {
return [this.contentPath];
}
async loadContent() {
const {include} = this.options;
const {env, siteConfig} = this.context;
const pagesDir = this.contentPath;
const {baseUrl} = siteConfig;
const pagesFiles = await globby(include, {
cwd: pagesDir,
});
// Prepare metadata container.
const pagesMetadatas = [];
// Translation.
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']);
await Promise.all(
pagesFiles.map(async relativeSource => {
const source = path.join(pagesDir, relativeSource);
const pathName = encodePath(fileToPath(relativeSource));
if (translationEnabled && enabledLangTags.length > 0) {
enabledLangTags.forEach(langTag => {
// Default lang should also be available. E.g: /en/users and /users is the same.
if (langTag === defaultLangTag) {
pagesMetadatas.push({
permalink: pathName.replace(/^\//, baseUrl),
language: langTag,
source,
});
}
const metadata = {
permalink: pathName.replace(/^\//, `${baseUrl}${langTag}/`),
language: langTag,
source,
};
pagesMetadatas.push(metadata);
});
} else {
// Default Language.
const metadata = {
permalink: pathName.replace(/^\//, baseUrl),
source,
};
pagesMetadatas.push(metadata);
}
}),
);
return pagesMetadatas;
}
async contentLoaded({content, actions}) {
const {component} = this.options;
const {addRoute} = actions;
content.forEach(metadataItem => {
const {permalink, source} = metadataItem;
addRoute({
path: permalink,
component,
metadata: metadataItem,
modules: [source],
});
});
}
}
module.exports = DocusaurusPluginContentPages;