Compare commits

...

3 Commits

Author SHA1 Message Date
Klemek 378ed438b6 Merge pull request #15 from Klemek/dev
v1.2.4
2019-07-01 23:03:23 +02:00
Klemek 3b07b6b9c5 Drafted articles 2019-07-01 22:18:40 +02:00
Klemek b6afcd4992 Update README.md 2019-06-26 21:20:45 +02:00
7 changed files with 98 additions and 18 deletions
+10 -2
View File
@@ -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
@@ -146,6 +150,8 @@ In your template, the following data is sent :
* `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) : the error code * `error` (error pages only) : the error code
</p>
</details>
#### 5. Create and init your git source #### 5. Create and init your git source
@@ -280,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)
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "gitblog.md", "name": "gitblog.md",
"version": "1.2.3", "version": "1.2.4",
"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": {
+5 -1
View File
@@ -144,7 +144,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))
});
}); });
}); });
+1
View File
@@ -24,6 +24,7 @@
}, },
"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",
+7 -5
View File
@@ -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,6 +81,7 @@ 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]),
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]),
year: parseInt(p[0]), year: parseInt(p[0]),
month: parseInt(p[1]), month: parseInt(p[1]),
@@ -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(path.join(article.realPath, p[3]), 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);
+11 -8
View File
@@ -121,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();
}); });
}); });
@@ -160,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(() => {
@@ -394,10 +397,10 @@ 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();
}); });
}); });
+63 -1
View File
@@ -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'
@@ -238,6 +239,7 @@ describe('Test article fetching', () => {
realPath: dir, realPath: dir,
year: 2019, year: 2019,
month: 5, month: 5,
draft: false,
day: 5, day: 5,
date: date, date: date,
title: 'Untitled', title: 'Untitled',
@@ -269,6 +271,7 @@ describe('Test article fetching', () => {
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 !
![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();
});
});
}); });