mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 03:12:35 +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
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* 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 createSitemap from '../createSitemap';
|
||||
|
||||
describe('createSitemap', () => {
|
||||
test('simple site', () => {
|
||||
const sitemap = createSitemap({
|
||||
siteConfig: {
|
||||
url: 'https://example.com',
|
||||
},
|
||||
routesPaths: ['/', '/test'],
|
||||
});
|
||||
expect(sitemap).toContain(
|
||||
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">`,
|
||||
);
|
||||
});
|
||||
|
||||
test('empty site', () => {
|
||||
expect(() => {
|
||||
createSitemap({});
|
||||
}).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Url in docusaurus.config.js cannot be empty/undefined"`,
|
||||
);
|
||||
});
|
||||
});
|
|
@ -1,36 +0,0 @@
|
|||
/**
|
||||
* 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 DocusaurusPluginSitemap from '../index';
|
||||
|
||||
describe('docusaurus-plugin-sitemap', () => {
|
||||
describe('createSitemap', () => {
|
||||
test('simple site', async () => {
|
||||
const context = {
|
||||
siteConfig: {
|
||||
url: 'https://example.com',
|
||||
},
|
||||
routesPaths: ['/', '/test'],
|
||||
};
|
||||
const plugin = new DocusaurusPluginSitemap(context, null);
|
||||
const sitemap = await plugin.createSitemap(context);
|
||||
expect(sitemap).toContain(
|
||||
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">`,
|
||||
);
|
||||
});
|
||||
|
||||
test('empty site', async () => {
|
||||
const context = {};
|
||||
const plugin = new DocusaurusPluginSitemap(context, null);
|
||||
expect(
|
||||
plugin.createSitemap(context),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"Url in docusaurus.config.js cannot be empty/undefined"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
33
packages/docusaurus-plugin-sitemap/src/createSitemap.js
Normal file
33
packages/docusaurus-plugin-sitemap/src/createSitemap.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* 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 sitemap = require('sitemap');
|
||||
|
||||
module.exports = function createSitemap({
|
||||
siteConfig = {},
|
||||
routesPaths,
|
||||
options = {},
|
||||
}) {
|
||||
const {url: hostname} = siteConfig;
|
||||
if (!hostname) {
|
||||
throw new Error('Url in docusaurus.config.js cannot be empty/undefined');
|
||||
}
|
||||
|
||||
const urls = routesPaths.map(routesPath => ({
|
||||
url: routesPath,
|
||||
changefreq: options.changefreq,
|
||||
priority: options.priority,
|
||||
}));
|
||||
|
||||
return sitemap
|
||||
.createSitemap({
|
||||
hostname,
|
||||
cacheTime: options.cacheTime,
|
||||
urls,
|
||||
})
|
||||
.toString();
|
||||
};
|
|
@ -6,61 +6,37 @@
|
|||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const sitemap = require('sitemap');
|
||||
const path = require('path');
|
||||
|
||||
const createSitemap = require('./createSitemap');
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
cacheTime: 600 * 1000, // 600 sec - cache purge period
|
||||
changefreq: 'weekly',
|
||||
priority: 0.5,
|
||||
};
|
||||
|
||||
class DocusaurusPluginSitemap {
|
||||
constructor(context, opts) {
|
||||
this.options = {...DEFAULT_OPTIONS, ...opts};
|
||||
this.context = context;
|
||||
}
|
||||
module.exports = function(context, opts) {
|
||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
||||
|
||||
getName() {
|
||||
return 'docusaurus-plugin-sitemap';
|
||||
}
|
||||
return {
|
||||
name: 'docusaurus-plugin-sitemap',
|
||||
|
||||
async createSitemap({siteConfig = {}, routesPaths}) {
|
||||
const {url: hostname} = siteConfig;
|
||||
if (!hostname) {
|
||||
throw new Error(`Url in docusaurus.config.js cannot be empty/undefined`);
|
||||
}
|
||||
async postBuild({siteConfig = {}, routesPaths = [], outDir}) {
|
||||
// Generate sitemap
|
||||
const generatedSitemap = createSitemap({
|
||||
siteConfig,
|
||||
routesPaths,
|
||||
options,
|
||||
}).toString();
|
||||
|
||||
const urls = routesPaths.map(routesPath => ({
|
||||
url: routesPath,
|
||||
changefreq: this.changefreq,
|
||||
priority: this.priority,
|
||||
}));
|
||||
|
||||
return sitemap
|
||||
.createSitemap({
|
||||
hostname,
|
||||
cacheTime: this.cacheTime,
|
||||
urls,
|
||||
})
|
||||
.toString();
|
||||
}
|
||||
|
||||
async postBuild({siteConfig = {}, routesPaths = [], outDir}) {
|
||||
// Generate sitemap
|
||||
const generatedSitemap = await this.createSitemap({
|
||||
siteConfig,
|
||||
routesPaths,
|
||||
});
|
||||
|
||||
// Write sitemap file
|
||||
const sitemapPath = path.join(outDir, 'sitemap.xml');
|
||||
fs.writeFile(sitemapPath, generatedSitemap, err => {
|
||||
if (err) {
|
||||
throw new Error(`Sitemap error: ${err}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DocusaurusPluginSitemap;
|
||||
// Write sitemap file
|
||||
const sitemapPath = path.join(outDir, 'sitemap.xml');
|
||||
fs.writeFile(sitemapPath, generatedSitemap, err => {
|
||||
if (err) {
|
||||
throw new Error(`Sitemap error: ${err}`);
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue