Fixed draft rendering bug

This commit is contained in:
Klemek
2019-07-01 23:15:13 +02:00
parent 3b07b6b9c5
commit 8bb455b576
5 changed files with 25 additions and 12 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "gitblog.md", "name": "gitblog.md",
"version": "1.2.4", "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": {
+3 -2
View File
@@ -69,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`);
@@ -220,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);
+2 -2
View File
@@ -82,7 +82,7 @@ module.exports = (config) => {
const article = { const article = {
path: joinUrl(p[0], p[1], p[2]), path: joinUrl(p[0], p[1], p[2]),
draft: p[3] === config['article']['draft'], draft: p[3] === config['article']['draft'],
realPath: path.join(config['data_dir'], p[0], p[1], p[2]), 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])
@@ -90,7 +90,7 @@ 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, p[3]), 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'];
+15 -3
View File
@@ -322,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();
@@ -359,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([
+4 -4
View File
@@ -236,7 +236,7 @@ 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, draft: false,
@@ -267,7 +267,7 @@ 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,
@@ -298,7 +298,7 @@ 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,
@@ -326,7 +326,7 @@ 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, draft: false,