Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de26feb05c | |||
| 8bb455b576 | |||
| 378ed438b6 | |||
| 3b07b6b9c5 | |||
| b6afcd4992 | |||
| 35fcdc7320 | |||
| dfb93b6764 | |||
| 6af4012522 | |||
| 1b91002c03 |
@@ -127,6 +127,10 @@ Resources are located on the `data` folder and can be referenced as the root of
|
|||||||
|
|
||||||
In your template, the following data is sent :
|
In your template, the following data is sent :
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>details (click)</summary>
|
||||||
|
<p>
|
||||||
|
|
||||||
* `info` (every pages)
|
* `info` (every pages)
|
||||||
* `title` : the blog's title as in the config
|
* `title` : the blog's title as in the config
|
||||||
* `description` the blog's description as in the config
|
* `description` the blog's description as in the config
|
||||||
@@ -145,9 +149,9 @@ In your template, the following data is sent :
|
|||||||
* `path` : the URL path for the folder of the article (without the title)
|
* `path` : the URL path for the folder of the article (without the title)
|
||||||
* `realPath` : the system's path for the folder
|
* `realPath` : the system's path for the folder
|
||||||
* `escapedTitle` : the code with alphanumeric and underscore characters only
|
* `escapedTitle` : the code with alphanumeric and underscore characters only
|
||||||
* `error` (error pages only)
|
* `error` (error pages only) : the error code
|
||||||
* `error` : the error code
|
</p>
|
||||||
* `path` : the resource that caused the error
|
</details>
|
||||||
|
|
||||||
#### 5. Create and init your git source
|
#### 5. Create and init your git source
|
||||||
|
|
||||||
@@ -282,11 +286,13 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
|
|||||||
* `error` (default: error.ejs)
|
* `error` (default: error.ejs)
|
||||||
the name of the error page template on the data directory
|
the name of the error page template on the data directory
|
||||||
it will receive `error`, the error code
|
it will receive `error`, the error code
|
||||||
* `hidden` (default: `[.ejs]`)
|
* `hidden` (default: `[*.ejs,/.git*]`)
|
||||||
file extensions to be returned 404 when reached
|
path matches to be returned 404 when reached
|
||||||
* `article`
|
* `article`
|
||||||
* `index` (default: index.md)
|
* `index` (default: index.md)
|
||||||
the name of the Markdown page of the article on the `/year/month/day/` directory
|
the name of the Markdown page of the article on the `/year/month/day/` directory
|
||||||
|
* `draft` (default: draft.md)
|
||||||
|
the name of the Markdown page of an article not shown on the list
|
||||||
* `template` (default: template.ejs)
|
* `template` (default: template.ejs)
|
||||||
the name of the article page template on the data directory
|
the name of the article page template on the data directory
|
||||||
* `thumbnail_tag`: (default: thumbnail)
|
* `thumbnail_tag`: (default: thumbnail)
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gitblog.md",
|
"name": "gitblog.md",
|
||||||
"version": "1.2.2",
|
"version": "1.2.5",
|
||||||
"description": "A static blog using Markdown pulled from your git repository.",
|
"description": "A static blog using Markdown pulled from your git repository.",
|
||||||
"main": "src/server.js",
|
"main": "src/server.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -48,6 +48,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nodemonConfig": {
|
"nodemonConfig": {
|
||||||
|
"verbose": true,
|
||||||
"ignore": [
|
"ignore": [
|
||||||
"test/*",
|
"test/*",
|
||||||
"sample_data/*",
|
"sample_data/*",
|
||||||
@@ -55,7 +56,6 @@
|
|||||||
"uml/*",
|
"uml/*",
|
||||||
"*.log",
|
"*.log",
|
||||||
"README.md"
|
"README.md"
|
||||||
],
|
]
|
||||||
"delay": "2500"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title><%= info.title %> - Error <%= error %></title>
|
|
||||||
<%- include('head'); %>
|
<%- include('head'); %>
|
||||||
|
<title><%= info.title %> - Error <%= error %></title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
|
|||||||
@@ -1,21 +1,26 @@
|
|||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
|
||||||
|
<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
|
||||||
|
|
||||||
<meta name="twitter:card" content="summary_large_image">
|
<meta name="twitter:card" content="summary_large_image">
|
||||||
|
<%- `<meta property="og:description" content="${info.description}">` %>
|
||||||
|
<%- `<meta property="twitter:description" content="${info.description}">` %>
|
||||||
<% if(locals.article){ %>
|
<% if(locals.article){ %>
|
||||||
|
<%- `<meta property="org:url" content="${info.host + article.url}">` %>
|
||||||
<%- `<meta property="og:title" content="${info.title} - ${article.title}">` %>
|
<%- `<meta property="og:title" content="${info.title} - ${article.title}">` %>
|
||||||
<%- `<meta property="twitter:title" content="${info.title} - ${article.title}">` %>
|
<%- `<meta property="twitter:title" content="${info.title} - ${article.title}">` %>
|
||||||
<%- `<meta property="og:description" content="${info.description}">` %>
|
|
||||||
<%- `<meta property="twitter:description" content="${info.description}">` %>
|
|
||||||
<%- `<meta property="org:url" content="${info.host + article.url}">` %>
|
|
||||||
<% if (article.thumbnail) { %>
|
<% if (article.thumbnail) { %>
|
||||||
<%- `<meta property="og:image" content="${info.host}/${article.thumbnail}">` %>
|
<%- `<meta property="og:image" content="${info.host}/${article.thumbnail}">` %>
|
||||||
<%- `<meta property="twitter:image" content="${info.host}/${article.thumbnail}">` %>
|
<%- `<meta property="twitter:image" content="${info.host}/${article.thumbnail}">` %>
|
||||||
<% } %>
|
<% } %>
|
||||||
<link rel="stylesheet" type="text/css" href="/prism.css">
|
|
||||||
|
<link rel="stylesheet" type="text/css" href="/css/prism.css">
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
|
<%- `<meta property="org:url" content="${info.host}/">` %>
|
||||||
<%- `<meta property="og:title" content="${info.title} - Home">` %>
|
<%- `<meta property="og:title" content="${info.title} - Home">` %>
|
||||||
<%- `<meta property="twitter:title" content="${info.title} - Home">` %>
|
<%- `<meta property="twitter:title" content="${info.title} - Home">` %>
|
||||||
<%- `<meta property="og:description" content="${info.description}">` %>
|
|
||||||
<%- `<meta property="twitter:description" content="${info.description}">` %>
|
<%- `<meta property="description" content="${info.description}">` %>
|
||||||
<%- `<meta property="org:url" content="${info.host}/">` %>
|
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
||||||
<link rel="alternate" type="application/rss+xml" title="RSS feed" href="/rss"/>
|
<link rel="alternate" type="application/rss+xml" title="RSS feed" href="/rss"/>
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title><%= info.title %> - Home</title>
|
|
||||||
<%- include('head'); %>
|
<%- include('head'); %>
|
||||||
|
<title><%= info.title %> - Home</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title><%= info.title %> - <%= article.title %></title>
|
|
||||||
<%- include('head'); %>
|
<%- include('head'); %>
|
||||||
|
<title><%= info.title %> - <%= article.title %></title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main class="article">
|
<main class="article">
|
||||||
|
|||||||
+42
-32
@@ -26,6 +26,28 @@ const cons = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = (config) => {
|
module.exports = (config) => {
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
let render;
|
||||||
|
/**
|
||||||
|
* Show an error with the correct page
|
||||||
|
* @param req
|
||||||
|
* @param res
|
||||||
|
* @param code - error code
|
||||||
|
*/
|
||||||
|
let showError;
|
||||||
const fw = require('./file_walker')(config);
|
const fw = require('./file_walker')(config);
|
||||||
const renderer = require('./renderer')(config);
|
const renderer = require('./renderer')(config);
|
||||||
|
|
||||||
@@ -38,12 +60,7 @@ module.exports = (config) => {
|
|||||||
let lastRSS = '';
|
let lastRSS = '';
|
||||||
let host = config['host'];
|
let host = config['host'];
|
||||||
|
|
||||||
/**
|
reload = (success, error) => {
|
||||||
* Fetch articles from the data folder and send success as a response
|
|
||||||
* @param success
|
|
||||||
* @param error
|
|
||||||
*/
|
|
||||||
const reload = (success, error) => {
|
|
||||||
fw.fetchArticles((err, dict) => {
|
fw.fetchArticles((err, dict) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(cons.error, 'error loading articles : ' + err);
|
console.error(cons.error, 'error loading articles : ' + err);
|
||||||
@@ -52,8 +69,9 @@ module.exports = (config) => {
|
|||||||
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;
|
||||||
|
const dnb = Object.values(articles).filter(a => a.draft).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' : ''} (${dnb} drafted)`);
|
||||||
else
|
else
|
||||||
console.log(cons.warn, `no articles loaded, check your configuration`);
|
console.log(cons.warn, `no articles loaded, check your configuration`);
|
||||||
|
|
||||||
@@ -65,15 +83,7 @@ module.exports = (config) => {
|
|||||||
if (config['test'])
|
if (config['test'])
|
||||||
app.reload = reload;
|
app.reload = reload;
|
||||||
|
|
||||||
/**
|
render = (req, res, vPath, data, code = 200) => {
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
const render = (req, res, vPath, data, code = 200) => {
|
|
||||||
data.info = {
|
data.info = {
|
||||||
title: config['home']['title'],
|
title: config['home']['title'],
|
||||||
description: config['home']['description'],
|
description: config['home']['description'],
|
||||||
@@ -83,27 +93,24 @@ module.exports = (config) => {
|
|||||||
config: config
|
config: config
|
||||||
};
|
};
|
||||||
res.render(vPath, data, (err, html) => {
|
res.render(vPath, data, (err, html) => {
|
||||||
if (err) {
|
if (err && vPath !== path.join(config['data_dir'], config['home']['error'])) {
|
||||||
|
console.log(cons.error, `failed to render page ${vPath} : ${err}`);
|
||||||
|
showError(req, res, 500);
|
||||||
|
} else if (err) {
|
||||||
res.sendStatus(500);
|
res.sendStatus(500);
|
||||||
console.log(cons.error, `failed to render ${vPath} : ${err}`);
|
console.log(cons.error, `failed to render error page : ${err}`);
|
||||||
} else
|
} else
|
||||||
res.status(code).send(html);
|
res.status(code).send(html);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
showError = (req, res, code) => {
|
||||||
* Show an error with the correct page
|
|
||||||
* @param req
|
|
||||||
* @param res
|
|
||||||
* @param code - error code
|
|
||||||
*/
|
|
||||||
const showError = (req, res, code) => {
|
|
||||||
const errorPath = path.join(config['data_dir'], config['home']['error']);
|
const errorPath = path.join(config['data_dir'], config['home']['error']);
|
||||||
fs.access(errorPath, fs.constants.R_OK, (err) => {
|
fs.access(errorPath, fs.constants.R_OK, (err) => {
|
||||||
if (err)
|
if (err)
|
||||||
res.sendStatus(code);
|
res.sendStatus(code);
|
||||||
else
|
else
|
||||||
render(req, res, errorPath, {error: code, path: req.path}, code);
|
render(req, res, errorPath, {error: code}, code);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -138,7 +145,11 @@ module.exports = (config) => {
|
|||||||
if (err)
|
if (err)
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
else
|
else
|
||||||
render(req, res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))});
|
render(req, res, homePath,
|
||||||
|
{
|
||||||
|
articles: Object.values(articles)
|
||||||
|
.filter(d => !d.draft).sort((a, b) => ('' + b.path).localeCompare(a.path))
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -210,7 +221,7 @@ module.exports = (config) => {
|
|||||||
if (!article)
|
if (!article)
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
else {
|
else {
|
||||||
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
|
renderer.render(article.realPath, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
|
||||||
return showError(req, res, 500);
|
return showError(req, res, 500);
|
||||||
@@ -232,11 +243,10 @@ module.exports = (config) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// catch all hidden file type and return 404
|
// catch all hidden file type and return 404
|
||||||
app.get('*', (req, res, next) => {
|
config['home']['hidden'].forEach(pathMatcher => {
|
||||||
if (config['home']['hidden'].includes(path.extname(req.path)))
|
app.get(pathMatcher, (req, res) => {
|
||||||
showError(req, res, 404);
|
showError(req, res, 404);
|
||||||
else
|
});
|
||||||
next();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// serve all static files via get
|
// serve all static files via get
|
||||||
|
|||||||
@@ -18,11 +18,13 @@
|
|||||||
"index": "index.ejs",
|
"index": "index.ejs",
|
||||||
"error": "error.ejs",
|
"error": "error.ejs",
|
||||||
"hidden": [
|
"hidden": [
|
||||||
".ejs"
|
"*.ejs",
|
||||||
|
"/.git*"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"article": {
|
"article": {
|
||||||
"index": "index.md",
|
"index": "index.md",
|
||||||
|
"draft": "draft.md",
|
||||||
"template": "template.ejs",
|
"template": "template.ejs",
|
||||||
"thumbnail_tag": "thumbnail",
|
"thumbnail_tag": "thumbnail",
|
||||||
"default_title": "Untitled",
|
"default_title": "Untitled",
|
||||||
|
|||||||
+8
-6
@@ -1,7 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/');
|
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all files path inside a given folder path
|
* Get all files path inside a given folder path
|
||||||
@@ -71,8 +71,8 @@ module.exports = (config) => {
|
|||||||
if (err)
|
if (err)
|
||||||
return cb(err);
|
return cb(err);
|
||||||
const paths = fileList
|
const paths = fileList
|
||||||
.map((p) => p.substr(config['data_dir'].length+1).split(path.sep))
|
.map((p) => p.substr(config['data_dir'].length + 1).split(path.sep))
|
||||||
.filter((p) => p.length === 4 && p[3] === config['article']['index'] &&
|
.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]));
|
/^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2]));
|
||||||
if (paths.length === 0)
|
if (paths.length === 0)
|
||||||
cb(null, {});
|
cb(null, {});
|
||||||
@@ -81,7 +81,8 @@ module.exports = (config) => {
|
|||||||
paths.forEach((p) => {
|
paths.forEach((p) => {
|
||||||
const article = {
|
const article = {
|
||||||
path: joinUrl(p[0], p[1], p[2]),
|
path: joinUrl(p[0], p[1], p[2]),
|
||||||
realPath: path.join(config['data_dir'], 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]),
|
year: parseInt(p[0]),
|
||||||
month: parseInt(p[1]),
|
month: parseInt(p[1]),
|
||||||
day: parseInt(p[2])
|
day: parseInt(p[2])
|
||||||
@@ -89,14 +90,15 @@ module.exports = (config) => {
|
|||||||
article.date = new Date(article.year, article.month, article.day);
|
article.date = new Date(article.year, article.month, article.day);
|
||||||
article.date.setUTCHours(0);
|
article.date.setUTCHours(0);
|
||||||
remaining++;
|
remaining++;
|
||||||
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
|
readIndexFile(article.realPath, 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 ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail'];
|
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.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_');
|
||||||
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
|
article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/';
|
||||||
articles[article.path] = article;
|
if (!articles[article.path] || !article.draft)
|
||||||
|
articles[article.path] = article;
|
||||||
remaining--;
|
remaining--;
|
||||||
if (remaining === 0)
|
if (remaining === 0)
|
||||||
cb(null, articles);
|
cb(null, articles);
|
||||||
|
|||||||
+60
-20
@@ -16,16 +16,15 @@ config['data_dir'] = dataDir;
|
|||||||
config['webhook']['endpoint'] = '/webhooktest';
|
config['webhook']['endpoint'] = '/webhooktest';
|
||||||
config['rss']['endpoint'] = '/rsstest';
|
config['rss']['endpoint'] = '/rsstest';
|
||||||
config['rss']['length'] = 2;
|
config['rss']['length'] = 2;
|
||||||
config['home']['index'] = testIndex;
|
|
||||||
config['home']['error'] = testError;
|
config['home']['error'] = testError;
|
||||||
config['article']['template'] = testTemplate;
|
config['article']['template'] = testTemplate;
|
||||||
|
|
||||||
const app = require('../src/app')(config);
|
const app = require('../src/app')(config);
|
||||||
|
|
||||||
beforeEach((done, fail) => {
|
beforeEach((done, fail) => {
|
||||||
|
config['home']['index'] = testIndex;
|
||||||
config['data_dir'] = dataDir;
|
config['data_dir'] = dataDir;
|
||||||
config['article']['index'] = 'index.md';
|
config['article']['index'] = 'index.md';
|
||||||
config['home']['hidden'] = ['.ejs', '.test'];
|
|
||||||
config['access_log'] = '';
|
config['access_log'] = '';
|
||||||
config['error_log'] = '';
|
config['error_log'] = '';
|
||||||
config['modules']['rss'] = true;
|
config['modules']['rss'] = true;
|
||||||
@@ -93,20 +92,20 @@ describe('Test request logging', () => {
|
|||||||
|
|
||||||
describe('Test error logging', () => {
|
describe('Test error logging', () => {
|
||||||
test('test no log', (done) => {
|
test('test no log', (done) => {
|
||||||
config['home']['hidden'] = null;
|
config['home']['index'] = null;
|
||||||
request(app).get('/somefile.txt').then(() => {
|
request(app).get('/').then(() => {
|
||||||
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
|
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('test null error ', (done) => {
|
test('test null error ', (done) => {
|
||||||
config['home']['hidden'] = null;
|
config['home']['index'] = null;
|
||||||
config['error_log'] = path.join(dataDir, 'error.log');
|
config['error_log'] = path.join(dataDir, 'error.log');
|
||||||
request(app).get('/somefile.txt').then(() => {
|
request(app).get('/').then(() => {
|
||||||
fs.readFile(path.join(dataDir, 'error.log'), {encoding: 'UTF-8'}, (err, data) => {
|
fs.readFile(path.join(dataDir, 'error.log'), {encoding: 'UTF-8'}, (err, data) => {
|
||||||
expect(err).toBeNull();
|
expect(err).toBeNull();
|
||||||
const start = data.split('\n').slice(0, 2).join('\n');
|
const start = data.split('\n').slice(0, 2).join('\n');
|
||||||
const expected = '500 GET /somefile.txt ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError: Cannot read property \'includes\' of null';
|
const expected = '500 GET / ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object';
|
||||||
expect(start).toBe(expected);
|
expect(start).toBe(expected);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -122,10 +121,10 @@ describe('Test root path', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 no index but error page', (done) => {
|
test('404 no index but error page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/').then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
expect(response.text).toBe('error 404 at /');
|
expect(response.text).toBe('error 404');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -136,6 +135,23 @@ describe('Test root path', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('500 render error with page', (done) => {
|
||||||
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
|
request(app).get('/').then((response) => {
|
||||||
|
expect(response.statusCode).toBe(500);
|
||||||
|
expect(response.text).toBe('error 500');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('500 render error with failing page', (done) => {
|
||||||
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
|
||||||
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= null.error %>');
|
||||||
|
request(app).get('/').then((response) => {
|
||||||
|
expect(response.statusCode).toBe(500);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
test('200 no articles', (done) => {
|
test('200 no articles', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
request(app).get('/').then((response) => {
|
request(app).get('/').then((response) => {
|
||||||
@@ -144,14 +160,17 @@ describe('Test root path', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 2 articles', (done, fail) => {
|
test('200 2 articles 1 drafted', (done, fail) => {
|
||||||
utils.createEmptyDirs([
|
utils.createEmptyDirs([
|
||||||
path.join(dataDir, '2019', '05', '05'),
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
path.join(dataDir, '2018', '05', '05')
|
path.join(dataDir, '2018', '05', '05'),
|
||||||
|
path.join(dataDir, '2017', '05', '05')
|
||||||
]);
|
]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
path.join(dataDir, '2019', '05', '05', 'index.md'),
|
path.join(dataDir, '2019', '05', '05', 'draft.md'),
|
||||||
path.join(dataDir, '2018', '05', '05', 'index.md')
|
path.join(dataDir, '2018', '05', '05', 'index.md'),
|
||||||
|
path.join(dataDir, '2018', '05', '05', 'draft.md'),
|
||||||
|
path.join(dataDir, '2017', '05', '05', 'index.md'),
|
||||||
]);
|
]);
|
||||||
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
@@ -303,12 +322,11 @@ describe('Test articles rendering', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('500 no index', (done, fail) => {
|
test('500 fail to render', (done, fail) => {
|
||||||
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), '<%- articl.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
app.reload(() => {
|
app.reload(() => {
|
||||||
config['article']['index'] = 'invalid.md';
|
|
||||||
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();
|
||||||
@@ -340,6 +358,19 @@ describe('Test articles rendering', () => {
|
|||||||
}, fail);
|
}, fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('200 rendered draft', (done, fail) => {
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
|
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'draft.md'), '# Hello');
|
||||||
|
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
|
||||||
|
app.reload(() => {
|
||||||
|
request(app).get('/2019/05/05/hello/').then((response) => {
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}, fail);
|
||||||
|
});
|
||||||
|
|
||||||
test('200 other url', (done, fail) => {
|
test('200 other url', (done, fail) => {
|
||||||
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
|
||||||
utils.createEmptyFiles([
|
utils.createEmptyFiles([
|
||||||
@@ -378,16 +409,25 @@ describe('Test static files', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 invalid file but error page', (done) => {
|
test('404 invalid file but error page', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
|
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
|
||||||
request(app).get('/somefile.txt').then((response) => {
|
request(app).get('/somefile.txt').then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
expect(response.text).toBe('error 404 at /somefile.txt');
|
expect(response.text).toBe('error 404');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('404 hidden file', (done) => {
|
test('404 hidden file', (done) => {
|
||||||
fs.writeFileSync(path.join(dataDir, 'somefile.test'), '');
|
utils.createEmptyDirs([path.join(dataDir, 'tmp')]);
|
||||||
request(app).get('/somefile.test').then((response) => {
|
fs.writeFileSync(path.join(dataDir, 'tmp', 'somefile.ejs'), '');
|
||||||
|
request(app).get('/tmp/somefile.ejs').then((response) => {
|
||||||
|
expect(response.statusCode).toBe(404);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('404 hidden folder', (done) => {
|
||||||
|
utils.createEmptyDirs([path.join(dataDir, '.git')]);
|
||||||
|
fs.writeFileSync(path.join(dataDir, '.git', 'file.txt'), '');
|
||||||
|
request(app).get('/.git/file.txt').then((response) => {
|
||||||
expect(response.statusCode).toBe(404);
|
expect(response.statusCode).toBe(404);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
+1
-1
@@ -77,5 +77,5 @@ test('array fix', () => {
|
|||||||
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
|
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
|
||||||
const config = require('../src/config')();
|
const config = require('../src/config')();
|
||||||
expect(config).toBeDefined();
|
expect(config).toBeDefined();
|
||||||
expect(config['home']['hidden']).toEqual(['.ejs']);
|
expect(config['home']['hidden']).toEqual(['*.ejs', '/.git*']);
|
||||||
});
|
});
|
||||||
@@ -6,13 +6,14 @@ const utils = require('./test_utils');
|
|||||||
const dataDir = 'test_data';
|
const dataDir = 'test_data';
|
||||||
const testIndex = 'testindex.md';
|
const testIndex = 'testindex.md';
|
||||||
|
|
||||||
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/');
|
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/');
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
'test': true,
|
'test': true,
|
||||||
'data_dir': dataDir,
|
'data_dir': dataDir,
|
||||||
'article': {
|
'article': {
|
||||||
'index': testIndex,
|
'index': testIndex,
|
||||||
|
'draft': 'draft.md',
|
||||||
'default_title': 'Untitled',
|
'default_title': 'Untitled',
|
||||||
'default_thumbnail': 'default.png',
|
'default_thumbnail': 'default.png',
|
||||||
'thumbnail_tag': 'thumbnail'
|
'thumbnail_tag': 'thumbnail'
|
||||||
@@ -235,9 +236,10 @@ describe('Test article fetching', () => {
|
|||||||
expect(Object.keys(dict).length).toBe(1);
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
|
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
|
||||||
path: joinUrl('2019', '05', '05'),
|
path: joinUrl('2019', '05', '05'),
|
||||||
realPath: dir,
|
realPath: file,
|
||||||
year: 2019,
|
year: 2019,
|
||||||
month: 5,
|
month: 5,
|
||||||
|
draft: false,
|
||||||
day: 5,
|
day: 5,
|
||||||
date: date,
|
date: date,
|
||||||
title: 'Untitled',
|
title: 'Untitled',
|
||||||
@@ -265,10 +267,11 @@ describe('Test article fetching', () => {
|
|||||||
expect(Object.keys(dict).length).toBe(1);
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
|
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
|
||||||
path: joinUrl('2019', '05', '05'),
|
path: joinUrl('2019', '05', '05'),
|
||||||
realPath: dir,
|
realPath: file,
|
||||||
year: 2019,
|
year: 2019,
|
||||||
month: 5,
|
month: 5,
|
||||||
day: 5,
|
day: 5,
|
||||||
|
draft: false,
|
||||||
date: date,
|
date: date,
|
||||||
title: 'Title with : info !',
|
title: 'Title with : info !',
|
||||||
thumbnail: joinUrl('2019', '05', '05', './thumbnail.jpg'),
|
thumbnail: joinUrl('2019', '05', '05', './thumbnail.jpg'),
|
||||||
@@ -278,5 +281,64 @@ describe('Test article fetching', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('correct draft file', (done) => {
|
||||||
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
|
const file = path.join(dir, 'draft.md');
|
||||||
|
utils.createEmptyDirs([dir]);
|
||||||
|
fs.writeFileSync(file, `
|
||||||
|
# Title with : info !
|
||||||
|

|
||||||
|
this is some text
|
||||||
|
`);
|
||||||
|
const date = new Date(2019, 5, 5);
|
||||||
|
date.setUTCHours(0);
|
||||||
|
fw.fetchArticles((err, dict) => {
|
||||||
|
expect(err).toBeNull();
|
||||||
|
expect(dict).toBeDefined();
|
||||||
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
|
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
|
||||||
|
path: joinUrl('2019', '05', '05'),
|
||||||
|
realPath: file,
|
||||||
|
year: 2019,
|
||||||
|
month: 5,
|
||||||
|
day: 5,
|
||||||
|
draft: true,
|
||||||
|
date: date,
|
||||||
|
title: 'Title with : info !',
|
||||||
|
thumbnail: joinUrl('2019', '05', '05', './thumbnail.jpg'),
|
||||||
|
escapedTitle: 'title_with___info',
|
||||||
|
url: '/' + joinUrl('2019', '05', '05', 'title_with___info') + '/',
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('index file override draft', (done) => {
|
||||||
|
const dir = path.join(dataDir, '2019', '05', '05');
|
||||||
|
const file = path.join(dir, testIndex);
|
||||||
|
const file2 = path.join(dir, 'draft.md');
|
||||||
|
utils.createEmptyDirs([dir]);
|
||||||
|
utils.createEmptyFiles([file, file2]);
|
||||||
|
const date = new Date(2019, 5, 5);
|
||||||
|
date.setUTCHours(0);
|
||||||
|
fw.fetchArticles((err, dict) => {
|
||||||
|
expect(err).toBeNull();
|
||||||
|
expect(dict).toBeDefined();
|
||||||
|
expect(Object.keys(dict).length).toBe(1);
|
||||||
|
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
|
||||||
|
path: joinUrl('2019', '05', '05'),
|
||||||
|
realPath: file,
|
||||||
|
year: 2019,
|
||||||
|
month: 5,
|
||||||
|
draft: false,
|
||||||
|
day: 5,
|
||||||
|
date: date,
|
||||||
|
title: 'Untitled',
|
||||||
|
thumbnail: 'default.png',
|
||||||
|
escapedTitle: 'untitled',
|
||||||
|
url: '/' + joinUrl('2019', '05', '05', 'untitled') + '/',
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user