docusaurus/lib/server/server.js

552 lines
17 KiB
JavaScript
Raw Normal View History

2017-07-07 13:28:29 -04:00
/**
* 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.
2017-07-07 13:28:29 -04:00
*/
function execute(port) {
const extractTranslations = require('../write-translations');
const env = require('./env.js');
const translation = require('./translation');
const express = require('express');
const React = require('react');
const request = require('request');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const color = require('color');
const toSlug = require('../core/toSlug');
const mkdirp = require('mkdirp');
const glob = require('glob');
const chalk = require('chalk');
const translate = require('./translate');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
const feed = require('./feed');
const sitemap = require('./sitemap');
2017-08-02 18:13:10 -04:00
const CWD = process.cwd();
2017-07-07 13:28:29 -04:00
const join = path.join;
const sep = path.sep;
2017-08-11 14:39:04 -04:00
// remove a module and child modules from require cache, so server does not have
// to be restarted
function removeModuleAndChildrenFromCache(moduleName) {
2017-07-07 13:28:29 -04:00
let mod = require.resolve(moduleName);
2017-08-11 14:39:04 -04:00
if (mod && (mod = require.cache[mod])) {
mod.children.forEach(child => {
delete require.cache[child.id];
removeModulePathFromCache(mod.id);
});
delete require.cache[mod.id];
removeModulePathFromCache(mod.id);
2017-07-07 13:28:29 -04:00
}
}
2017-08-11 14:39:04 -04:00
function removeModulePathFromCache(moduleName) {
2017-08-11 14:39:04 -04:00
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
if (cacheKey.indexOf(moduleName) > 0) {
delete module.constructor._pathCache[cacheKey];
}
});
2017-07-10 19:38:35 -04:00
}
2017-07-07 13:28:29 -04:00
/****************************************************************************/
let readMetadata = require('./readMetadata.js');
2017-07-07 13:28:29 -04:00
let Metadata;
let MetadataBlog;
let siteConfig;
2017-07-07 13:28:29 -04:00
2017-07-25 19:30:49 -04:00
function reloadMetadata() {
removeModuleAndChildrenFromCache('./readMetadata.js');
readMetadata.generateMetadataDocs();
removeModuleAndChildrenFromCache('../core/metadata.js');
Metadata = require('../core/metadata.js');
2017-07-07 13:28:29 -04:00
}
function reloadMetadataBlog() {
if (fs.existsSync(join(__dirname, '..', 'core', 'MetadataBlog.js'))) {
removeModuleAndChildrenFromCache(join('..', 'core', 'MetadataBlog.js'));
fs.removeSync(join(__dirname, '..', 'core', 'MetadataBlog.js'));
}
readMetadata.generateMetadataBlog();
MetadataBlog = require(join('..', 'core', 'MetadataBlog.js'));
}
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'));
}
}
2017-07-07 13:28:29 -04:00
/****************************************************************************/
const TABLE_OF_CONTENTS_TOKEN = '<AUTOGENERATED_TABLE_OF_CONTENTS>';
2017-07-07 13:28:29 -04:00
const insertTableOfContents = rawContent => {
const regexp = /\n###\s+(`.*`.*)\n/g;
let match;
const headers = [];
while ((match = regexp.exec(rawContent))) {
headers.push(match[1]);
}
const tableOfContents = headers
.map(header => ` - [${header}](#${toSlug(header)})`)
.join('\n');
2017-07-07 13:28:29 -04:00
return rawContent.replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents);
};
/****************************************************************************/
function isSeparateCss(file) {
if (!siteConfig.separateCss) {
return false;
}
for (let i = 0; i < siteConfig.separateCss.length; i++) {
if (file.includes(siteConfig.separateCss[i])) {
return true;
}
}
return false;
}
/****************************************************************************/
2017-07-25 19:30:49 -04:00
reloadMetadata();
reloadMetadataBlog();
extractTranslations();
reloadSiteConfig();
2017-07-07 13:28:29 -04:00
2017-08-15 19:55:38 -04:00
// handle all requests for document pages
const app = express();
2017-08-03 13:25:01 -04:00
app.get(/docs\/.*html$/, (req, res, next) => {
let url = req.path.toString().replace(siteConfig.baseUrl, '');
2017-08-03 13:25:01 -04:00
2017-08-15 19:55:38 -04:00
// links is a map from a permalink to an id for each document
2017-07-10 19:38:35 -04:00
let links = {};
2017-08-03 13:25:01 -04:00
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
links[metadata.permalink] = id;
});
2017-08-15 19:55:38 -04:00
// 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
2017-08-03 13:25:01 -04:00
const mdToHtml = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
if (metadata.language !== 'en' || metadata.original_id) {
2017-08-03 13:25:01 -04:00
return;
}
let htmlLink =
siteConfig.baseUrl + metadata.permalink.replace('/next/', '/');
if (htmlLink.includes('/docs/en/')) {
htmlLink = htmlLink.replace('/docs/en/', '/docs/en/VERSION/');
2017-08-03 13:25:01 -04:00
} else {
htmlLink = htmlLink.replace('/docs/', '/docs/VERSION/');
2017-08-03 13:25:01 -04:00
}
mdToHtml[metadata.source] = htmlLink;
});
const metadata = Metadata[links[url]];
2017-08-09 18:43:30 -04:00
if (!metadata) {
next();
return;
}
2017-08-03 13:25:01 -04:00
const language = metadata.language;
2017-08-15 19:55:38 -04:00
// determine what file to use according to its id
2017-08-03 13:25:01 -04:00
let file;
if (metadata.original_id) {
if (env.translation.enabled && metadata.language !== 'en') {
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
2017-08-03 13:25:01 -04:00
} else {
file = join(CWD, 'versioned_docs', metadata.source);
2017-08-03 13:25:01 -04:00
}
} else {
if (!env.translation.enabled || metadata.language === 'en') {
file = join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
2017-07-31 19:19:02 -04:00
} else {
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
2017-07-31 19:19:02 -04:00
}
2017-07-10 19:38:35 -04:00
}
2017-08-02 18:13:10 -04:00
2017-07-18 19:52:47 -04:00
if (!fs.existsSync(file)) {
next();
2017-07-19 13:50:32 -04:00
return;
2017-07-18 19:52:47 -04:00
}
2017-07-07 13:28:29 -04:00
let rawContent = readMetadata.extractMetadata(fs.readFileSync(file, 'utf8'))
2017-08-03 13:25:01 -04:00
.rawContent;
2017-07-07 13:28:29 -04:00
2017-08-15 19:55:38 -04:00
// generate table of contents if appropriate
2017-07-10 19:38:35 -04:00
if (rawContent && rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) !== -1) {
rawContent = insertTableOfContents(rawContent);
}
2017-07-07 13:28:29 -04:00
let latestVersion = env.versioning.latestVersion;
2017-08-15 19:55:38 -04:00
// replace any links to markdown files to their website html links
2017-07-10 19:38:35 -04:00
Object.keys(mdToHtml).forEach(function(key, index) {
2017-08-03 13:25:01 -04:00
let link = mdToHtml[key];
link = link.replace('/en/', '/' + language + '/');
2017-08-03 13:25:01 -04:00
link = link.replace(
'/VERSION/',
2017-08-08 18:44:51 -04:00
metadata.version && metadata.version !== latestVersion
? '/' + metadata.version + '/'
: '/'
2017-07-31 19:19:02 -04:00
);
2017-08-16 19:17:33 -04:00
// replace relative links without "./"
2017-08-08 18:44:51 -04:00
rawContent = rawContent.replace(
new RegExp('\\]\\(' + key, 'g'),
'](' + link
2017-08-08 18:44:51 -04:00
);
2017-08-16 19:17:33 -04:00
// replace relative links with "./"
rawContent = rawContent.replace(
new RegExp('\\]\\(\\./' + key, 'g'),
'](' + link
2017-08-16 19:17:33 -04:00
);
2017-07-10 19:38:35 -04:00
});
2017-07-07 13:28:29 -04:00
2017-08-15 19:55:38 -04:00
// replace any relative links to static assets to absolute links
2017-07-31 19:19:02 -04:00
rawContent = rawContent.replace(
/\]\(assets\//g,
'](' + siteConfig.baseUrl + 'docs/assets/'
2017-07-31 19:19:02 -04:00
);
removeModuleAndChildrenFromCache('../core/DocsLayout.js');
const DocsLayout = require('../core/DocsLayout.js');
let Doc;
if (
metadata.layout &&
siteConfig.layouts &&
siteConfig.layouts[metadata.layout]
) {
Doc = siteConfig.layouts[metadata.layout]({
React,
MarkdownBlock: require('../core/MarkdownBlock.js'),
});
}
2017-07-10 19:38:35 -04:00
const docComp = (
<DocsLayout
metadata={metadata}
language={language}
config={siteConfig}
Doc={Doc}>
2017-07-10 19:38:35 -04:00
{rawContent}
</DocsLayout>
);
2017-07-07 13:28:29 -04:00
res.send(renderToStaticMarkupWithDoctype(docComp));
2017-07-10 19:38:35 -04:00
});
2017-08-03 13:25:01 -04:00
app.get('/sitemap.xml', function(req, res) {
res.set('Content-Type', 'application/xml');
sitemap(xml => {
res.send(xml);
});
});
app.get(/blog\/.*xml$/, (req, res) => {
res.set('Content-Type', 'application/rss+xml');
let parts = req.path.toString().split('blog/');
if (parts[1].toLowerCase() == 'atom.xml') {
res.send(feed('atom'));
return;
}
res.send(feed('rss'));
});
// Handle all requests for blog pages and posts.
2017-07-31 19:19:02 -04:00
app.get(/blog\/.*html$/, (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();
// Generate all of the blog pages.
removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPageLayout.js'));
const BlogPageLayout = require(join('..', 'core', 'BlogPageLayout.js'));
2017-07-07 13:28:29 -04:00
const blogPages = {};
// Make blog pages with 10 posts per page.
2017-07-07 13:28:29 -04:00
const perPage = 10;
for (
let page = 0;
page < Math.ceil(MetadataBlog.length / perPage);
page++
) {
let language = 'en';
const metadata = {page: page, perPage: perPage};
2017-07-10 19:38:35 -04:00
const blogPageComp = (
<BlogPageLayout
metadata={metadata}
language={language}
config={siteConfig}
/>
);
const str = renderToStaticMarkupWithDoctype(blogPageComp);
2017-07-07 13:28:29 -04:00
let path = (page > 0 ? 'page' + (page + 1) : '') + '/index.html';
2017-07-07 13:28:29 -04:00
blogPages[path] = str;
}
let parts = req.path.toString().split('blog/');
2017-07-07 13:28:29 -04:00
// send corresponding blog page if appropriate
if (parts[1] === 'index.html') {
res.send(blogPages['/index.html']);
} else if (parts[1].endsWith('/index.html')) {
2017-07-07 13:28:29 -04:00
res.send(blogPages[parts[1]]);
2017-07-10 19:38:35 -04:00
} else if (parts[1].match(/page([0-9]+)/)) {
if (parts[1].endsWith('/')) {
res.send(blogPages[parts[1] + 'index.html']);
2017-07-07 13:28:29 -04:00
} else {
res.send(blogPages[parts[1] + '/index.html']);
2017-07-07 13:28:29 -04:00
}
2017-07-10 19:38:35 -04:00
} else {
// else send corresponding blog post
2017-07-07 13:28:29 -04:00
let file = parts[1];
file = file.replace(/\.html$/, '.md');
file = file.replace(new RegExp('/', 'g'), '-');
file = join(CWD, 'blog', file);
2017-07-07 13:28:29 -04:00
2017-07-10 19:38:35 -04:00
const result = readMetadata.extractMetadata(
fs.readFileSync(file, {encoding: 'utf8'})
2017-07-10 19:38:35 -04:00
);
2017-07-31 19:19:02 -04:00
let rawContent = result.rawContent;
rawContent = rawContent.replace(
/\]\(assets\//g,
'](' + siteConfig.baseUrl + 'blog/assets/'
2017-07-31 19:19:02 -04:00
);
2017-07-07 13:28:29 -04:00
const metadata = Object.assign(
{path: req.path.toString().split('blog/')[1], content: rawContent},
2017-07-07 13:28:29 -04:00
result.metadata
);
metadata.id = metadata.title;
let language = 'en';
removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js'));
const BlogPostLayout = require(join('..', 'core', 'BlogPostLayout.js'));
2017-07-10 19:38:35 -04:00
const blogPostComp = (
<BlogPostLayout
metadata={metadata}
language={language}
config={siteConfig}>
2017-07-10 19:38:35 -04:00
{rawContent}
</BlogPostLayout>
);
res.send(renderToStaticMarkupWithDoctype(blogPostComp));
2017-07-07 13:28:29 -04:00
}
});
2017-08-15 19:55:38 -04:00
// handle all other main pages
app.get('*.html', (req, res, next) => {
2017-08-15 19:55:38 -04:00
// look for user provided html file first
let htmlFile = req.path.toString().replace(siteConfig.baseUrl, '');
htmlFile = join(CWD, 'pages', htmlFile);
2017-07-10 19:38:35 -04:00
if (
fs.existsSync(htmlFile) ||
fs.existsSync(
(htmlFile = htmlFile.replace(
path.basename(htmlFile),
join('en', path.basename(htmlFile))
2017-07-10 19:38:35 -04:00
))
)
) {
if (siteConfig.wrapPagesHTML) {
removeModuleAndChildrenFromCache(join('..', 'core', 'Site.js'));
const Site = require(join('..', 'core', 'Site.js'));
const str = renderToStaticMarkupWithDoctype(
<Site
language="en"
config={siteConfig}
metadata={{id: path.basename(htmlFile, '.html')}}>
<div
dangerouslySetInnerHTML={{
__html: fs.readFileSync(htmlFile, {encoding: 'utf8'}),
}}
/>
</Site>
);
res.send(str);
} else {
res.send(fs.readFileSync(htmlFile, {encoding: 'utf8'}));
}
2017-07-07 13:28:29 -04:00
return;
}
2017-08-15 19:55:38 -04:00
// look for user provided react file either in specified path or in path for english files
let file = req.path.toString().replace(/\.html$/, '.js');
file = file.replace(siteConfig.baseUrl, '');
let userFile = join(CWD, 'pages', file);
2017-07-07 13:28:29 -04:00
let language = env.translation.enabled ? 'en' : '';
2017-07-07 13:28:29 -04:00
const regexLang = /(.*)\/.*\.html$/;
const match = regexLang.exec(req.path);
const parts = match[1].split('/');
const enabledLangTags = env.translation
.enabledLanguages()
.map(lang => lang.tag);
2017-07-07 13:28:29 -04:00
for (let i = 0; i < parts.length; i++) {
if (enabledLangTags.indexOf(parts[i]) !== -1) {
language = parts[i];
}
}
let englishFile = join(CWD, 'pages', file);
if (language && language !== 'en') {
englishFile = englishFile.replace(sep + language + sep, sep + 'en' + sep);
}
// check for: a file for the page, an english file for page with unspecified language, or an
2017-08-15 19:55:38 -04:00
// english file for the page
2017-07-10 19:38:35 -04:00
if (
fs.existsSync(userFile) ||
fs.existsSync(
(userFile = userFile.replace(
path.basename(userFile),
'en' + sep + path.basename(userFile)
2017-07-10 19:38:35 -04:00
))
) ||
fs.existsSync((userFile = englishFile))
2017-07-10 19:38:35 -04:00
) {
2017-08-15 19:55:38 -04:00
// copy into docusaurus so require paths work
let parts = userFile.split('pages' + sep);
let tempFile = join(__dirname, '..', 'pages', parts[1]);
2017-07-10 19:38:35 -04:00
tempFile = tempFile.replace(
path.basename(file),
'temp' + path.basename(file)
2017-07-10 19:38:35 -04:00
);
mkdirp.sync(path.dirname(tempFile));
2017-07-07 13:28:29 -04:00
fs.copySync(userFile, tempFile);
2017-08-15 19:55:38 -04:00
// render into a string
removeModuleAndChildrenFromCache(tempFile);
2017-07-07 13:28:29 -04:00
const ReactComp = require(tempFile);
removeModuleAndChildrenFromCache(join('..', 'core', 'Site.js'));
const Site = require(join('..', 'core', 'Site.js'));
translate.setLanguage(language);
const str = renderToStaticMarkupWithDoctype(
<Site
language={language}
config={siteConfig}
metadata={{id: path.basename(userFile, '.js')}}>
2017-07-10 19:38:35 -04:00
<ReactComp language={language} />
</Site>
);
2017-07-07 13:28:29 -04:00
fs.removeSync(tempFile);
res.send(str);
2017-07-10 19:38:35 -04:00
} else {
2017-07-19 13:50:32 -04:00
next();
return;
2017-07-07 13:28:29 -04:00
}
});
2017-08-15 19:55:38 -04:00
// generate the main.css file by concatenating user provided css to the end
2017-07-10 19:38:35 -04:00
app.get(/main\.css$/, (req, res) => {
const mainCssPath = join(
__dirname,
'..',
'static',
req.path.toString().replace(siteConfig.baseUrl, '/')
);
let cssContent = fs.readFileSync(mainCssPath, {encoding: 'utf8'});
2017-07-07 13:28:29 -04:00
let files = glob.sync(join(CWD, 'static', '**', '*.css'));
2017-07-07 13:28:29 -04:00
files.forEach(file => {
if (isSeparateCss(file)) {
return;
}
2017-07-10 19:38:35 -04:00
cssContent =
cssContent + '\n' + fs.readFileSync(file, {encoding: 'utf8'});
2017-07-07 13:28:29 -04:00
});
2017-08-10 19:03:43 -04:00
if (
2017-08-10 19:10:30 -04:00
!siteConfig.colors ||
2017-08-10 19:03:43 -04:00
!siteConfig.colors.primaryColor ||
!siteConfig.colors.secondaryColor
2017-08-10 19:03:43 -04:00
) {
console.error(
`${chalk.yellow(
'Missing color configuration.'
)} Make sure siteConfig.colors includes primaryColor and secondaryColor fields.`
2017-07-10 19:38:35 -04:00
);
2017-08-10 19:03:43 -04:00
}
Object.keys(siteConfig.colors).forEach(key => {
const color = siteConfig.colors[key];
cssContent = cssContent.replace(new RegExp('\\$' + key, 'g'), color);
2017-08-10 19:03:43 -04:00
});
2017-12-12 15:49:02 -05:00
const codeColor = color(siteConfig.colors.primaryColor)
.alpha(0.07)
.string();
cssContent = cssContent.replace(new RegExp('\\$codeColor', 'g'), codeColor);
2017-07-07 13:28:29 -04:00
if (siteConfig.fonts) {
Object.keys(siteConfig.fonts).forEach(key => {
const fontString = siteConfig.fonts[key]
.map(font => '"' + font + '"')
.join(', ');
cssContent = cssContent.replace(
new RegExp('\\$' + key, 'g'),
fontString
);
});
}
res.header('Content-Type', 'text/css');
2017-07-07 13:28:29 -04:00
res.send(cssContent);
});
2017-08-15 19:55:38 -04:00
// serve static assets from these locations
2017-07-31 19:19:02 -04:00
app.use(
join(siteConfig.baseUrl, 'docs', 'assets'),
express.static(join(CWD, '..', readMetadata.getDocsPath(), 'assets'))
2017-07-31 19:19:02 -04:00
);
app.use(
join(siteConfig.baseUrl, 'blog', 'assets'),
express.static(join(CWD, 'blog', 'assets'))
2017-07-31 19:19:02 -04:00
);
app.use(siteConfig.baseUrl, express.static(join(CWD, 'static')));
app.use(siteConfig.baseUrl, express.static(join(__dirname, '..', 'static')));
2017-07-07 13:28:29 -04:00
// "redirect" requests to pages ending with "/" or no extension so that,
// for example, request to "blog" returns same result as "blog/index.html"
2017-07-07 13:28:29 -04:00
app.get(/\/[^\.]*\/?$/, (req, res) => {
let slash = req.path.toString().endsWith('/') ? '' : '/';
request.get(
'http://localhost:' + port + req.path + slash + 'index.html',
(err, response, body) => {
if (!err) {
if (response) {
res.status(response.statusCode).send(body);
} else {
console.error('No response');
2017-07-10 19:38:35 -04:00
}
} else {
console.error('Request failed:', err);
2017-07-07 13:28:29 -04:00
}
}
);
2017-07-07 13:28:29 -04:00
});
app.listen(port);
console.log('Open http://localhost:' + port + '/');
2017-07-07 13:28:29 -04:00
}
module.exports = execute;