mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
Refactor + add more tests (Part 2) (#854)
* Refactor doc in server.js & generate.js * Refactor finding metadata with find() & avoid param reassign * Refactor replacing markdown link to html + add test * Minify func to getFile & add test * use ./ for md link as well * nits & better test * better mdToHtmlify * babel-register on versions & write-translations to transpile ES6 in server/env.js * better docs test & move out metadata to fixtures * Update .babelrc
This commit is contained in:
parent
03e237abda
commit
9f718a5097
13 changed files with 430 additions and 154 deletions
|
@ -11,6 +11,7 @@ function execute(port, options) {
|
|||
const extractTranslations = require('../write-translations');
|
||||
|
||||
const metadataUtils = require('./metadataUtils');
|
||||
const docs = require('./docs');
|
||||
|
||||
const env = require('./env.js');
|
||||
const express = require('express');
|
||||
|
@ -18,8 +19,6 @@ function execute(port, options) {
|
|||
const request = require('request');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const {insertTOC} = require('../core/toc');
|
||||
const {getPath} = require('../core/utils');
|
||||
const {isSeparateCss} = require('./utils');
|
||||
const mkdirp = require('mkdirp');
|
||||
const glob = require('glob');
|
||||
|
@ -152,100 +151,19 @@ function execute(port, options) {
|
|||
// handle all requests for document pages
|
||||
app.get(routing.docs(siteConfig.baseUrl), (req, res, next) => {
|
||||
const url = req.path.toString().replace(siteConfig.baseUrl, '');
|
||||
|
||||
// links is a map from a permalink to an id for each document
|
||||
const links = {};
|
||||
Object.keys(Metadata).forEach(id => {
|
||||
const metadata = Metadata[id];
|
||||
links[metadata.permalink] = id;
|
||||
});
|
||||
|
||||
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);
|
||||
|
||||
const metadata = Metadata[links[url]];
|
||||
if (!metadata) {
|
||||
const metadata =
|
||||
Metadata[
|
||||
Object.keys(Metadata).find(id => Metadata[id].permalink === url)
|
||||
];
|
||||
const file = docs.getFile(metadata);
|
||||
if (!file) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
const language = metadata.language;
|
||||
|
||||
// determine what file to use according to its id
|
||||
let file;
|
||||
if (metadata.original_id) {
|
||||
if (env.translation.enabled && metadata.language !== 'en') {
|
||||
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
|
||||
} else {
|
||||
file = join(CWD, 'versioned_docs', metadata.source);
|
||||
}
|
||||
} else if (!env.translation.enabled || metadata.language === 'en') {
|
||||
file = join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
|
||||
} else {
|
||||
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(file)) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
let rawContent = metadataUtils.extractMetadata(
|
||||
fs.readFileSync(file, 'utf8')
|
||||
).rawContent;
|
||||
|
||||
// generate table of contents if appropriate
|
||||
rawContent = insertTOC(rawContent);
|
||||
|
||||
const defaultVersion = env.versioning.defaultVersion;
|
||||
|
||||
// replace any links to markdown files to their website html links
|
||||
Object.keys(mdToHtml).forEach(key => {
|
||||
let link = mdToHtml[key];
|
||||
link = getPath(link, siteConfig.cleanUrl);
|
||||
link = link.replace('/en/', `/${language}/`);
|
||||
link = link.replace(
|
||||
'/VERSION/',
|
||||
metadata.version && metadata.version !== defaultVersion
|
||||
? `/${metadata.version}/`
|
||||
: '/'
|
||||
);
|
||||
// replace relative links with & without "./"
|
||||
rawContent = rawContent.replace(
|
||||
new RegExp(`\\]\\((${key}|\\./${key})`, 'g'),
|
||||
`](${link}`
|
||||
);
|
||||
});
|
||||
|
||||
// replace any relative links to static assets to absolute links
|
||||
rawContent = rawContent.replace(
|
||||
/\]\(assets\//g,
|
||||
`](${siteConfig.baseUrl}docs/assets/`
|
||||
);
|
||||
|
||||
const rawContent = metadataUtils.extractMetadata(file).rawContent;
|
||||
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'),
|
||||
});
|
||||
}
|
||||
|
||||
const docComp = (
|
||||
<DocsLayout
|
||||
metadata={metadata}
|
||||
language={language}
|
||||
config={siteConfig}
|
||||
Doc={Doc}>
|
||||
{rawContent}
|
||||
</DocsLayout>
|
||||
);
|
||||
|
||||
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);
|
||||
const docComp = docs.getComponent(rawContent, mdToHtml, metadata);
|
||||
res.send(renderToStaticMarkupWithDoctype(docComp));
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue