From 66b2033546a6ad20146d5f81b115d6f080b98e89 Mon Sep 17 00:00:00 2001 From: Endilie Yacop Sucipto Date: Sun, 24 Jun 2018 14:10:32 +0700 Subject: [PATCH] Fix bad routing regex for docs & blogs (#795) * fix bad routing regex for docs & blogs * extract to routing.js & add test * add more test case * address code review * prettier --- lib/core/__tests__/routing.test.js | 62 ++++++++++++++++++++++++++++++ lib/core/routing.js | 20 ++++++++++ lib/server/server.js | 7 ++-- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 lib/core/__tests__/routing.test.js create mode 100644 lib/core/routing.js diff --git a/lib/core/__tests__/routing.test.js b/lib/core/__tests__/routing.test.js new file mode 100644 index 000000000000..05f41c74250f --- /dev/null +++ b/lib/core/__tests__/routing.test.js @@ -0,0 +1,62 @@ +/** + * 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 {docsRouting, blogRouting} = require('../routing'); + +describe('Blog routing', () => { + const blogRegex = blogRouting('/'); + const blogRegex2 = blogRouting('/react/'); + + test('valid blog', () => { + expect('/blog/test.html').toMatch(blogRegex); + expect('/react/blog/test.html').toMatch(blogRegex2); + }); + + test('invalid blog', () => { + expect('/react/blog/test.html').not.toMatch(blogRegex); + expect('/blog/test.html').not.toMatch(blogRegex2); + }); + + test('assets not classified as blog', () => { + expect('/blog/assets/any.png').not.toMatch(blogRegex); + expect('/react/blog/assets/any.png').not.toMatch(blogRegex2); + }); + + test('docs not classified as blog', () => { + expect('/docs/en/blog.html').not.toMatch(blogRegex); + expect('/docs/en/blog/blog.html').not.toMatch(blogRegex); + expect('/react/docs/en/blog.html').not.toMatch(blogRegex2); + expect('/react/docs/en/blog/blog.html').not.toMatch(blogRegex2); + }); +}); + +describe('Docs routing', () => { + const docsRegex = docsRouting('/'); + const docsRegex2 = docsRouting('/reason/'); + + test('valid docs', () => { + expect('/docs/en/test.html').toMatch(docsRegex); + expect('/reason/docs/en/test.html').toMatch(docsRegex2); + }); + + test('invalid docs', () => { + expect('/reason/docs/en/test.html').not.toMatch(docsRegex); + expect('/docs/en/test.html').not.toMatch(docsRegex2); + }); + + test('assets not classified as docs', () => { + expect('/docs/en/notvalid.png').not.toMatch(docsRegex); + expect('/reason/docs/en/notvalid.png').not.toMatch(docsRegex2); + }); + + test('blog not classified as docs', () => { + expect('/blog/docs.html').not.toMatch(docsRegex); + expect('/blog/docs/docs.html').not.toMatch(docsRegex); + expect('/reason/blog/docs.html').not.toMatch(docsRegex2); + expect('/reason/blog/docs/docs.html').not.toMatch(docsRegex2); + }); +}); diff --git a/lib/core/routing.js b/lib/core/routing.js new file mode 100644 index 000000000000..b511ecf78a5d --- /dev/null +++ b/lib/core/routing.js @@ -0,0 +1,20 @@ +/** + * 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 escapeStringRegexp = require('escape-string-regexp'); + +function docsRouting(baseUrl) { + return new RegExp(`^${escapeStringRegexp(baseUrl)}docs\/.*html$`); +} + +function blogRouting(baseUrl) { + return new RegExp(`^${escapeStringRegexp(baseUrl)}blog\/.*html$`); +} + +module.exports = { + docsRouting, + blogRouting, +}; diff --git a/lib/server/server.js b/lib/server/server.js index b4cca1f0e348..9792fd689628 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -20,6 +20,7 @@ function execute(port, options) { const path = require('path'); const color = require('color'); const getTOC = require('../core/getTOC'); + const {docsRouting, blogRouting} = require('../core/routing'); const mkdirp = require('mkdirp'); const glob = require('glob'); const chalk = require('chalk'); @@ -129,10 +130,10 @@ function execute(port, options) { extractTranslations(); reloadSiteConfig(); - // handle all requests for document pages const app = express(); - app.get(/\/docs\/.*html$/, (req, res, next) => { + // handle all requests for document pages + app.get(docsRouting(siteConfig.baseUrl), (req, res, next) => { let url = req.path.toString().replace(siteConfig.baseUrl, ''); // links is a map from a permalink to an id for each document @@ -275,7 +276,7 @@ function execute(port, options) { }); // Handle all requests for blog pages and posts. - app.get(/\/blog\/.*html$/, (req, res) => { + app.get(blogRouting(siteConfig.baseUrl), (req, res) => { // Regenerate the blog metadata in case it has changed. Consider improving // this to regenerate on file save rather than on page request. reloadMetadataBlog();