eslint update

This commit is contained in:
Klemek
2021-03-30 16:29:20 +02:00
parent b0ba52e140
commit 88cb7ce30f
15 changed files with 637 additions and 491 deletions
+65 -57
View File
@@ -28,26 +28,26 @@ const cons = {
module.exports = (config) => {
/**
* Fetch articles from the data folder and send success as a response
* @param success
* @param error
*/
* Fetch articles from the data folder and send success as a response
* @param success
* @param error
*/
let reload;
/**
* Render the page with the view engine and catch errors
* @param req
* @param res
* @param vPath - path of the view
* @param data - data to pass to the view
* @param code - code to send along the page
*/
* Render the page with the view engine and catch errors
* @param req
* @param res
* @param vPath - path of the view
* @param data - data to pass to the view
* @param code - code to send along the page
*/
let render;
/**
* Show an error with the correct page
* @param req
* @param res
* @param code - error code
*/
* Show an error with the correct page
* @param req
* @param res
* @param code - error code
*/
let showError;
const fw = require('./file_walker')(config);
const renderer = require('./renderer')(config);
@@ -66,21 +66,22 @@ module.exports = (config) => {
fw.fetchArticles((err, dict) => {
if (err) {
console.error(cons.error, 'error loading articles : ' + err);
return error ? error() : null;
}
Object.keys(articles).forEach((key) => delete articles[key]);
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
const nb = Object.keys(articles).length;
const dnb = Object.values(articles).filter(a => a.draft).length;
if (nb > 0) {
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''} (${dnb} drafted)`);
error();
} else {
console.log(cons.warn, 'no articles loaded, check your configuration');
Object.keys(articles).forEach((key) => delete articles[key]);
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
const nb = Object.keys(articles).length;
const dnb = Object.values(articles).filter(a => a.draft).length;
if (nb > 0) {
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''} (${dnb} drafted)`);
} else {
console.log(cons.warn, 'no articles loaded, check your configuration');
}
lastRSS = '';
success();
}
lastRSS = '';
success();
});
};
if (config['test']) {
@@ -115,7 +116,7 @@ module.exports = (config) => {
if (err) {
res.sendStatus(code);
} else {
render(req, res, errorPath, {error: code}, code);
render(req, res, errorPath, { error: code }, code);
}
});
};
@@ -142,7 +143,7 @@ module.exports = (config) => {
res.end = (chunk, encoding) => {
fs.appendFile(config['access_log'],
`${res.statusCode} ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n`,
{encoding: 'UTF-8'}, () => {
{ encoding: 'UTF-8' }, () => {
res.end = end;
res.end(chunk, encoding);
});
@@ -162,7 +163,8 @@ module.exports = (config) => {
render(req, res, homePath,
{
articles: Object.values(articles)
.filter(d => !d.draft).sort((a, b) => ('' + b.path).localeCompare(a.path)),
.filter(d => !d.draft)
.sort((a, b) => ('' + b.path).localeCompare(a.path)),
});
}
});
@@ -180,10 +182,10 @@ module.exports = (config) => {
if (config['modules']['rss']) {
if (!lastRSS) {
const feed = new Rss({
'title': config['rss']['title'],
'description': config['rss']['description'],
'feed_url': host + req.url,
'site_url': host,
title: config['rss']['title'],
description: config['rss']['description'],
feed_url: host + req.url,
site_url: host,
});
Object.values(articles)
.slice(0, config['rss']['length'])
@@ -205,24 +207,29 @@ module.exports = (config) => {
//webhook endpoint
app.post(config['webhook']['endpoint'], (req, res) => {
if (config['modules']['webhook']) {
let valid = true;
if (config['webhook']['signature_header'] && config['webhook']['secret']) {
const payload = JSON.stringify(req.body) || '';
const hmac = crypto.createHmac('sha1', config['webhook']['secret']);
const digest = 'sha1=' + hmac.update(payload).digest('hex');
const checksum = req.headers[config['webhook']['signature_header']];
if (!checksum || !digest || checksum !== digest) {
return res.sendStatus(403);
res.sendStatus(403);
valid = false;
}
}
cp.exec(config['webhook']['pull_command'], {cwd: path.join(__dirname, '..', config['data_dir'])}, (err) => {
if (err) {
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
return res.sendStatus(500);
}
reload(() => {
res.sendStatus(200);
if (valid) {
cp.exec(config['webhook']['pull_command'], { cwd: path.join(__dirname, '..', config['data_dir']) }, (err) => {
if (err) {
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
res.sendStatus(500);
} else {
reload(() => {
res.sendStatus(200);
});
}
});
});
}
} else {
res.sendStatus(400);
}
@@ -254,18 +261,19 @@ module.exports = (config) => {
renderer.render(article.realPath, (err, html) => {
if (err) {
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
return showError(req, res, 500);
showError(req, res, 500);
} else {
article.content = html;
const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) {
console.log(cons.error, `no template found at ${templatePath}`);
showError(req, res, 500);
} else {
render(req, res, templatePath, { article: article });
}
});
}
article.content = html;
const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) {
console.log(cons.error, `no template found at ${templatePath}`);
showError(req, res, 500);
} else {
render(req, res, templatePath, {article: article});
}
});
});
}
} else {
@@ -300,7 +308,7 @@ module.exports = (config) => {
}
fs.appendFile(config['error_log'],
`500 ${req.method} ${req.url} ${new Date().toUTCString()} ${req.ips.join(' ') || req.ip}\n${err.stack}\n`,
{encoding: 'UTF-8'}, () => {
{ encoding: 'UTF-8' }, () => {
next(err);
});
});
+2 -2
View File
@@ -25,11 +25,11 @@ const merge = (ref, src) => {
module.exports = () => {
try {
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
let configData = fs.readFileSync('config.json', { encoding: 'UTF-8' });
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
console.log('\x1b[33m⚠\x1b[0m %s', 'Failed to load config.json : ' + error);
return refConfig;
}
};
};
+83 -78
View File
@@ -11,29 +11,31 @@ const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
const getFileTree = (dir, cb) => {
let list = [];
let remaining = 0;
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
fs.readdir(dir, { withFileTypes: true }, (err, items) => {
if (err) {
return cb(err);
}
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
getFileTree(path.join(dir, item.name), (err, out) => {
if (err) {
return cb(err);
}
list.push(...out);
remaining--;
if (remaining === 0) {
cb(null, list);
}
});
} else {
list.push(path.join(dir, item.name));
cb(err);
} else {
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
getFileTree(path.join(dir, item.name), (err, out) => {
if (err) {
cb(err);
} else {
list.push(...out);
remaining--;
if (remaining === 0) {
cb(null, list);
}
}
});
} else {
list.push(path.join(dir, item.name));
}
});
if (remaining === 0) {
cb(null, list);
}
});
if (remaining === 0) {
cb(null, list);
}
});
};
@@ -45,21 +47,21 @@ const getFileTree = (dir, cb) => {
* @param cb
*/
const readIndexFile = (path, thumbnailTag, cb) => {
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
fs.readFile(path, { encoding: 'UTF-8' }, (err, data) => {
if (err) {
return cb(err);
cb(err);
} else {
let info = {};
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
info.title = regRes1 ? regRes1[2].trim() : undefined;
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
const regRes2 = data.match(thumbnailRegEx);
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
cb(null, info);
}
let info = {};
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
info.title = regRes1 ? regRes1[2].trim() : undefined;
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
const regRes2 = data.match(thumbnailRegEx);
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
cb(null, info);
});
};
@@ -68,54 +70,57 @@ module.exports = (config) => {
fileTree: config['test'] ? getFileTree : undefined,
readIndexFile: config['test'] ? readIndexFile : undefined,
/**
* find and read all articles inside the data directory
* @param cb
*/
* find and read all articles inside the data directory
* @param cb
*/
fetchArticles: (cb) => {
getFileTree(config['data_dir'], (err, fileList) => {
if (err) {
return cb(err);
}
const paths = fileList
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
.filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) &&
/^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2]));
if (paths.length === 0) {
cb(null, {});
}
const articles = {};
let remaining = 0;
paths.forEach((p) => {
const article = {
path: joinUrl(p[0], p[1], p[2]),
draft: p[3] === config['article']['draft'],
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
year: parseInt(p[0]),
month: parseInt(p[1]),
day: parseInt(p[2]),
};
article.date = new Date(article.year, article.month, article.day);
article.date.setUTCHours(0);
remaining++;
readIndexFile(article.realPath, config['article']['thumbnail_tag'], (err, info) => {
if (err) {
return cb(err);
}
article.title = info.title || config['article']['default_title'];
article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail'];
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
if (!articles[article.path] || !article.draft) {
articles[article.path] = article;
}
remaining--;
if (remaining === 0) {
cb(null, articles);
}
cb(err);
} else {
const paths = fileList
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
.filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) &&
/^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2]));
if (paths.length === 0) {
cb(null, {});
}
const articles = {};
let remaining = 0;
paths.forEach((p) => {
const article = {
path: joinUrl(p[0], p[1], p[2]),
draft: p[3] === config['article']['draft'],
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
year: parseInt(p[0]),
month: parseInt(p[1]),
day: parseInt(p[2]),
};
article.date = new Date(article.year, article.month, article.day);
article.date.setUTCHours(0);
remaining++;
readIndexFile(article.realPath, config['article']['thumbnail_tag'], (err, info) => {
if (err) {
cb(err);
} else {
article.title = info.title || config['article']['default_title'];
article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail'];
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ')
.trim()
.replace(/ /gm, '_');
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
if (!articles[article.path] || !article.draft) {
articles[article.path] = article;
}
remaining--;
if (remaining === 0) {
cb(null, articles);
}
}
});
});
});
}
});
},
};
};
};
+1 -1
View File
@@ -14,4 +14,4 @@ module.exports = (config) => {
count: count,
read: read,
};
};
};
+2 -2
View File
@@ -25,8 +25,8 @@ if (!fs.existsSync('data')) {
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
fs.mkdirSync(dir, { recursive: true });
}
copy(path.join('sample_data', 'article'), dir);
}
}
+119 -99
View File
@@ -6,10 +6,10 @@ module.exports = (config) => {
const converter = new showdown.Converter(config['showdown']);
/**
* get parts outside of codes/scripts
* @param {string} data
* @returns {{index:number, end:number, text:string}[]} parts
*/
* get parts outside of codes/scripts
* @param {string} data
* @returns {{index:number, end:number, text:string}[]} parts
*/
const getParts = (data) => {
let parts = [];
let match;
@@ -67,17 +67,18 @@ module.exports = (config) => {
const renderPrism = (data, cb) => {
if (!config['modules']['prism']) {
return cb(data);
cb(data);
} else {
const codeRegex = /```([\w-]+)\r?\n((?:(?!```)[\s\S])*)\r?\n```/m;
let match;
while ((match = codeRegex.exec(data))) {
const lang = match[1].trim();
const code = match[2].trim();
const block = Prism.highlight(code, Prism.languages[lang] || Prism.languages.autoit, lang);
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
}
cb(data);
}
const codeRegex = /```([\w-]+)\r?\n((?:(?!```)[\s\S])*)\r?\n```/m;
let match;
while ((match = codeRegex.exec(data))) {
const lang = match[1].trim();
const code = match[2].trim();
const block = Prism.highlight(code, Prism.languages[lang] || Prism.languages.autoit, lang);
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
}
cb(data);
};
if (config['modules']['plantuml']) {
@@ -87,22 +88,23 @@ module.exports = (config) => {
const renderPlantUML = (data, cb) => {
/* global encode64 */
if (!config['modules']['plantuml']) {
return cb(data);
cb(data);
} else {
const parts = getParts(data);
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
let match;
parts.forEach(part => {
while ((match = umlRegex.exec(part.text))) {
const code = match[1].trim();
const s = unescape(encodeURIComponent(code));
const compressed = global['zip_deflate'](s);
const url = `http://www.plantuml.com/plantuml/${config['plantuml']['output_format']}/${encode64(compressed)}`;
part.text = part.text.slice(0, match.index) + `<img alt="generated PlantUML diagram" src="${url}">` + part.text.slice(match.index + match[0].length);
}
data = data.slice(0, part.index) + part.text + data.slice(part.end);
});
cb(data);
}
const parts = getParts(data);
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
let match;
parts.forEach(part => {
while ((match = umlRegex.exec(part.text))) {
const code = match[1].trim();
const s = unescape(encodeURIComponent(code));
const compressed = global['zip_deflate'](s);
const url = `http://www.plantuml.com/plantuml/${config['plantuml']['output_format']}/${encode64(compressed)}`;
part.text = part.text.slice(0, match.index) + `<img alt="generated PlantUML diagram" src="${url}">` + part.text.slice(match.index + match[0].length);
}
data = data.slice(0, part.index) + part.text + data.slice(part.end);
});
cb(data);
};
let mjAPI;
@@ -111,8 +113,18 @@ module.exports = (config) => {
mjAPI.config({
MathJax: {
tex2jax: {
inlineMath: [['$', '$']],
displayMath: [['$$', '$$']],
inlineMath: [
[
'$',
'$',
],
],
displayMath: [
[
'$$',
'$$',
],
],
},
},
});
@@ -120,40 +132,47 @@ module.exports = (config) => {
const renderMathJax = (data, cb) => {
if (!config['modules']['mathjax']) {
return cb(data);
}
cb(data);
} else {
const parts = getParts(data);
const parts = getParts(data);
const doMJ = (match, format, i) => {
const eq = match[1].trim();
const output = config['mathjax']['output_format'];
const mjConf = {
math: eq,
format: format,
speakText: config['mathjax']['speak_text'],
};
mjConf[output] = true;
mjAPI.typeset(mjConf, (res) => {
data = data.slice(0, parts[i].index + match.index) + res[output] + data.slice(parts[i].index + match.index + match[0].length);
renderMathJax(data, (data2) => {
cb(data2);
const doMJ = (match, format, i) => {
const eq = match[1].trim();
const output = config['mathjax']['output_format'];
const mjConf = {
math: eq,
format: format,
speakText: config['mathjax']['speak_text'],
};
mjConf[output] = true;
mjAPI.typeset(mjConf, (res) => {
data = data.slice(0, parts[i].index + match.index) + res[output] + data.slice(parts[i].index + match.index + match[0].length);
renderMathJax(data, (data2) => {
cb(data2);
});
});
});
};
};
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
const inlineEqRegex = /\$([^$\n]*)\$/;
const eqRegex = /\$\$((?:(?!\$\$)[\s\S])*)\$\$/m;
const inlineEqRegex = /\$([^$\n]*)\$/;
for (let i = 0; i < parts.length; i++) {
let match;
if ((match = eqRegex.exec(parts[i].text))) {
return doMJ(match, 'TeX', i);
} else if ((match = inlineEqRegex.exec(parts[i].text))) {
return doMJ(match, 'inline-TeX', i);
let found = false;
for (let i = 0; i < parts.length; i++) {
let match;
if ((match = eqRegex.exec(parts[i].text))) {
doMJ(match, 'TeX', i);
found = true;
break;
} else if ((match = inlineEqRegex.exec(parts[i].text))) {
doMJ(match, 'inline-TeX', i);
found = true;
break;
}
}
if (!found) {
cb(data);
}
}
cb(data);
};
let faDiagrams;
@@ -165,36 +184,37 @@ module.exports = (config) => {
const renderFaDiagrams = (data, cb) => {
if (!config['modules']['fa-diagrams']) {
return cb(data);
}
const parts = getParts(data);
const diagramsRegex = /@startfad\r?\n((?:(?!@endfad)[\s\S])*)\r?\n@endfad/m;
let match;
parts.forEach(part => {
while ((match = diagramsRegex.exec(part.text))) {
const code = match[1].trim();
let output;
try {
const diagData = toml.parse(code);
const findLineBreaks = (data) => {
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object') {
findLineBreaks(data[key]);
} else if (typeof data[key] === 'string') {
data[key] = data[key].replace(/\\n/gm, '\n');
}
});
};
findLineBreaks(diagData);
output = faDiagrams.compute(diagData);
} catch (err) {
output = `<b style="color:red">${err.toString()}</b>`;
cb(data);
} else {
const parts = getParts(data);
const diagramsRegex = /@startfad\r?\n((?:(?!@endfad)[\s\S])*)\r?\n@endfad/m;
let match;
parts.forEach(part => {
while ((match = diagramsRegex.exec(part.text))) {
const code = match[1].trim();
let output;
try {
const diagData = toml.parse(code);
const findLineBreaks = (data) => {
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object') {
findLineBreaks(data[key]);
} else if (typeof data[key] === 'string') {
data[key] = data[key].replace(/\\n/gm, '\n');
}
});
};
findLineBreaks(diagData);
output = faDiagrams.compute(diagData);
} catch (err) {
output = `<b style="color:red">${err.toString()}</b>`;
}
part.text = part.text.slice(0, match.index) + output + part.text.slice(match.index + match[0].length);
}
part.text = part.text.slice(0, match.index) + output + part.text.slice(match.index + match[0].length);
}
data = data.slice(0, part.index) + part.text + data.slice(part.end);
});
cb(data);
data = data.slice(0, part.index) + part.text + data.slice(part.end);
});
cb(data);
}
};
return {
@@ -205,22 +225,22 @@ module.exports = (config) => {
renderMathJax: config['test'] ? renderMathJax : undefined,
renderFaDiagrams: config['test'] ? renderFaDiagrams : undefined,
render: (file, cb) => {
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
fs.readFile(file, { encoding: 'UTF-8' }, (err, data) => {
if (err) {
return cb(err);
}
renderPlantUML(data, (data) => {
renderFaDiagrams(data, (data) => {
renderMathJax(data, (data) => {
renderPrism(data, (data) => {
renderShowDown(data, (html) => {
cb(null, html);
cb(err);
} else {
renderPlantUML(data, (data) => {
renderFaDiagrams(data, (data) => {
renderMathJax(data, (data) => {
renderPrism(data, (data) => {
renderShowDown(data, (html) => {
cb(null, html);
});
});
});
});
});
});
}
});
},
};
+1 -1
View File
@@ -5,6 +5,6 @@ const fs = require('fs');
* @param scriptPath
*/
module.exports = (scriptPath) => {
eval.call(global, fs.readFileSync(scriptPath, {encoding: 'UTF-8'}));
eval.call(global, fs.readFileSync(scriptPath, { encoding: 'UTF-8' }));
};
+1 -1
View File
@@ -1,4 +1,4 @@
const config = require('./config')();
const app = require('./app')(config);
app.start();
app.start();