mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 08:37:25 +02:00
Fix & refactor server routing + add tests (#799)
* Fix bad routing regex for sitemap & feed * add tests for sitemap & feed * use next middleware function if file nto found * add pages routing & test * refactor + add more test for page routing * extension-less url routing + test * refactor out requestFile * add dot routing + test to handle special case like http://localhost:3000/blog/2018/05/27/1.13.0 * exit properly * add more test for sitemap * update nits from my phone
This commit is contained in:
parent
e9f290f788
commit
e9eef39760
3 changed files with 231 additions and 37 deletions
|
@ -4,17 +4,47 @@
|
|||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
const escapeStringRegexp = require('escape-string-regexp');
|
||||
|
||||
function docsRouting(baseUrl) {
|
||||
return new RegExp(`^${escapeStringRegexp(baseUrl)}docs\/.*html$`);
|
||||
}
|
||||
const escape = require('escape-string-regexp');
|
||||
|
||||
function blogRouting(baseUrl) {
|
||||
return new RegExp(`^${escapeStringRegexp(baseUrl)}blog\/.*html$`);
|
||||
return new RegExp(`^${escape(baseUrl)}blog\/.*html$`);
|
||||
}
|
||||
|
||||
function docsRouting(baseUrl) {
|
||||
return new RegExp(`^${escape(baseUrl)}docs\/.*html$`);
|
||||
}
|
||||
|
||||
function dotRouting() {
|
||||
return /(?!.*html$)^\/.*\.[^\n\/]+$/;
|
||||
}
|
||||
|
||||
function feedRouting(baseUrl) {
|
||||
return new RegExp(`^${escape(baseUrl)}blog\/(feed\.xml|atom\.xml)$`);
|
||||
}
|
||||
|
||||
function noExtRouting() {
|
||||
return /\/[^\.]*\/?$/;
|
||||
}
|
||||
|
||||
function pageRouting(baseUrl) {
|
||||
const gr = regex => regex.toString().replace(/(^\/|\/$)/gm, '');
|
||||
return new RegExp(
|
||||
`(?!${gr(docsRouting(baseUrl))}|${gr(blogRouting(baseUrl))})^${escape(
|
||||
baseUrl
|
||||
)}.*\.html$`
|
||||
);
|
||||
}
|
||||
|
||||
function sitemapRouting(baseUrl) {
|
||||
return new RegExp(`^${escape(baseUrl)}sitemap.xml$`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
docsRouting,
|
||||
blogRouting,
|
||||
docsRouting,
|
||||
dotRouting,
|
||||
feedRouting,
|
||||
pageRouting,
|
||||
noExtRouting,
|
||||
sitemapRouting,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue