From 3b07b6b9c5ba0bd9a0532685e6c0cc499f3b7ff1 Mon Sep 17 00:00:00 2001 From: Klemek Date: Mon, 1 Jul 2019 22:18:40 +0200 Subject: [PATCH] Drafted articles --- README.md | 2 ++ package.json | 2 +- src/app.js | 6 +++- src/config.default.json | 1 + src/file_walker.js | 12 ++++---- test/app.test.js | 19 +++++++----- test/file_walker.test.js | 64 +++++++++++++++++++++++++++++++++++++++- 7 files changed, 90 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 16a7cc9..baa206a 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,8 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link * `article` * `index` (default: index.md) 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) the name of the article page template on the data directory * `thumbnail_tag`: (default: thumbnail) diff --git a/package.json b/package.json index 3b561dd..ee82655 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gitblog.md", - "version": "1.2.3", + "version": "1.2.4", "description": "A static blog using Markdown pulled from your git repository.", "main": "src/server.js", "dependencies": { diff --git a/src/app.js b/src/app.js index 405953c..e9f0b5b 100644 --- a/src/app.js +++ b/src/app.js @@ -144,7 +144,11 @@ module.exports = (config) => { if (err) showError(req, res, 404); 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)) + }); }); }); diff --git a/src/config.default.json b/src/config.default.json index 0f62c17..63e3f92 100644 --- a/src/config.default.json +++ b/src/config.default.json @@ -24,6 +24,7 @@ }, "article": { "index": "index.md", + "draft": "draft.md", "template": "template.ejs", "thumbnail_tag": "thumbnail", "default_title": "Untitled", diff --git a/src/file_walker.js b/src/file_walker.js index b0bdcae..83ab260 100644 --- a/src/file_walker.js +++ b/src/file_walker.js @@ -1,7 +1,7 @@ const fs = require('fs'); 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 @@ -71,8 +71,8 @@ module.exports = (config) => { if (err) return cb(err); const paths = fileList - .map((p) => p.substr(config['data_dir'].length+1).split(path.sep)) - .filter((p) => p.length === 4 && p[3] === config['article']['index'] && + .map((p) => p.substr(config['data_dir'].length + 1).split(path.sep)) + .filter((p) => p.length === 4 && (p[3] === config['article']['index'] || p[3] === config['article']['draft']) && /^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2])); if (paths.length === 0) cb(null, {}); @@ -81,6 +81,7 @@ module.exports = (config) => { paths.forEach((p) => { const article = { path: joinUrl(p[0], p[1], p[2]), + draft: p[3] === config['article']['draft'], realPath: path.join(config['data_dir'], p[0], p[1], p[2]), year: parseInt(p[0]), month: parseInt(p[1]), @@ -89,14 +90,15 @@ module.exports = (config) => { article.date = new Date(article.year, article.month, article.day); article.date.setUTCHours(0); remaining++; - readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => { + readIndexFile(path.join(article.realPath, p[3]), config['article']['thumbnail_tag'], (err, info) => { if (err) return cb(err); article.title = info.title || config['article']['default_title']; article.thumbnail = info.thumbnail ? joinUrl(article.path, info.thumbnail) : config['article']['default_thumbnail']; article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_'); article.url = '/' + joinUrl(article.path, article.escapedTitle) + '/'; - articles[article.path] = article; + if (!articles[article.path] || !article.draft) + articles[article.path] = article; remaining--; if (remaining === 0) cb(null, articles); diff --git a/test/app.test.js b/test/app.test.js index b64914b..1d8c6e2 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -121,10 +121,10 @@ describe('Test root path', () => { }); }); 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) => { expect(response.statusCode).toBe(404); - expect(response.text).toBe('error 404 at /'); + expect(response.text).toBe('error 404'); done(); }); }); @@ -160,14 +160,17 @@ describe('Test root path', () => { done(); }); }); - test('200 2 articles', (done, fail) => { + test('200 2 articles 1 drafted', (done, fail) => { utils.createEmptyDirs([ 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([ - path.join(dataDir, '2019', '05', '05', 'index.md'), - path.join(dataDir, '2018', '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', 'draft.md'), + path.join(dataDir, '2017', '05', '05', 'index.md'), ]); fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>'); app.reload(() => { @@ -394,10 +397,10 @@ describe('Test static files', () => { }); }); 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) => { expect(response.statusCode).toBe(404); - expect(response.text).toBe('error 404 at /somefile.txt'); + expect(response.text).toBe('error 404'); done(); }); }); diff --git a/test/file_walker.test.js b/test/file_walker.test.js index 55ff7ce..519ad9e 100644 --- a/test/file_walker.test.js +++ b/test/file_walker.test.js @@ -6,13 +6,14 @@ const utils = require('./test_utils'); const dataDir = 'test_data'; const testIndex = 'testindex.md'; -const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/'); +const joinUrl = (...paths) => path.join(...paths).replace(/\\/g, '/'); const config = { 'test': true, 'data_dir': dataDir, 'article': { 'index': testIndex, + 'draft': 'draft.md', 'default_title': 'Untitled', 'default_thumbnail': 'default.png', 'thumbnail_tag': 'thumbnail' @@ -238,6 +239,7 @@ describe('Test article fetching', () => { realPath: dir, year: 2019, month: 5, + draft: false, day: 5, date: date, title: 'Untitled', @@ -269,6 +271,7 @@ describe('Test article fetching', () => { year: 2019, month: 5, day: 5, + draft: false, date: date, title: 'Title with : info !', thumbnail: joinUrl('2019', '05', '05', './thumbnail.jpg'), @@ -278,5 +281,64 @@ describe('Test article fetching', () => { 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 ! + ![thumbnail](./thumbnail.jpg) + 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: dir, + 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: dir, + year: 2019, + month: 5, + draft: false, + day: 5, + date: date, + title: 'Untitled', + thumbnail: 'default.png', + escapedTitle: 'untitled', + url: '/' + joinUrl('2019', '05', '05', 'untitled') + '/', + }); + done(); + }); + }); });