Code formatting with 2 space indent
This commit is contained in:
+132
-132
@@ -8,155 +8,155 @@ const path = require('path');
|
|||||||
* @type {{warn: string, ok: string, error: string}}
|
* @type {{warn: string, ok: string, error: string}}
|
||||||
*/
|
*/
|
||||||
const cons = {
|
const cons = {
|
||||||
ok: '\x1b[32m✔\x1b[0m %s',
|
ok: '\x1b[32m✔\x1b[0m %s',
|
||||||
warn: '\x1b[33m⚠\x1b[0m %s',
|
warn: '\x1b[33m⚠\x1b[0m %s',
|
||||||
error: '\x1b[31m✘\x1b[0m %s',
|
error: '\x1b[31m✘\x1b[0m %s',
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = (config) => {
|
module.exports = (config) => {
|
||||||
const fw = require('./file_walker')(config);
|
const fw = require('./file_walker')(config);
|
||||||
const renderer = require('./renderer')(config);
|
const renderer = require('./renderer')(config);
|
||||||
|
|
||||||
// set view engine from configuration
|
// set view engine from configuration
|
||||||
app.set('view engine', config['view_engine']);
|
app.set('view engine', config['view_engine']);
|
||||||
// reroute the views folder to the root folder
|
// reroute the views folder to the root folder
|
||||||
app.set('views', path.join(__dirname, '..'));
|
app.set('views', path.join(__dirname, '..'));
|
||||||
|
|
||||||
const articles = {};
|
const articles = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch articles from the data folder and send success as a response
|
* Fetch articles from the data folder and send success as a response
|
||||||
* @param callback
|
* @param callback
|
||||||
*/
|
*/
|
||||||
const reload = (callback) => {
|
const reload = (callback) => {
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(false);
|
callback(false);
|
||||||
return console.error(cons.error, 'loading articles : ' + err);
|
return console.error(cons.error, 'loading articles : ' + err);
|
||||||
}
|
}
|
||||||
Object.keys(articles).forEach((key) => delete articles[key]);
|
Object.keys(articles).forEach((key) => delete articles[key]);
|
||||||
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
|
||||||
const nb = Object.keys(articles).length;
|
const nb = Object.keys(articles).length;
|
||||||
if (nb > 0)
|
if (nb > 0)
|
||||||
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
||||||
else
|
else
|
||||||
console.log(cons.warn, `no articles loaded, check your configuration`);
|
console.log(cons.warn, `no articles loaded, check your configuration`);
|
||||||
callback(true);
|
callback(true);
|
||||||
});
|
|
||||||
};
|
|
||||||
if (config['test'])
|
|
||||||
app.reload = reload;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render the page with the view engine and catch errors
|
|
||||||
* @param res
|
|
||||||
* @param vPath - path of the view
|
|
||||||
* @param data - data to pass to the view
|
|
||||||
* @param code - code to send along the page
|
|
||||||
*/
|
|
||||||
const render = (res, vPath, data, code = 200) => {
|
|
||||||
res.render(vPath, data, (err, html) => {
|
|
||||||
if (err) {
|
|
||||||
res.sendStatus(500);
|
|
||||||
console.log(cons.error, `failed to render ${vPath} : ${err}`);
|
|
||||||
} else
|
|
||||||
res.status(code).send(html);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Show an error with the correct page
|
|
||||||
* @param resPath - the page of the original error
|
|
||||||
* @param code - error code
|
|
||||||
* @param res
|
|
||||||
*/
|
|
||||||
const showError = (resPath, code, res) => {
|
|
||||||
const errorPath = path.join(config['data_dir'], config['home']['error']);
|
|
||||||
fs.access(errorPath, fs.constants.R_OK, (err) => {
|
|
||||||
if (err)
|
|
||||||
res.sendStatus(code);
|
|
||||||
else
|
|
||||||
render(res, errorPath, {error: code, path: resPath}, code);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// home endpoint : send the correct index page or error if not existing
|
|
||||||
app.get('/', (req, res) => {
|
|
||||||
const homePath = path.join(config['data_dir'], config['home']['index']);
|
|
||||||
fs.access(homePath, fs.constants.R_OK, (err) => {
|
|
||||||
if (err)
|
|
||||||
showError(req.path, 404, res);
|
|
||||||
else
|
|
||||||
render(res, homePath, {articles: Object.values(articles)});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
if (config['test'])
|
||||||
|
app.reload = reload;
|
||||||
|
|
||||||
// catch all article urls and render them
|
/**
|
||||||
app.get('*', (req, res, next) => {
|
* Render the page with the view engine and catch errors
|
||||||
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
|
* @param res
|
||||||
const articlePath = req.path.substr(1, 10);
|
* @param vPath - path of the view
|
||||||
const article = articles[articlePath];
|
* @param data - data to pass to the view
|
||||||
if (!article)
|
* @param code - code to send along the page
|
||||||
showError(req.path, 404, res);
|
*/
|
||||||
else {
|
const render = (res, vPath, data, code = 200) => {
|
||||||
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
|
res.render(vPath, data, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
res.sendStatus(500);
|
||||||
return showError(req.path, 500, res);
|
console.log(cons.error, `failed to render ${vPath} : ${err}`);
|
||||||
}
|
} else
|
||||||
article.content = html;
|
res.status(code).send(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.path, 500, res);
|
|
||||||
} else
|
|
||||||
render(res, templatePath, {article: article});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// catch all hidden file type and return 404
|
/**
|
||||||
app.get('*', (req, res, next) => {
|
* Show an error with the correct page
|
||||||
if (config['home']['hidden'].includes(path.extname(req.path)))
|
* @param resPath - the page of the original error
|
||||||
showError(req.path, 404, res);
|
* @param code - error code
|
||||||
else
|
* @param res
|
||||||
next();
|
*/
|
||||||
|
const showError = (resPath, code, res) => {
|
||||||
|
const errorPath = path.join(config['data_dir'], config['home']['error']);
|
||||||
|
fs.access(errorPath, fs.constants.R_OK, (err) => {
|
||||||
|
if (err)
|
||||||
|
res.sendStatus(code);
|
||||||
|
else
|
||||||
|
render(res, errorPath, {error: code, path: resPath}, code);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// serve all static files via get
|
// home endpoint : send the correct index page or error if not existing
|
||||||
app.get('*', express.static(config['data_dir']));
|
app.get('/', (req, res) => {
|
||||||
// catch express.static errors (mostly not found) by displaying 404
|
const homePath = path.join(config['data_dir'], config['home']['index']);
|
||||||
app.get('*', (req, res) => {
|
fs.access(homePath, fs.constants.R_OK, (err) => {
|
||||||
|
if (err)
|
||||||
showError(req.path, 404, res);
|
showError(req.path, 404, res);
|
||||||
|
else
|
||||||
|
render(res, homePath, {articles: Object.values(articles)});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// catch all other methods and return 400
|
// catch all article urls and render them
|
||||||
app.all('*', (req, res) => {
|
app.get('*', (req, res, next) => {
|
||||||
res.status(400).send('bad request');
|
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
|
||||||
});
|
const articlePath = req.path.substr(1, 10);
|
||||||
|
const article = articles[articlePath];
|
||||||
app.use((err, req, res, next) => {
|
if (!article)
|
||||||
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
|
showError(req.path, 404, res);
|
||||||
console.error(err.stack);
|
else {
|
||||||
next(err);
|
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
|
||||||
});
|
if (err) {
|
||||||
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
// must be use in a server.js to start the server
|
return showError(req.path, 500, res);
|
||||||
app.start = () => {
|
}
|
||||||
reload((res) => {
|
article.content = html;
|
||||||
if (res)
|
const templatePath = path.join(config['data_dir'], config['article']['template']);
|
||||||
app.listen(config['node_port'], () => {
|
fs.access(templatePath, fs.constants.R_OK, (err) => {
|
||||||
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
|
if (err) {
|
||||||
});
|
console.log(cons.error, `no template found at ${templatePath}`);
|
||||||
|
showError(req.path, 500, res);
|
||||||
|
} else
|
||||||
|
render(res, templatePath, {article: article});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return app;
|
// catch all hidden file type and return 404
|
||||||
|
app.get('*', (req, res, next) => {
|
||||||
|
if (config['home']['hidden'].includes(path.extname(req.path)))
|
||||||
|
showError(req.path, 404, res);
|
||||||
|
else
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
// serve all static files via get
|
||||||
|
app.get('*', express.static(config['data_dir']));
|
||||||
|
// catch express.static errors (mostly not found) by displaying 404
|
||||||
|
app.get('*', (req, res) => {
|
||||||
|
showError(req.path, 404, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
// catch all other methods and return 400
|
||||||
|
app.all('*', (req, res) => {
|
||||||
|
res.status(400).send('bad request');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
|
||||||
|
console.error(err.stack);
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
// must be use in a server.js to start the server
|
||||||
|
app.start = () => {
|
||||||
|
reload((res) => {
|
||||||
|
if (res)
|
||||||
|
app.listen(config['node_port'], () => {
|
||||||
|
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return app;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,9 @@
|
|||||||
"home": {
|
"home": {
|
||||||
"index": "index.ejs",
|
"index": "index.ejs",
|
||||||
"error": "error.ejs",
|
"error": "error.ejs",
|
||||||
"hidden": [".ejs"]
|
"hidden": [
|
||||||
|
".ejs"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"article": {
|
"article": {
|
||||||
"index": "index.md",
|
"index": "index.md",
|
||||||
|
|||||||
+17
-17
@@ -8,24 +8,24 @@ const fs = require('fs');
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
const merge = (ref, src) => {
|
const merge = (ref, src) => {
|
||||||
if (typeof ref !== typeof src) {
|
if (typeof ref !== typeof src) {
|
||||||
return ref;
|
return ref;
|
||||||
} else if (typeof ref === 'object') {
|
} else if (typeof ref === 'object') {
|
||||||
const out = {};
|
const out = {};
|
||||||
Object.keys(ref).forEach((key) =>out[key] = merge(ref[key], src[key]));
|
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
|
||||||
return out;
|
return out;
|
||||||
} else {
|
} else {
|
||||||
return src;
|
return src;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
try {
|
try {
|
||||||
let configData = fs.readFileSync('config.json', {encoding:'UTF-8'});
|
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
|
||||||
let config = JSON.parse(configData);
|
let config = JSON.parse(configData);
|
||||||
return merge(refConfig, config);
|
return merge(refConfig, config);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load config.json : '+error);
|
console.error('Failed to load config.json : ' + error);
|
||||||
return refConfig;
|
return refConfig;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
+78
-78
@@ -7,29 +7,29 @@ const path = require('path');
|
|||||||
* @param cb
|
* @param cb
|
||||||
*/
|
*/
|
||||||
const getFileTree = (dir, cb) => {
|
const getFileTree = (dir, cb) => {
|
||||||
let list = [];
|
let list = [];
|
||||||
let remaining = 0;
|
let remaining = 0;
|
||||||
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
|
||||||
if (err)
|
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);
|
return cb(err);
|
||||||
items.forEach((item) => {
|
list.push(...out);
|
||||||
if (item.isDirectory()) {
|
remaining--;
|
||||||
remaining++;
|
if (remaining === 0)
|
||||||
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));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (remaining === 0)
|
|
||||||
cb(null, list);
|
cb(null, list);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
list.push(path.join(dir, item.name));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
if (remaining === 0)
|
||||||
|
cb(null, list);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,70 +39,70 @@ const getFileTree = (dir, cb) => {
|
|||||||
* @param cb
|
* @param cb
|
||||||
*/
|
*/
|
||||||
const readIndexFile = (path, thumbnailTag, cb) => {
|
const readIndexFile = (path, thumbnailTag, cb) => {
|
||||||
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
|
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
|
||||||
if (err)
|
if (err)
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
|
||||||
let info = {};
|
let info = {};
|
||||||
|
|
||||||
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
|
||||||
info.title = regRes1 ? regRes1[2].trim() : undefined;
|
info.title = regRes1 ? regRes1[2].trim() : undefined;
|
||||||
|
|
||||||
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
|
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
|
||||||
const regRes2 = data.match(thumbnailRegEx);
|
const regRes2 = data.match(thumbnailRegEx);
|
||||||
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
|
||||||
|
|
||||||
cb(null, info);
|
cb(null, info);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = (config) => {
|
module.exports = (config) => {
|
||||||
return {
|
return {
|
||||||
fileTree: config['test'] ? getFileTree : undefined,
|
fileTree: config['test'] ? getFileTree : undefined,
|
||||||
readIndexFile: config['test'] ? readIndexFile : undefined,
|
readIndexFile: config['test'] ? readIndexFile : undefined,
|
||||||
/**
|
/**
|
||||||
* find and read all articles inside the data directory
|
* find and read all articles inside the data directory
|
||||||
* @param cb
|
* @param cb
|
||||||
*/
|
*/
|
||||||
fetchArticles: (cb) => {
|
fetchArticles: (cb) => {
|
||||||
getFileTree(config['data_dir'], (err, fileList) => {
|
getFileTree(config['data_dir'], (err, fileList) => {
|
||||||
if (err)
|
if (err)
|
||||||
return cb(err);
|
return cb(err);
|
||||||
const paths = fileList
|
const paths = fileList
|
||||||
.map((path) => path.substr(config['data_dir'].length))
|
.map((path) => path.substr(config['data_dir'].length))
|
||||||
.filter((path) => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
|
.filter((path) => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length)
|
||||||
.map((path) => path.substr(0, path.length - config['article']['index'].length))
|
.map((path) => path.substr(0, path.length - config['article']['index'].length))
|
||||||
.map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
.map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/))
|
||||||
.filter((matches) => matches && matches.length > 1);
|
.filter((matches) => matches && matches.length > 1);
|
||||||
if (paths.length === 0)
|
if (paths.length === 0)
|
||||||
cb(null, {});
|
cb(null, {});
|
||||||
const articles = {};
|
const articles = {};
|
||||||
let remaining = 0;
|
let remaining = 0;
|
||||||
paths.forEach((matches) => {
|
paths.forEach((matches) => {
|
||||||
const article = {
|
const article = {
|
||||||
path: path.join(matches[1], matches[2], matches[3]),
|
path: path.join(matches[1], matches[2], matches[3]),
|
||||||
realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
|
realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]),
|
||||||
year: parseInt(matches[1]),
|
year: parseInt(matches[1]),
|
||||||
month: parseInt(matches[2]),
|
month: parseInt(matches[2]),
|
||||||
day: parseInt(matches[3])
|
day: parseInt(matches[3])
|
||||||
};
|
};
|
||||||
article.date = new Date(article.year, article.month, article.day);
|
article.date = new Date(article.year, article.month, article.day);
|
||||||
remaining++;
|
remaining++;
|
||||||
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
|
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
|
||||||
if (err)
|
if (err)
|
||||||
return cb(err);
|
return cb(err);
|
||||||
article.title = info.title || config['article']['default_title'];
|
article.title = info.title || config['article']['default_title'];
|
||||||
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
||||||
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
||||||
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
|
article.url = '/' + path.join(article.path, article.escapedTitle) + '/';
|
||||||
articles[article.path] = article;
|
articles[article.path] = article;
|
||||||
remaining--;
|
remaining--;
|
||||||
if (remaining === 0)
|
if (remaining === 0)
|
||||||
cb(null, articles);
|
cb(null, articles);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
+14
-14
@@ -3,28 +3,28 @@ const path = require('path');
|
|||||||
const ncp = require('ncp').ncp;
|
const ncp = require('ncp').ncp;
|
||||||
|
|
||||||
const copy = (src, dest) => {
|
const copy = (src, dest) => {
|
||||||
ncp(src, dest, function (err) {
|
ncp(src, dest, function (err) {
|
||||||
if (err)
|
if (err)
|
||||||
console.error(err);
|
console.error(err);
|
||||||
else
|
else
|
||||||
console.log(`copied ${src} to ${dest}`);
|
console.log(`copied ${src} to ${dest}`);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
copy(path.join('src', 'config.default.json'), 'config.example.json');
|
copy(path.join('src', 'config.default.json'), 'config.example.json');
|
||||||
|
|
||||||
if (!fs.existsSync('data')) {
|
if (!fs.existsSync('data')) {
|
||||||
fs.mkdirSync('data');
|
fs.mkdirSync('data');
|
||||||
|
|
||||||
copy(path.join('sample_data','home'), 'data');
|
copy(path.join('sample_data', 'home'), 'data');
|
||||||
|
|
||||||
const pad0 = (n) =>('0' + n).substr(-2);
|
const pad0 = (n) => ('0' + n).substr(-2);
|
||||||
|
|
||||||
const datetime = new Date();
|
const datetime = new Date();
|
||||||
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
|
||||||
|
|
||||||
if (!fs.existsSync(dir))
|
if (!fs.existsSync(dir))
|
||||||
fs.mkdirSync(dir, {recursive: true});
|
fs.mkdirSync(dir, {recursive: true});
|
||||||
|
|
||||||
copy(path.join('sample_data','article'), dir);
|
copy(path.join('sample_data', 'article'), dir);
|
||||||
}
|
}
|
||||||
+11
-11
@@ -2,15 +2,15 @@ const fs = require('fs');
|
|||||||
const showdown = require('showdown');
|
const showdown = require('showdown');
|
||||||
|
|
||||||
module.exports = (config) => {
|
module.exports = (config) => {
|
||||||
const converter = new showdown.Converter(config['showdown']);
|
const converter = new showdown.Converter(config['showdown']);
|
||||||
return {
|
return {
|
||||||
render : (file, cb) => {
|
render: (file, cb) => {
|
||||||
fs.readFile(file, {encoding:'UTF-8'}, (err, data) => {
|
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
|
||||||
if(err)
|
if (err)
|
||||||
return cb(err);
|
return cb(err);
|
||||||
const html = converter.makeHtml(data);
|
const html = converter.makeHtml(data);
|
||||||
cb(null,html);
|
cb(null, html);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
+152
-152
@@ -10,197 +10,197 @@ const testError = 'testerror.ejs';
|
|||||||
const testTemplate = 'testtemplate.ejs';
|
const testTemplate = 'testtemplate.ejs';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
'test': true,
|
||||||
'data_dir': dataDir,
|
'data_dir': dataDir,
|
||||||
'view_engine': 'ejs',
|
'view_engine': 'ejs',
|
||||||
'home': {
|
'home': {
|
||||||
'index': testIndex,
|
'index': testIndex,
|
||||||
'error': testError,
|
'error': testError,
|
||||||
'hidden': ['.ejs','.test']
|
'hidden': ['.ejs', '.test']
|
||||||
},
|
},
|
||||||
'article': {
|
'article': {
|
||||||
'index': 'index.md',
|
'index': 'index.md',
|
||||||
'template' : testTemplate,
|
'template': testTemplate,
|
||||||
'thumbnail_tag': 'thumbnail',
|
'thumbnail_tag': 'thumbnail',
|
||||||
'default_title': 'Untitled',
|
'default_title': 'Untitled',
|
||||||
'default_thumbnail': null
|
'default_thumbnail': null
|
||||||
},
|
},
|
||||||
'showdown' : {}
|
'showdown': {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const app = require('../src/app')(config);
|
const app = require('../src/app')(config);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
fs.mkdirSync(dataDir);
|
fs.mkdirSync(dataDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
if (fs.existsSync(dataDir)) {
|
if (fs.existsSync(dataDir)) {
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test root path', () => {
|
describe('Test root path', () => {
|
||||||
test('404 no index no error', (done) =>{
|
test('404 no index no error', (done) => {
|
||||||
request(app).get('/').then((response) =>{
|
request(app).get('/').then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
test('404 no index but error page', (done) =>{
|
});
|
||||||
fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>');
|
test('404 no index but error page', (done) => {
|
||||||
request(app).get('/').then((response) =>{
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
|
||||||
expect(response.statusCode).toBe(404);
|
request(app).get('/').then((response) => {
|
||||||
expect(response.text).toBe('error 404 at /');
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
expect(response.text).toBe('error 404 at /');
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('200 no articles', (done) =>{
|
});
|
||||||
fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>');
|
test('200 no articles', (done) => {
|
||||||
request(app).get('/').then((response) =>{
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
expect(response.statusCode).toBe(200);
|
request(app).get('/').then((response) => {
|
||||||
expect(response.text).toBe('articles 0');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('articles 0');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('200 2 articles', (done) => {
|
||||||
|
utils.createEmptyDirs([
|
||||||
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
|
path.join(dataDir, '2018', '05', '05')
|
||||||
|
]);
|
||||||
|
utils.createEmptyFiles([
|
||||||
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
|
path.join(dataDir, '2018', '05', '05', 'index.md')
|
||||||
|
]);
|
||||||
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
|
app.reload((res) => {
|
||||||
|
expect(res).toBe(true);
|
||||||
|
request(app).get('/').then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.text).toBe('articles 2');
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
test('200 2 articles', (done) =>{
|
|
||||||
utils.createEmptyDirs([
|
|
||||||
path.join(dataDir, '2019', '05', '05'),
|
|
||||||
path.join(dataDir, '2018', '05', '05')
|
|
||||||
]);
|
|
||||||
utils.createEmptyFiles([
|
|
||||||
path.join(dataDir, '2019', '05', '05','index.md'),
|
|
||||||
path.join(dataDir, '2018', '05', '05','index.md')
|
|
||||||
]);
|
|
||||||
fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>');
|
|
||||||
app.reload((res) => {
|
|
||||||
expect(res).toBe(true);
|
|
||||||
request(app).get('/').then((response) =>{
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
expect(response.text).toBe('articles 2');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test articles rendering', () => {
|
describe('Test articles rendering', () => {
|
||||||
test('404 article not found', (done) =>{
|
test('404 article not found', (done) => {
|
||||||
request(app).get('/2019/05/06/untitled/').then((response) =>{
|
request(app).get('/2019/05/06/untitled/').then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('500 no template', (done) =>{
|
test('500 no template', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05','index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
app.reload((res) => {
|
app.reload((res) => {
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
request(app).get('/2019/05/05/hello/').then((response) =>{
|
request(app).get('/2019/05/05/hello/').then((response) => {
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('200 rendered article', (done) =>{
|
test('200 rendered article', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
fs.writeFileSync(path.join(dataDir, '2019', '05', '05','index.md'), '# Hello');
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
|
||||||
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload((res) => {
|
app.reload((res) => {
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
request(app).get('/2019/05/05/hello/').then((response) =>{
|
request(app).get('/2019/05/05/hello/').then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('200 other url', (done) =>{
|
test('200 other url', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05','index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate)
|
path.join(dataDir, testTemplate)
|
||||||
]);
|
]);
|
||||||
app.reload((res) => {
|
app.reload((res) => {
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
request(app).get('/2019/05/05/anything/').then((response) =>{
|
request(app).get('/2019/05/05/anything/').then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('200 other url 2', (done) =>{
|
test('200 other url 2', (done) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05','index.md'),
|
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
||||||
path.join(dataDir, testTemplate)
|
path.join(dataDir, testTemplate)
|
||||||
]);
|
]);
|
||||||
app.reload((res) => {
|
app.reload((res) => {
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
request(app).get('/2019/05/05/').then((response) =>{
|
request(app).get('/2019/05/05/').then((response) => {
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('Test static files', () => {
|
describe('Test static files', () => {
|
||||||
test('404 invalid file no error page', (done) =>{
|
test('404 invalid file no error page', (done) => {
|
||||||
request(app).get('/somefile.txt').then((response) =>{
|
request(app).get('/somefile.txt').then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
test('404 invalid file but error page', (done) =>{
|
});
|
||||||
fs.writeFileSync(path.join(dataDir,testError), 'error <%= error %> at <%= path %>');
|
test('404 invalid file but error page', (done) => {
|
||||||
request(app).get('/somefile.txt').then((response) =>{
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
|
||||||
expect(response.statusCode).toBe(404);
|
request(app).get('/somefile.txt').then((response) => {
|
||||||
expect(response.text).toBe('error 404 at /somefile.txt');
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
expect(response.text).toBe('error 404 at /somefile.txt');
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('404 hidden file', (done) =>{
|
});
|
||||||
fs.writeFileSync(path.join(dataDir,'somefile.test'), '');
|
test('404 hidden file', (done) => {
|
||||||
request(app).get('/somefile.test').then((response) =>{
|
fs.writeFileSync(path.join(dataDir, 'somefile.test'), '');
|
||||||
expect(response.statusCode).toBe(404);
|
request(app).get('/somefile.test').then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(404);
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('200 valid file', (done) =>{
|
});
|
||||||
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
|
test('200 valid file', (done) => {
|
||||||
request(app).get('/somefile.txt').then((response) =>{
|
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
|
||||||
expect(response.statusCode).toBe(200);
|
request(app).get('/somefile.txt').then((response) => {
|
||||||
expect(response.text).toBe('filecontent');
|
expect(response.statusCode).toBe(200);
|
||||||
done();
|
expect(response.text).toBe('filecontent');
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test other requests', () => {
|
describe('Test other requests', () => {
|
||||||
test('400 POST', (done) =>{
|
test('400 POST', (done) => {
|
||||||
request(app).post('/').then((response) =>{
|
request(app).post('/').then((response) => {
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
test('400 PUT', (done) =>{
|
});
|
||||||
request(app).put('/').then((response) =>{
|
test('400 PUT', (done) => {
|
||||||
expect(response.statusCode).toBe(400);
|
request(app).put('/').then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(400);
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('400 DELETE', (done) =>{
|
});
|
||||||
request(app).delete('/').then((response) =>{
|
test('400 DELETE', (done) => {
|
||||||
expect(response.statusCode).toBe(400);
|
request(app).delete('/').then((response) => {
|
||||||
done();
|
expect(response.statusCode).toBe(400);
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+31
-31
@@ -5,50 +5,50 @@ const configFile = 'config.json';
|
|||||||
const tmpConfigFile = 'config.temp.json';
|
const tmpConfigFile = 'config.temp.json';
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
if (fs.existsSync(configFile)) {
|
if (fs.existsSync(configFile)) {
|
||||||
fs.renameSync(configFile, tmpConfigFile);
|
fs.renameSync(configFile, tmpConfigFile);
|
||||||
}
|
}
|
||||||
expect(fs.existsSync(configFile)).toBeFalsy();
|
expect(fs.existsSync(configFile)).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
if (fs.existsSync(tmpConfigFile)) {
|
if (fs.existsSync(tmpConfigFile)) {
|
||||||
fs.renameSync(tmpConfigFile, configFile);
|
fs.renameSync(tmpConfigFile, configFile);
|
||||||
} else if (fs.existsSync(configFile)) {
|
} else if (fs.existsSync(configFile)) {
|
||||||
fs.unlinkSync(configFile); //remove config file if remaining
|
fs.unlinkSync(configFile); //remove config file if remaining
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('no config', () => {
|
test('no config', () => {
|
||||||
if (fs.existsSync(configFile))
|
if (fs.existsSync(configFile))
|
||||||
fs.unlinkSync(configFile);
|
fs.unlinkSync(configFile);
|
||||||
expect(fs.existsSync(configFile)).toBeFalsy();
|
expect(fs.existsSync(configFile)).toBeFalsy();
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['node_port']).toBe(3000);
|
expect(config['node_port']).toBe(3000);
|
||||||
expect(config['data_dir']).toBe('data');
|
expect(config['data_dir']).toBe('data');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('invalid config ignored', () => {
|
test('invalid config ignored', () => {
|
||||||
fs.writeFileSync(configFile, 'invalid JSON');
|
fs.writeFileSync(configFile, 'invalid JSON');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['node_port']).toBe(3000);
|
expect(config['node_port']).toBe(3000);
|
||||||
expect(config['data_dir']).toBe('data');
|
expect(config['data_dir']).toBe('data');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('good config merged', () => {
|
test('good config merged', () => {
|
||||||
fs.writeFileSync(configFile, '{"node_port":5000}');
|
fs.writeFileSync(configFile, '{"node_port":5000}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['node_port']).toBe(5000);
|
expect(config['node_port']).toBe(5000);
|
||||||
expect(config['data_dir']).toBe('data');
|
expect(config['data_dir']).toBe('data');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('wrong config fixed', () => {
|
test('wrong config fixed', () => {
|
||||||
fs.writeFileSync(configFile, '{"node_port":"hello","data_dir":"data2"}');
|
fs.writeFileSync(configFile, '{"node_port":"hello","data_dir":"data2"}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['node_port']).toBe(3000);
|
expect(config['node_port']).toBe(3000);
|
||||||
expect(config['data_dir']).toBe('data2');
|
expect(config['data_dir']).toBe('data2');
|
||||||
});
|
});
|
||||||
+200
-200
@@ -7,265 +7,265 @@ const dataDir = 'test_data';
|
|||||||
const testIndex = 'testindex.md';
|
const testIndex = 'testindex.md';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
'test': true,
|
||||||
'data_dir': dataDir,
|
'data_dir': dataDir,
|
||||||
'article': {
|
'article': {
|
||||||
'index': testIndex,
|
'index': testIndex,
|
||||||
'default_title': 'Untitled',
|
'default_title': 'Untitled',
|
||||||
'default_thumbnail': 'default.png',
|
'default_thumbnail': 'default.png',
|
||||||
'thumbnail_tag': 'thumbnail'
|
'thumbnail_tag': 'thumbnail'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const fw = require('../src/file_walker')(config);
|
const fw = require('../src/file_walker')(config);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
fs.mkdirSync(dataDir);
|
fs.mkdirSync(dataDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
if (fs.existsSync(dataDir)) {
|
if (fs.existsSync(dataDir)) {
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test function fileTree', () => {
|
describe('Test function fileTree', () => {
|
||||||
test('empty root', (done) => {
|
test('empty root', (done) => {
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(list).toBeDefined();
|
expect(list).toBeDefined();
|
||||||
expect(list.length).toBe(0);
|
expect(list.length).toBe(0);
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
test('empty folders', (done) => {
|
});
|
||||||
utils.createEmptyDirs([
|
test('empty folders', (done) => {
|
||||||
path.join(dataDir, 'test', 'test'),
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, 'test', 'test2'),
|
path.join(dataDir, 'test', 'test'),
|
||||||
path.join(dataDir, 'test2')
|
path.join(dataDir, 'test', 'test2'),
|
||||||
]);
|
path.join(dataDir, 'test2')
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
]);
|
||||||
expect(err).toBeNull();
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
expect(list).toBeDefined();
|
expect(err).toBeNull();
|
||||||
expect(list.length).toBe(0);
|
expect(list).toBeDefined();
|
||||||
done();
|
expect(list.length).toBe(0);
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('simple files', (done) => {
|
});
|
||||||
const fileList = [
|
test('simple files', (done) => {
|
||||||
path.join(dataDir, 'f1.txt'),
|
const fileList = [
|
||||||
path.join(dataDir, 'f2.txt')
|
path.join(dataDir, 'f1.txt'),
|
||||||
];
|
path.join(dataDir, 'f2.txt')
|
||||||
utils.createEmptyFiles(fileList);
|
];
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
utils.createEmptyFiles(fileList);
|
||||||
expect(err).toBeNull();
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
expect(list).toBeDefined();
|
expect(err).toBeNull();
|
||||||
expect(list.length).toBe(fileList.length);
|
expect(list).toBeDefined();
|
||||||
expect(list).toEqual(expect.arrayContaining(fileList));
|
expect(list.length).toBe(fileList.length);
|
||||||
done();
|
expect(list).toEqual(expect.arrayContaining(fileList));
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('nested files', (done) => {
|
});
|
||||||
utils.createEmptyDirs([
|
test('nested files', (done) => {
|
||||||
path.join(dataDir, 'test', 'test'),
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, 'test2')
|
path.join(dataDir, 'test', 'test'),
|
||||||
]);
|
path.join(dataDir, 'test2')
|
||||||
const fileList = [
|
]);
|
||||||
path.join(dataDir, 'f1.txt'),
|
const fileList = [
|
||||||
path.join(dataDir, 'test', 'f2.txt'),
|
path.join(dataDir, 'f1.txt'),
|
||||||
path.join(dataDir, 'test', 'test', 'f3.txt'),
|
path.join(dataDir, 'test', 'f2.txt'),
|
||||||
path.join(dataDir, 'test2', 'f4.txt')
|
path.join(dataDir, 'test', 'test', 'f3.txt'),
|
||||||
];
|
path.join(dataDir, 'test2', 'f4.txt')
|
||||||
utils.createEmptyFiles(fileList);
|
];
|
||||||
fw.fileTree(dataDir, (err, list) => {
|
utils.createEmptyFiles(fileList);
|
||||||
expect(err).toBeNull();
|
fw.fileTree(dataDir, (err, list) => {
|
||||||
expect(list).toBeDefined();
|
expect(err).toBeNull();
|
||||||
expect(list.length).toBe(fileList.length);
|
expect(list).toBeDefined();
|
||||||
expect(list).toEqual(expect.arrayContaining(fileList));
|
expect(list.length).toBe(fileList.length);
|
||||||
done();
|
expect(list).toEqual(expect.arrayContaining(fileList));
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('invalid root', (done) => {
|
});
|
||||||
fw.fileTree('invalid root', (err, list) => {
|
test('invalid root', (done) => {
|
||||||
expect(err).not.toBeNull();
|
fw.fileTree('invalid root', (err, list) => {
|
||||||
expect(list).not.toBeDefined();
|
expect(err).not.toBeNull();
|
||||||
done();
|
expect(list).not.toBeDefined();
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test index article reading', () => {
|
describe('Test index article reading', () => {
|
||||||
const file = path.join(dataDir, testIndex);
|
const file = path.join(dataDir, testIndex);
|
||||||
|
|
||||||
test('invalid file', (done) => {
|
|
||||||
fw.readIndexFile('invalid file', 'thumbnail', (err, info) => {
|
|
||||||
expect(err).not.toBeNull();
|
|
||||||
expect(info).not.toBeDefined();
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
test('invalid file', (done) => {
|
||||||
|
fw.readIndexFile('invalid file', 'thumbnail', (err, info) => {
|
||||||
|
expect(err).not.toBeNull();
|
||||||
|
expect(info).not.toBeDefined();
|
||||||
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('correct file', (done) => {
|
});
|
||||||
fs.writeFileSync(file, `
|
|
||||||
|
test('correct file', (done) => {
|
||||||
|
fs.writeFileSync(file, `
|
||||||
# This is an awesome title !?¤
|
# This is an awesome title !?¤
|
||||||

|

|
||||||
this is some text
|
this is some text
|
||||||
`);
|
`);
|
||||||
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toBeDefined();
|
expect(info).toBeDefined();
|
||||||
expect(info.title).toBe('This is an awesome title !?¤');
|
expect(info.title).toBe('This is an awesome title !?¤');
|
||||||
expect(info.thumbnail).toBe('./thumbnail.jpg');
|
expect(info.thumbnail).toBe('./thumbnail.jpg');
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('no title', (done) => {
|
test('no title', (done) => {
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
## This is an awesome title !?¤
|
## This is an awesome title !?¤
|
||||||

|

|
||||||
### this is some text
|
### this is some text
|
||||||
`);
|
`);
|
||||||
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toBeDefined();
|
expect(info).toBeDefined();
|
||||||
expect(info.title).not.toBeDefined();
|
expect(info.title).not.toBeDefined();
|
||||||
expect(info.thumbnail).toBe('./thumbnail.jpg');
|
expect(info.thumbnail).toBe('./thumbnail.jpg');
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('title at beginning', (done) => {
|
test('title at beginning', (done) => {
|
||||||
fs.writeFileSync(file, '#title');
|
fs.writeFileSync(file, '#title');
|
||||||
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toBeDefined();
|
expect(info).toBeDefined();
|
||||||
expect(info.title).toBe('title');
|
expect(info.title).toBe('title');
|
||||||
expect(info.thumbnail).not.toBeDefined();
|
expect(info.thumbnail).not.toBeDefined();
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('no thumbnail', (done) => {
|
test('no thumbnail', (done) => {
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
# This is an awesome title !?¤
|
# This is an awesome title !?¤
|
||||||

|

|
||||||
this is some text
|
this is some text
|
||||||
`);
|
`);
|
||||||
fw.readIndexFile(file, 'thumbnail', (err, info) => {
|
fw.readIndexFile(file, 'thumbnail', (err, info) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toBeDefined();
|
expect(info).toBeDefined();
|
||||||
expect(info.title).toBe('This is an awesome title !?¤');
|
expect(info.title).toBe('This is an awesome title !?¤');
|
||||||
expect(info.thumbnail).not.toBeDefined();
|
expect(info.thumbnail).not.toBeDefined();
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('multiple thumbnails', (done) => {
|
test('multiple thumbnails', (done) => {
|
||||||
fs.writeFileSync(file, `
|
fs.writeFileSync(file, `
|
||||||
# This is an awesome title !?¤
|
# This is an awesome title !?¤
|
||||||

|

|
||||||
this is some text
|
this is some text
|
||||||

|

|
||||||
`);
|
`);
|
||||||
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(info).toBeDefined();
|
expect(info).toBeDefined();
|
||||||
expect(info.title).toBe('This is an awesome title !?¤');
|
expect(info.title).toBe('This is an awesome title !?¤');
|
||||||
expect(info.thumbnail).toBe('./thumbnail.jpg');
|
expect(info.thumbnail).toBe('./thumbnail.jpg');
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test article fetching', () => {
|
describe('Test article fetching', () => {
|
||||||
test('invalid data dir', (done) => {
|
test('invalid data dir', (done) => {
|
||||||
config['data_dir'] = 'invalid root';
|
config['data_dir'] = 'invalid root';
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, list) => {
|
||||||
expect(err).not.toBeNull();
|
expect(err).not.toBeNull();
|
||||||
expect(list).not.toBeDefined();
|
expect(list).not.toBeDefined();
|
||||||
config['data_dir'] = dataDir;
|
config['data_dir'] = dataDir;
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
test('empty data dir', (done) => {
|
});
|
||||||
fw.fetchArticles((err, dict) => {
|
test('empty data dir', (done) => {
|
||||||
expect(err).toBeNull();
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(dict).toBeDefined();
|
expect(err).toBeNull();
|
||||||
expect(Object.keys(dict).length).toBe(0);
|
expect(dict).toBeDefined();
|
||||||
done();
|
expect(Object.keys(dict).length).toBe(0);
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('misplaced index file', (done) => {
|
});
|
||||||
utils.createEmptyDirs([
|
test('misplaced index file', (done) => {
|
||||||
path.join(dataDir, 'test', 'test'),
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, '2019', '05', '05')
|
path.join(dataDir, 'test', 'test'),
|
||||||
]);
|
path.join(dataDir, '2019', '05', '05')
|
||||||
utils.createEmptyFiles([
|
]);
|
||||||
path.join(dataDir, testIndex),
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, 'test', 'test', testIndex),
|
path.join(dataDir, testIndex),
|
||||||
path.join(dataDir, '2019', '05', testIndex)
|
path.join(dataDir, 'test', 'test', testIndex),
|
||||||
]);
|
path.join(dataDir, '2019', '05', testIndex)
|
||||||
fw.fetchArticles((err, dict) => {
|
]);
|
||||||
expect(err).toBeNull();
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(dict).toBeDefined();
|
expect(err).toBeNull();
|
||||||
expect(Object.keys(dict).length).toBe(0);
|
expect(dict).toBeDefined();
|
||||||
done();
|
expect(Object.keys(dict).length).toBe(0);
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('empty index file', (done) => {
|
});
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
test('empty index file', (done) => {
|
||||||
const file = path.join(dir, testIndex);
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
utils.createEmptyDirs([dir]);
|
const file = path.join(dir, testIndex);
|
||||||
utils.createEmptyFiles([file]);
|
utils.createEmptyDirs([dir]);
|
||||||
fw.fetchArticles((err, dict) => {
|
utils.createEmptyFiles([file]);
|
||||||
expect(err).toBeNull();
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(dict).toBeDefined();
|
expect(err).toBeNull();
|
||||||
expect(Object.keys(dict).length).toBe(1);
|
expect(dict).toBeDefined();
|
||||||
expect(dict[path.join('2019', '05', '05')]).toEqual({
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
path: path.join('2019', '05', '05'),
|
expect(dict[path.join('2019', '05', '05')]).toEqual({
|
||||||
realPath: dir,
|
path: path.join('2019', '05', '05'),
|
||||||
year: 2019,
|
realPath: dir,
|
||||||
month: 5,
|
year: 2019,
|
||||||
day: 5,
|
month: 5,
|
||||||
date : new Date(2019,5,5),
|
day: 5,
|
||||||
title: 'Untitled',
|
date: new Date(2019, 5, 5),
|
||||||
thumbnail: 'default.png',
|
title: 'Untitled',
|
||||||
escapedTitle: 'untitled',
|
thumbnail: 'default.png',
|
||||||
url: '/'+path.join('2019', '05', '05', 'untitled')+'/',
|
escapedTitle: 'untitled',
|
||||||
});
|
url: '/' + path.join('2019', '05', '05', 'untitled') + '/',
|
||||||
done();
|
});
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
test('correct index file', (done) => {
|
});
|
||||||
const dir = path.join(dataDir, '2019', '05', '05');
|
test('correct index file', (done) => {
|
||||||
const file = path.join(dir, testIndex);
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
utils.createEmptyDirs([dir]);
|
const file = path.join(dir, testIndex);
|
||||||
fs.writeFileSync(file, `
|
utils.createEmptyDirs([dir]);
|
||||||
|
fs.writeFileSync(file, `
|
||||||
# Title with : info !
|
# Title with : info !
|
||||||

|

|
||||||
this is some text
|
this is some text
|
||||||
`);
|
`);
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(dict).toBeDefined();
|
expect(dict).toBeDefined();
|
||||||
expect(Object.keys(dict).length).toBe(1);
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
expect(dict[path.join('2019', '05', '05')]).toEqual({
|
expect(dict[path.join('2019', '05', '05')]).toEqual({
|
||||||
path: path.join('2019', '05', '05'),
|
path: path.join('2019', '05', '05'),
|
||||||
realPath: dir,
|
realPath: dir,
|
||||||
year: 2019,
|
year: 2019,
|
||||||
month: 5,
|
month: 5,
|
||||||
day: 5,
|
day: 5,
|
||||||
date : new Date(2019,5,5),
|
date: new Date(2019, 5, 5),
|
||||||
title: 'Title with : info !',
|
title: 'Title with : info !',
|
||||||
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
|
thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'),
|
||||||
escapedTitle: 'title_with___info',
|
escapedTitle: 'title_with___info',
|
||||||
url: '/'+path.join('2019', '05', '05', 'title_with___info')+'/',
|
url: '/' + path.join('2019', '05', '05', 'title_with___info') + '/',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+26
-26
@@ -7,47 +7,47 @@ const dataDir = 'test_data';
|
|||||||
const file = path.join(dataDir, 'test.md');
|
const file = path.join(dataDir, 'test.md');
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'showdown': {
|
'showdown': {
|
||||||
'simplifiedAutoLink': true,
|
'simplifiedAutoLink': true,
|
||||||
'smartIndentationFix': true
|
'smartIndentationFix': true
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderer = require('../src/renderer')(config);
|
const renderer = require('../src/renderer')(config);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
fs.mkdirSync(dataDir);
|
fs.mkdirSync(dataDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
if (fs.existsSync(dataDir)) {
|
if (fs.existsSync(dataDir)) {
|
||||||
utils.deleteFolderSync(dataDir);
|
utils.deleteFolderSync(dataDir);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('invalid file', (done) => {
|
test('invalid file', (done) => {
|
||||||
renderer.render('invalid file', (err, html) => {
|
renderer.render('invalid file', (err, html) => {
|
||||||
expect(err).not.toBeNull();
|
expect(err).not.toBeNull();
|
||||||
expect(html).not.toBeDefined();
|
expect(html).not.toBeDefined();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('normal file', (done) => {
|
test('normal file', (done) => {
|
||||||
fs.writeFileSync(file, `# Hello`);
|
fs.writeFileSync(file, `# Hello`);
|
||||||
renderer.render(file, (err, html) => {
|
renderer.render(file, (err, html) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(html).toBe('<h1 id="hello">Hello</h1>');
|
expect(html).toBe('<h1 id="hello">Hello</h1>');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('custom rules', (done) => {
|
test('custom rules', (done) => {
|
||||||
fs.writeFileSync(file, `www.google.com`);
|
fs.writeFileSync(file, `www.google.com`);
|
||||||
renderer.render(file, (err, html) => {
|
renderer.render(file, (err, html) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
expect(html).toBe('<p><a href="http://www.google.com">www.google.com</a></p>');
|
expect(html).toBe('<p><a href="http://www.google.com">www.google.com</a></p>');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+12
-12
@@ -2,19 +2,19 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const deleteFolderSync = (dir) => {
|
const deleteFolderSync = (dir) => {
|
||||||
if (!fs.existsSync(dir))
|
if (!fs.existsSync(dir))
|
||||||
return;
|
return;
|
||||||
fs.readdirSync(dir, {withFileTypes: true}).forEach((item) => {
|
fs.readdirSync(dir, {withFileTypes: true}).forEach((item) => {
|
||||||
if (item.isDirectory())
|
if (item.isDirectory())
|
||||||
deleteFolderSync(path.join(dir,item.name));
|
deleteFolderSync(path.join(dir, item.name));
|
||||||
else
|
else
|
||||||
fs.unlinkSync(path.join(dir,item.name));
|
fs.unlinkSync(path.join(dir, item.name));
|
||||||
});
|
});
|
||||||
fs.rmdirSync(dir);
|
fs.rmdirSync(dir);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
deleteFolderSync: deleteFolderSync,
|
deleteFolderSync: deleteFolderSync,
|
||||||
createEmptyDirs: (list) =>list.forEach((path) =>fs.mkdirSync(path, {recursive: true})),
|
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, {recursive: true})),
|
||||||
createEmptyFiles: (list) =>list.forEach((file) =>fs.writeFileSync(file, '')),
|
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user