Refactor + add more tests (Part 1) (#847)

* Refactor mdToHtml out

* Refactor routing + move it to server instead of core

* Refactor & Add more tests for server utils

* Refactor isSeparateCss function from server & generate

* Refactor insertTableOfContents from server & generate + add tests

* undo small nits
This commit is contained in:
Endilie Yacop Sucipto 2018-07-14 02:31:37 +08:00 committed by Yangshun Tay
parent a7a214fb3a
commit defcbcc8ee
14 changed files with 322 additions and 235 deletions

View file

@ -14,9 +14,9 @@ async function execute() {
const fs = require('fs-extra');
const readMetadata = require('./readMetadata.js');
const path = require('path');
const getTOC = require('../core/getTOC');
const utils = require('../core/utils.js');
const serverUtils = require('./utils');
const {insertTOC} = require('../core/toc');
const {getPath} = require('../core/utils.js');
const {minifyCss, isSeparateCss} = require('./utils');
const React = require('react');
const mkdirp = require('mkdirp');
const glob = require('glob');
@ -53,36 +53,6 @@ async function execute() {
}
}
const TABLE_OF_CONTENTS_TOKEN = '<AUTOGENERATED_TABLE_OF_CONTENTS>';
// takes the content of a doc article and returns the content with a table of
// contents inserted
const insertTableOfContents = rawContent => {
const filterRe = /^`[^`]*`/;
const headers = getTOC(rawContent, 'h3', null);
const tableOfContents = headers
.filter(header => filterRe.test(header.rawContent))
.map(header => ` - [${header.rawContent}](#${header.hashLink})`)
.join('\n');
return rawContent.replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents);
};
// returns true if a file should be excluded from concatentation to
// default Docusaurus styles
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;
}
console.log('generate.js triggered...');
// array of tags of enabled languages
@ -103,23 +73,7 @@ async function execute() {
const buildDir = join(CWD, 'build', siteConfig.projectName);
// 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
const mdToHtml = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
if (metadata.language !== 'en' || metadata.original_id) {
return;
}
let htmlLink =
siteConfig.baseUrl + metadata.permalink.replace('/next/', '/');
if (htmlLink.includes('/docs/en/')) {
htmlLink = htmlLink.replace('/docs/en/', '/docs/en/VERSION/');
} else {
htmlLink = htmlLink.replace('/docs/', '/docs/VERSION/');
}
mdToHtml[metadata.source] = htmlLink;
});
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);
const DocsLayout = require('../core/DocsLayout.js');
const Redirect = require('../core/Redirect.js');
@ -154,16 +108,14 @@ async function execute() {
const language = metadata.language;
// generate table of contents if appropriate
if (rawContent && rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) !== -1) {
rawContent = insertTableOfContents(rawContent);
}
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 = utils.getPath(link, siteConfig.cleanUrl);
link = getPath(link, siteConfig.cleanUrl);
link = link.replace('/en/', `/${language}/`);
link = link.replace(
'/VERSION/',
@ -171,14 +123,9 @@ async function execute() {
? `/${metadata.version}/`
: '/'
);
// replace relative links without "./"
// replace relative links with & without "./"
rawContent = rawContent.replace(
new RegExp(`\\]\\(${key}`, 'g'),
`](${link}`
);
// replace relative links with "./"
rawContent = rawContent.replace(
new RegExp(`\\]\\(\\./${key}`, 'g'),
new RegExp(`\\]\\((${key}|\\./${key})`, 'g'),
`](${link}`
);
});
@ -204,10 +151,7 @@ async function execute() {
env.translation.enabled &&
metadata.permalink.indexOf('docs/en') !== -1
) {
const redirectlink = utils.getPath(
metadata.permalink,
siteConfig.cleanUrl
);
const redirectlink = getPath(metadata.permalink, siteConfig.cleanUrl);
const redirectComp = (
<Redirect
metadata={metadata}
@ -391,7 +335,10 @@ async function execute() {
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
const normalizedFile = path.normalize(file);
// parse css files to replace colors and fonts according to siteConfig
if (normalizedFile.match(/\.css$/) && !isSeparateCss(normalizedFile)) {
if (
normalizedFile.match(/\.css$/) &&
!isSeparateCss(normalizedFile, siteConfig.separateCss)
) {
const mainCss = join(buildDir, 'css', 'main.css');
let cssContent = fs.readFileSync(normalizedFile, 'utf8');
cssContent = `${fs.readFileSync(mainCss, 'utf8')}\n${cssContent}`;
@ -445,7 +392,7 @@ async function execute() {
// Use cssnano to minify the final combined CSS.
const mainCss = join(buildDir, 'css', 'main.css');
const cssContent = fs.readFileSync(mainCss, 'utf8');
const css = await serverUtils.minifyCss(cssContent);
const css = await minifyCss(cssContent);
fs.writeFileSync(mainCss, css);
// compile/copy pages from user