mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-02 19:57:25 +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
95 lines
2.7 KiB
JavaScript
95 lines
2.7 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.
|
|
*/
|
|
|
|
// split markdown header
|
|
function splitHeader(content) {
|
|
// New line characters need to handle all operating systems.
|
|
const lines = content.split(/\r?\n/);
|
|
if (lines[0] !== '---') {
|
|
return {};
|
|
}
|
|
let i = 1;
|
|
for (; i < lines.length - 1; ++i) {
|
|
if (lines[i] === '---') {
|
|
break;
|
|
}
|
|
}
|
|
return {
|
|
header: lines.slice(1, i + 1).join('\n'),
|
|
content: lines.slice(i + 1).join('\n'),
|
|
};
|
|
}
|
|
|
|
// Extract markdown metadata header
|
|
function extractMetadata(content) {
|
|
const metadata = {};
|
|
const both = splitHeader(content);
|
|
|
|
// if no content returned, then that means there was no header, and both.header is the content
|
|
if (!both.content) {
|
|
if (!both.header) {
|
|
// if no both returned, then that means there was no header and no content => we return the current content of the file
|
|
return {metadata, rawContent: content};
|
|
}
|
|
return {metadata, rawContent: both.header};
|
|
}
|
|
|
|
// New line characters => to handle all operating systems.
|
|
const lines = both.header.split(/\r?\n/);
|
|
|
|
// Loop that add to metadata the current content of the fields of the header
|
|
// Like the format:
|
|
// id:
|
|
// title:
|
|
// original_id:
|
|
for (let i = 0; i < lines.length - 1; ++i) {
|
|
const keyvalue = lines[i].split(':');
|
|
const key = keyvalue[0].trim();
|
|
let value = keyvalue
|
|
.slice(1)
|
|
.join(':')
|
|
.trim();
|
|
try {
|
|
value = JSON.parse(value);
|
|
} catch (err) {
|
|
// Ignore the error as it means it's not a JSON value.
|
|
}
|
|
metadata[key] = value;
|
|
}
|
|
return {metadata, rawContent: both.content};
|
|
}
|
|
|
|
// mdToHtml is a map from a markdown file name to its html link, used to
|
|
// change relative markdown links that work on GitHub into actual site links
|
|
function mdToHtml(Metadata, siteConfig) {
|
|
const {baseUrl, docsUrl} = siteConfig;
|
|
const result = {};
|
|
Object.keys(Metadata).forEach(id => {
|
|
const metadata = Metadata[id];
|
|
if (metadata.language !== 'en' || metadata.original_id) {
|
|
return;
|
|
}
|
|
let htmlLink = baseUrl + metadata.permalink.replace('/next/', '/');
|
|
|
|
const baseDocsPart = `${baseUrl}${docsUrl ? `${docsUrl}/` : ''}`;
|
|
|
|
const i18nDocsRegex = new RegExp(`^${baseDocsPart}en/`);
|
|
const docsRegex = new RegExp(`^${baseDocsPart}`);
|
|
if (i18nDocsRegex.test(htmlLink)) {
|
|
htmlLink = htmlLink.replace(i18nDocsRegex, `${baseDocsPart}en/VERSION/`);
|
|
} else {
|
|
htmlLink = htmlLink.replace(docsRegex, `${baseDocsPart}VERSION/`);
|
|
}
|
|
result[metadata.source] = htmlLink;
|
|
});
|
|
return result;
|
|
}
|
|
|
|
module.exports = {
|
|
extractMetadata,
|
|
mdToHtml,
|
|
};
|