Skip to content

Commit

Permalink
Merge pull request #15 from Klemek/dev
Browse files Browse the repository at this point in the history
v1.2.4
  • Loading branch information
Klemek committed Jul 1, 2019
2 parents b6afcd4 + 3b07b6b commit 378ed43
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 5 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
});
});
});

Expand Down
1 change: 1 addition & 0 deletions src/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"article": {
"index": "index.md",
"draft": "draft.md",
"template": "template.ejs",
"thumbnail_tag": "thumbnail",
"default_title": "Untitled",
Expand Down
12 changes: 7 additions & 5 deletions src/file_walker.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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, {});
Expand All @@ -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]),
Expand All @@ -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);
Expand Down
19 changes: 11 additions & 8 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
});
});
Expand Down
64 changes: 63 additions & 1 deletion test/file_walker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -238,6 +239,7 @@ describe('Test article fetching', () => {
realPath: dir,
year: 2019,
month: 5,
draft: false,
day: 5,
date: date,
title: 'Untitled',
Expand Down Expand Up @@ -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'),
Expand All @@ -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();
});
});
});

0 comments on commit 378ed43

Please sign in to comment.