From e88eb94d78a7e2d65ae673e326468f058beefbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20GOUIN?= Date: Thu, 20 Jun 2019 11:42:01 +0200 Subject: [PATCH] Home minimal EJS template --- sample_data/home/error.ejs | 12 ++++++++++++ sample_data/home/index.ejs | 17 +++++++++++++++-- sample_data/home/style.css | 18 ++++++++++++++++++ src/file_walker.js | 23 +++++++++++++---------- test/file_walker.test.js | 22 ++++++++++++++-------- 5 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 sample_data/home/error.ejs create mode 100644 sample_data/home/style.css diff --git a/sample_data/home/error.ejs b/sample_data/home/error.ejs new file mode 100644 index 0000000..c2471f6 --- /dev/null +++ b/sample_data/home/error.ejs @@ -0,0 +1,12 @@ + + + + + Error <%= error %> + + +
+

Error <%= error %> at path <%= path %>

+
+ + \ No newline at end of file diff --git a/sample_data/home/index.ejs b/sample_data/home/index.ejs index 566549b..25c46d0 100644 --- a/sample_data/home/index.ejs +++ b/sample_data/home/index.ejs @@ -2,9 +2,22 @@ - Title + GitBlog.md - Home + - +
+

GitBlog.md

+ A static blog using Markdown pulled from your git repository +

Articles in this blog :

+ <% articles.forEach((article) => { %> +
+

<%- `${article.title}`%> (<%= `${article.day}/${article.month}/${article.year}`%>)

+ <% if(article.thumbnail){%> + <%- `thumbnail`%> + <% }%> +
+ <% }); %> +
\ No newline at end of file diff --git a/sample_data/home/style.css b/sample_data/home/style.css new file mode 100644 index 0000000..979ab2a --- /dev/null +++ b/sample_data/home/style.css @@ -0,0 +1,18 @@ +main { + max-width: 70ch; + padding: 2ch; + margin: auto; +} + +.article a, .article a:visited{ + color:black; +} + +.article a:visited{ + color:black; +} + +.article img{ + max-width:100%; + max-height:10vh; +} \ No newline at end of file diff --git a/src/file_walker.js b/src/file_walker.js index 5ce2e57..76647ba 100644 --- a/src/file_walker.js +++ b/src/file_walker.js @@ -54,29 +54,32 @@ module.exports = (config) => { if (err) return cb(err); const paths = fileList - .map((path) =>path.substr(config['data_dir'].length)) - .filter((path) =>path.indexOf(config['article']['index']) === path.length - config['article']['index'].length) - .map((path) =>path.substr(0, path.length - config['article']['index'].length)) - .map((path) =>path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/)) - .filter((matches) =>matches && matches.length > 1); + .map((path) => path.substr(config['data_dir'].length)) + .filter((path) => path.indexOf(config['article']['index']) === path.length - config['article']['index'].length) + .map((path) => path.substr(0, path.length - config['article']['index'].length)) + .map((path) => path.match(/^\/(\d{4})\/(\d{2})\/(\d{2})\/$/)) + .filter((matches) => matches && matches.length > 1); if (paths.length === 0) cb(null, []); const list = []; let remaining = 0; - paths.forEach((matches) =>{ + paths.forEach((matches) => { const article = { - path: path.join(config['data_dir'], matches[1], matches[2], matches[3], config['article']['index']), - parent: path.join(config['data_dir'], matches[1], matches[2], matches[3]), + path: path.join(matches[1], matches[2], matches[3]), + realPath: path.join(config['data_dir'], matches[1], matches[2], matches[3]), year: parseInt(matches[1]), month: parseInt(matches[2]), day: parseInt(matches[3]) }; + article.date = new Date(article.year, article.month, article.day); remaining++; - readIndexFile(article.path, config['article']['thumbnail_tag'], (err, info) => { + readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => { if (err) return cb(err); article.title = info.title || config['article']['default_title']; - article.thumbnail = info.thumbnail ? path.join(article.parent, info.thumbnail) : config['article']['default_thumbnail']; + article.thumbnail = info.thumbnail ? path.join(article.path, info.thumbnail) : config['article']['default_thumbnail']; + article.escapedTitle = article.title.toLowerCase().replace(/[^\w]/gm, ' ').trim().replace(/ /gm, '_'); + article.url = path.join(article.path, article.escapedTitle); list.push(article); remaining--; if (remaining === 0) diff --git a/test/file_walker.test.js b/test/file_walker.test.js index b60131a..c36a58e 100644 --- a/test/file_walker.test.js +++ b/test/file_walker.test.js @@ -225,13 +225,16 @@ describe('Test article fetching', () => { expect(list).toBeDefined(); expect(list.length).toBe(1); expect(list[0]).toEqual({ - path: file, - parent:dir, + path: path.join('2019', '05', '05'), + realPath: dir, year: 2019, month: 5, day: 5, + date : new Date(2019,5,5), title: 'Untitled', - thumbnail: 'default.png' + thumbnail: 'default.png', + escapedTitle: 'untitled', + url: path.join('2019', '05', '05', 'untitled'), }); done(); }); @@ -241,7 +244,7 @@ describe('Test article fetching', () => { const file = path.join(dir, testIndex); utils.createEmptyDirs([dir]); fs.writeFileSync(file, ` - # Title + # Title with : info ! ![thumbnail](./thumbnail.jpg) this is some text `); @@ -250,13 +253,16 @@ describe('Test article fetching', () => { expect(list).toBeDefined(); expect(list.length).toBe(1); expect(list[0]).toEqual({ - path: file, - parent:dir, + path: path.join('2019', '05', '05'), + realPath: dir, year: 2019, month: 5, day: 5, - title: 'Title', - thumbnail: path.join(dataDir, '2019', '05', '05', './thumbnail.jpg') + date : new Date(2019,5,5), + title: 'Title with : info !', + thumbnail: path.join('2019', '05', '05', './thumbnail.jpg'), + escapedTitle: 'title_with___info', + url: path.join('2019', '05', '05', 'title_with___info'), }); done(); });