mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 11:07:07 +02:00
feat: Allow modifying docs
url prefix (#914)
* 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
This commit is contained in:
parent
ff22074ff7
commit
61078e38a9
28 changed files with 423 additions and 319 deletions
|
@ -27,6 +27,7 @@ function execute(port) {
|
|||
const feed = require('./feed');
|
||||
const sitemap = require('./sitemap');
|
||||
const routing = require('./routing');
|
||||
const loadConfig = require('./config');
|
||||
const CWD = process.cwd();
|
||||
const join = path.join;
|
||||
const sep = path.sep;
|
||||
|
@ -76,12 +77,9 @@ function execute(port) {
|
|||
}
|
||||
|
||||
function reloadSiteConfig() {
|
||||
removeModuleAndChildrenFromCache(join(CWD, 'siteConfig.js'));
|
||||
siteConfig = require(join(CWD, 'siteConfig.js'));
|
||||
|
||||
if (siteConfig.highlight && siteConfig.highlight.hljs) {
|
||||
siteConfig.highlight.hljs(require('highlight.js'));
|
||||
}
|
||||
const siteConfigPath = join(CWD, 'siteConfig.js');
|
||||
removeModuleAndChildrenFromCache(siteConfigPath);
|
||||
siteConfig = loadConfig(siteConfigPath);
|
||||
}
|
||||
|
||||
function requestFile(url, res, notFoundCallback) {
|
||||
|
@ -109,12 +107,13 @@ function execute(port) {
|
|||
|
||||
const app = express();
|
||||
|
||||
app.get(routing.docs(siteConfig.baseUrl), (req, res, next) => {
|
||||
app.get(routing.docs(siteConfig), (req, res, next) => {
|
||||
const url = decodeURI(req.path.toString().replace(siteConfig.baseUrl, ''));
|
||||
const metadata =
|
||||
Metadata[
|
||||
Object.keys(Metadata).find(id => Metadata[id].permalink === url)
|
||||
];
|
||||
|
||||
const file = docs.getFile(metadata);
|
||||
if (!file) {
|
||||
next();
|
||||
|
@ -122,11 +121,11 @@ function execute(port) {
|
|||
}
|
||||
const rawContent = metadataUtils.extractMetadata(file).rawContent;
|
||||
removeModuleAndChildrenFromCache('../core/DocsLayout.js');
|
||||
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);
|
||||
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig);
|
||||
res.send(docs.getMarkup(rawContent, mdToHtml, metadata));
|
||||
});
|
||||
|
||||
app.get(routing.sitemap(siteConfig.baseUrl), (req, res) => {
|
||||
app.get(routing.sitemap(siteConfig), (req, res) => {
|
||||
sitemap((err, xml) => {
|
||||
if (err) {
|
||||
res.status(500).send('Sitemap error');
|
||||
|
@ -137,7 +136,7 @@ function execute(port) {
|
|||
});
|
||||
});
|
||||
|
||||
app.get(routing.feed(siteConfig.baseUrl), (req, res, next) => {
|
||||
app.get(routing.feed(siteConfig), (req, res, next) => {
|
||||
res.set('Content-Type', 'application/rss+xml');
|
||||
const file = req.path
|
||||
.toString()
|
||||
|
@ -151,7 +150,7 @@ function execute(port) {
|
|||
next();
|
||||
});
|
||||
|
||||
app.get(routing.blog(siteConfig.baseUrl), (req, res, next) => {
|
||||
app.get(routing.blog(siteConfig), (req, res, next) => {
|
||||
// Regenerate the blog metadata in case it has changed. Consider improving
|
||||
// this to regenerate on file save rather than on page request.
|
||||
reloadMetadataBlog();
|
||||
|
@ -177,7 +176,7 @@ function execute(port) {
|
|||
}
|
||||
});
|
||||
|
||||
app.get(routing.page(siteConfig.baseUrl), (req, res, next) => {
|
||||
app.get(routing.page(siteConfig), (req, res, next) => {
|
||||
// Look for user-provided HTML file first.
|
||||
let htmlFile = req.path.toString().replace(siteConfig.baseUrl, '');
|
||||
htmlFile = join(CWD, 'pages', htmlFile);
|
||||
|
@ -232,6 +231,7 @@ function execute(port) {
|
|||
language = parts[i];
|
||||
}
|
||||
}
|
||||
|
||||
let englishFile = join(CWD, 'pages', file);
|
||||
if (language && language !== 'en') {
|
||||
englishFile = englishFile.replace(sep + language + sep, `${sep}en${sep}`);
|
||||
|
@ -272,7 +272,7 @@ function execute(port) {
|
|||
title={ReactComp.title}
|
||||
description={ReactComp.description}
|
||||
metadata={{id: path.basename(userFile, '.js')}}>
|
||||
<ReactComp language={language} />
|
||||
<ReactComp config={siteConfig} language={language} />
|
||||
</Site>,
|
||||
);
|
||||
|
||||
|
@ -339,7 +339,9 @@ function execute(port) {
|
|||
|
||||
// serve static assets from these locations
|
||||
app.use(
|
||||
`${siteConfig.baseUrl}docs/assets`,
|
||||
`${siteConfig.baseUrl}${
|
||||
siteConfig.docsUrl ? `${siteConfig.docsUrl}/` : ''
|
||||
}assets`,
|
||||
express.static(join(CWD, '..', readMetadata.getDocsPath(), 'assets')),
|
||||
);
|
||||
app.use(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue