Refactor + add more tests (Part 1) ()

* 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
lib/server

View file

@ -9,23 +9,14 @@ const cssnano = require('cssnano');
const path = require('path');
const escapeStringRegexp = require('escape-string-regexp');
// Return the subdirectory path from a reference directory
// Example:
// (file: 'docs/projectA/test.md', refDir: 'docs')
// returns 'projectA'
function getSubDir(file, refDir) {
let subDir = path.dirname(path.relative(refDir, file));
subDir = subDir.replace('\\', '/');
return subDir !== '.' ? subDir : null;
const subDir = path.dirname(path.relative(refDir, file)).replace('\\', '/');
return subDir !== '.' && !subDir.includes('..') ? subDir : null;
}
// Get the corresponding enabled language locale of a file.
// Example:
// (file: '/website/translated_docs/ko/projectA/test.md', refDir: 'website/translated_docs')
// returns 'ko'
function getLanguage(file, refDir) {
const regexSubFolder = new RegExp(
`/${escapeStringRegexp(path.basename(refDir))}/(.*)/.*/`
`${escapeStringRegexp(path.basename(refDir))}/(.*?)/.*`
);
const match = regexSubFolder.exec(file);
@ -42,6 +33,18 @@ function getLanguage(file, refDir) {
return null;
}
function isSeparateCss(file, separateDirs) {
if (!separateDirs) {
return false;
}
for (let i = 0; i < separateDirs.length; i++) {
if (file.includes(separateDirs[i])) {
return true;
}
}
return false;
}
function minifyCss(cssContent) {
return cssnano
.process(cssContent, {
@ -54,5 +57,6 @@ function minifyCss(cssContent) {
module.exports = {
getSubDir,
getLanguage,
isSeparateCss,
minifyCss,
};