mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 17:17:28 +02:00
refactor(v2): change plugin api (#1547)
* misc(v2): new plugin format example * refactor(v2): make all plugins a function returning objects * misc: add CHANGELOG * misc(v2): update CHANGELOG * misc(v2): fix tests * misc(v2): convert swizzle command * misc(v2): convert sitemap back to commonjs
This commit is contained in:
parent
9feb7b2c64
commit
6a814ac64a
18 changed files with 709 additions and 725 deletions
|
@ -7,7 +7,7 @@
|
|||
|
||||
import path from 'path';
|
||||
|
||||
import DocusaurusPluginContentPages from '../index';
|
||||
import pluginContentPages from '../index';
|
||||
|
||||
describe('docusaurus-plugin-content-pages', () => {
|
||||
test('simple pages', async () => {
|
||||
|
@ -17,7 +17,7 @@ describe('docusaurus-plugin-content-pages', () => {
|
|||
url: 'https://docusaurus.io',
|
||||
};
|
||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||
const plugin = new DocusaurusPluginContentPages({
|
||||
const plugin = pluginContentPages({
|
||||
siteDir,
|
||||
siteConfig,
|
||||
});
|
||||
|
|
|
@ -16,75 +16,70 @@ const DEFAULT_OPTIONS = {
|
|||
include: ['**/*.{js,jsx}'], // Extensions to include.
|
||||
};
|
||||
|
||||
class DocusaurusPluginContentPages {
|
||||
constructor(context, opts) {
|
||||
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||
this.context = context;
|
||||
this.contentPath = path.resolve(this.context.siteDir, this.options.path);
|
||||
}
|
||||
module.exports = function(context, opts) {
|
||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
||||
const contentPath = path.resolve(context.siteDir, options.path);
|
||||
|
||||
getName() {
|
||||
return 'docusaurus-plugin-content-pages';
|
||||
}
|
||||
return {
|
||||
name: 'docusaurus-plugin-content-pages',
|
||||
|
||||
getPathsToWatch() {
|
||||
const {include = []} = this.options;
|
||||
const globPattern = include.map(
|
||||
pattern => `${this.contentPath}/${pattern}`,
|
||||
);
|
||||
return [...globPattern];
|
||||
}
|
||||
contentPath,
|
||||
|
||||
async loadContent() {
|
||||
const {include} = this.options;
|
||||
const {siteConfig} = this.context;
|
||||
const pagesDir = this.contentPath;
|
||||
getPathsToWatch() {
|
||||
const {include = []} = options;
|
||||
const globPattern = include.map(pattern => `${contentPath}/${pattern}`);
|
||||
return [...globPattern];
|
||||
},
|
||||
|
||||
if (!fs.existsSync(pagesDir)) {
|
||||
return null;
|
||||
}
|
||||
async loadContent() {
|
||||
const {include} = options;
|
||||
const {siteConfig} = context;
|
||||
const pagesDir = contentPath;
|
||||
|
||||
const {baseUrl} = siteConfig;
|
||||
const pagesFiles = await globby(include, {
|
||||
cwd: pagesDir,
|
||||
});
|
||||
if (!fs.existsSync(pagesDir)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return pagesFiles.map(relativeSource => {
|
||||
const source = path.join(pagesDir, relativeSource);
|
||||
const pathName = encodePath(fileToPath(relativeSource));
|
||||
// Default Language.
|
||||
return {
|
||||
permalink: pathName.replace(/^\//, baseUrl),
|
||||
source,
|
||||
};
|
||||
});
|
||||
}
|
||||
const {baseUrl} = siteConfig;
|
||||
const pagesFiles = await globby(include, {
|
||||
cwd: pagesDir,
|
||||
});
|
||||
|
||||
async contentLoaded({content, actions}) {
|
||||
if (!content) {
|
||||
return;
|
||||
}
|
||||
return pagesFiles.map(relativeSource => {
|
||||
const source = path.join(pagesDir, relativeSource);
|
||||
const pathName = encodePath(fileToPath(relativeSource));
|
||||
// Default Language.
|
||||
return {
|
||||
permalink: pathName.replace(/^\//, baseUrl),
|
||||
source,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
const {addRoute, createData} = actions;
|
||||
async contentLoaded({content, actions}) {
|
||||
if (!content) {
|
||||
return;
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
content.map(async metadataItem => {
|
||||
const {permalink, source} = metadataItem;
|
||||
const metadataPath = await createData(
|
||||
`${docuHash(permalink)}.json`,
|
||||
JSON.stringify(metadataItem, null, 2),
|
||||
);
|
||||
addRoute({
|
||||
path: permalink,
|
||||
component: source,
|
||||
exact: true,
|
||||
modules: {
|
||||
metadata: metadataPath,
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
const {addRoute, createData} = actions;
|
||||
|
||||
module.exports = DocusaurusPluginContentPages;
|
||||
await Promise.all(
|
||||
content.map(async metadataItem => {
|
||||
const {permalink, source} = metadataItem;
|
||||
const metadataPath = await createData(
|
||||
`${docuHash(permalink)}.json`,
|
||||
JSON.stringify(metadataItem, null, 2),
|
||||
);
|
||||
addRoute({
|
||||
path: permalink,
|
||||
component: source,
|
||||
exact: true,
|
||||
modules: {
|
||||
metadata: metadataPath,
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue