mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-05 13:17:23 +02:00
* Allow other routes than /docs in the URL siteConfig.js has a new mandatory field named *docsRoute* which default value is 'docs' and that can be customized by the user. This change will allow users who uses the library to host guides and tutorials to customize their websites by assign 'docsRoute' values like 'tutorials' or 'guides'. Fixes #879 * Make "docsRoute" field optional * Isolate docsRoute login in getDocsRoute function * Rename docsRoute to docsUrl * Run prettier * Remove old folders * fix: Restore docusaurus reference link * fix: Add `docsUrl` param fallback. Refactor multiple function calls * Fix linting errors * Update description for docsUrl field * Reduce redundant calls to getDocsUrl * Replace a missed use case for `docsUrl` instead of the function call * Move `getDocsUrl` out from `server/routing.js` to `server/utils.js` **Why?** Because `routing.js` is exporting all router RegEx's, and the `getDocsUrl` suffices more as a util * WiP: Align leading slashes and fix routing around `docsUrl` Checklist: - [x] Added `removeDuplicateLeadingSlashes` util to make sure there is only one leading slash - [-] Fix edge cases for routing: - [x] `docsUrl: ''` - [ ] `docsUrl: '/'` - [ ] make it work with languages - [ ] make it work with versioning * Make leading slashes canonical cross routing and generated links This ensures correct routing for customized `baseUrl` and `docsUrl`. - Changed all routing functions to take `siteConfig` instead of `siteConfig.baseUrl` - Updated tests accordingly * Alternative fallback for `docsUrl` * rework/ fix implementation * cleanup * refactor and add docs for config props * fix typo * fix broken url
100 lines
2.6 KiB
JavaScript
100 lines
2.6 KiB
JavaScript
/**
|
|
* 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 fs = require('fs-extra');
|
|
|
|
const glob = require('glob');
|
|
|
|
const CWD = process.cwd();
|
|
|
|
const sitemap = require('sitemap');
|
|
const utils = require('../core/utils');
|
|
|
|
const loadConfig = require('./config');
|
|
|
|
const siteConfig = loadConfig(`${CWD}/siteConfig.js`);
|
|
|
|
const readMetadata = require('./readMetadata.js');
|
|
|
|
readMetadata.generateMetadataDocs();
|
|
const Metadata = require('../core/metadata.js');
|
|
|
|
readMetadata.generateMetadataBlog();
|
|
const MetadataBlog = require('../core/MetadataBlog.js');
|
|
|
|
module.exports = function(callback) {
|
|
console.log('sitemap.js triggered...');
|
|
|
|
const files = glob.sync(`${CWD}/pages/en/**/*.js`);
|
|
|
|
// English-only is the default.
|
|
let enabledLanguages = [
|
|
{
|
|
enabled: true,
|
|
name: 'English',
|
|
tag: 'en',
|
|
},
|
|
];
|
|
|
|
// If we have a languages.js file, get all the enabled languages in there
|
|
if (fs.existsSync(`${CWD}/languages.js`)) {
|
|
const languages = require(`${CWD}/languages.js`);
|
|
enabledLanguages = languages.filter(lang => lang.enabled);
|
|
}
|
|
|
|
// Create a url mapping to all the enabled languages files
|
|
const urls = files.map(file => {
|
|
let url = file.split('/pages/en')[1];
|
|
url = siteConfig.cleanUrl
|
|
? url.replace(/\.js$/, '')
|
|
: url.replace(/\.js$/, '.html');
|
|
const links = enabledLanguages.map(lang => {
|
|
const langUrl = lang.tag + url;
|
|
return {lang: lang.tag, url: langUrl};
|
|
});
|
|
return {url, changefreq: 'weekly', priority: 0.5, links};
|
|
});
|
|
|
|
MetadataBlog.forEach(blog => {
|
|
urls.push({
|
|
url: `/blog/${utils.getPath(blog.path, siteConfig.cleanUrl)}`,
|
|
changefreq: 'weekly',
|
|
priority: 0.3,
|
|
});
|
|
});
|
|
|
|
Object.keys(Metadata)
|
|
.filter(key => Metadata[key].language === 'en')
|
|
.forEach(key => {
|
|
const doc = Metadata[key];
|
|
const docUrl = utils.getPath(doc.permalink, siteConfig.cleanUrl);
|
|
const docsPart = `${siteConfig.docsUrl ? `${siteConfig.docsUrl}/` : ''}`;
|
|
const links = enabledLanguages.map(lang => {
|
|
const langUrl = docUrl.replace(
|
|
new RegExp(`^${docsPart}en/`),
|
|
`${docsPart}${lang.tag}/`,
|
|
);
|
|
return {lang: lang.tag, url: langUrl};
|
|
});
|
|
urls.push({
|
|
url: docUrl,
|
|
changefreq: 'hourly',
|
|
priority: 1.0,
|
|
links,
|
|
});
|
|
});
|
|
|
|
const sm = sitemap.createSitemap({
|
|
hostname: siteConfig.url + siteConfig.baseUrl,
|
|
cacheTime: 600 * 1000, // 600 sec - cache purge period
|
|
urls,
|
|
});
|
|
|
|
sm.toXML((err, xml) => {
|
|
callback(err, xml);
|
|
});
|
|
};
|