Compare commits

...

30 Commits

Author SHA1 Message Date
Klemek bedd6a2953 Merge pull request #13 from Klemek/dev
v1.2.2
2019-06-26 19:52:25 +02:00
Klemek 52d37d56cd Bug fix 2019-06-26 19:48:51 +02:00
Klemek fc7bc63c46 Nodemon config 2019-06-26 19:44:52 +02:00
Klemek 4397a76d9b Merge pull request #12 from Klemek/dev
v1.2.1
2019-06-26 19:35:29 +02:00
Klemek ddf964eb27 Updated version 2019-06-26 19:28:29 +02:00
Klemek 4b47276484 Updated readme 2019-06-26 19:28:18 +02:00
Klemek a7fedb149f Host from config if specified 2019-06-26 19:28:00 +02:00
Klemek ea95a285c9 Merge pull request #11 from Klemek/dev
v1.2.0
2019-06-26 19:00:30 +02:00
Klemek 0fde428806 Updated coverage 2019-06-26 18:56:01 +02:00
Klemek 8fc7ff1ca7 Updated version 2019-06-26 18:44:31 +02:00
Klemek ae4e2eb8d5 Fixing Firefox RSS handling 2019-06-26 18:43:41 +02:00
Klemek 528e4be1fe Updated templates 2019-06-26 18:34:40 +02:00
Klemek bd42883330 Update template.ejs 2019-06-26 18:21:44 +02:00
Klemek b6ac0a73b4 Update style.css 2019-06-26 18:21:26 +02:00
Klemek aebc3da5bc Update template.ejs 2019-06-26 16:43:03 +02:00
Klemek 7a4a4f9006 Update footer.ejs 2019-06-26 15:07:32 +02:00
Klemek 1341aa5a56 Update config.default.json 2019-06-26 11:46:06 +02:00
Klemek 5e05f250f4 Update style.css 2019-06-26 10:06:12 +02:00
Klemek 6cf7be3afb Update README.md 2019-06-23 15:37:23 +02:00
Klemek 6aceacad18 Update README.md 2019-06-23 15:34:46 +02:00
Klemek a3a23be1c2 Update README.md 2019-06-23 15:34:34 +02:00
Klemek e8e8024021 Merge pull request #10 from Klemek/dev
v1.1.5
2019-06-23 15:15:45 +02:00
Klemek 1806d60ca7 Fixed meta tags being wrong 2019-06-23 15:14:47 +02:00
Klemek 2c5f2e589f Merge pull request #9 from Klemek/dev
v1.1.4
2019-06-23 15:09:27 +02:00
Klemek 847d228c0a Updated templates 2019-06-23 15:06:15 +02:00
Klemek 576948acee ViewPort property 2019-06-23 15:05:06 +02:00
Klemek fa6d91db20 updated README.md 2019-06-23 15:04:15 +02:00
Klemek 989bcdf130 Pages metadata by default 2019-06-23 15:02:33 +02:00
Klemek 000104c99d Merge pull request #8 from Klemek/dev
v1.1.3
2019-06-23 14:46:52 +02:00
Klemek f2bd0ec10e Fixed config merge 2019-06-23 14:44:32 +02:00
14 changed files with 174 additions and 42 deletions
+1
View File
@@ -3,6 +3,7 @@
/config.json /config.json
/config.example.json /config.example.json
/data /data
/data/*
/test_data /test_data
/access.log /access.log
/error.log /error.log
+45 -1
View File
@@ -125,6 +125,30 @@ Resources are located on the `data` folder and can be referenced as the root of
/styles/main.css => data/styles/main.css /styles/main.css => data/styles/main.css
``` ```
In your template, the following data is sent :
* `info` (every pages)
* `title` : the blog's title as in the config
* `description` the blog's description as in the config
* `host` : the specified or guessed host with the protocol
* `version` : the GitBlog.md current running version
* `request` : the Express request object
* `config` : the content of the config
* `article` (article pages only)
* `title` : the full title
* `thumbnail` the URL path of the thumbnail
* `url` : the URL path for this article (with the title)
* `date` : a JS date
* `year`
* `month`
* `day`
* `path` : the URL path for the folder of the article (without the title)
* `realPath` : the system's path for the folder
* `escapedTitle` : the code with alphanumeric and underscore characters only
* `error` (error pages only)
* `error` : the error code
* `path` : the resource that caused the error
#### 5. Create and init your git source #### 5. Create and init your git source
You need to [create a new repository](https://github.com/new) on your favorite Git service. You need to [create a new repository](https://github.com/new) on your favorite Git service.
@@ -132,7 +156,10 @@ You need to [create a new repository](https://github.com/new) on your favorite G
```bash ```bash
#gitblog.md/ #gitblog.md/
cd data cd data
git init
git remote add origin <url_of_your_repo.git> git remote add origin <url_of_your_repo.git>
git add .
git commit -m "initial commit"
git push -u origin master git push -u origin master
``` ```
@@ -157,7 +184,7 @@ Here are the steps for Github, if you use another platform adapt it your way (he
```json ```json
"webhook": { "webhook": {
"endpoint": "/webhook", "endpoint": "/webhook",
"secret": "sha1=<value>", "secret": "<value>",
"signature_header": "X-Hub-Signature" "signature_header": "X-Hub-Signature"
}, },
``` ```
@@ -165,6 +192,14 @@ Here are the steps for Github, if you use another platform adapt it your way (he
* Update your webhook on github to include the secret * Update your webhook on github to include the secret
* Check if Github successfully reached the endpoint * Check if Github successfully reached the endpoint
#### 8. Keep your server always up and running (optionnal)
This project `package.json` comes with a [nodemon](https://github.com/remy/nodemon) config.
After installing (`npm i -g nodemon`) you can then run the app with juste the `nodemon` command in the working directory.
With this method, you can do a simple `git pull` to update your server.
## Writing an article ## Writing an article
[back to top](#gitblog-md) [back to top](#gitblog-md)
@@ -212,6 +247,9 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
* `node_port` (default: 3000) * `node_port` (default: 3000)
the port the server is listening to the port the server is listening to
* `host` (default: none)
if set (like `https://mywebsite.com`, it will be used as reference for creating links
by default, host is guessed based on first request
* `data_dir` (default: data) * `data_dir` (default: data)
the directory where will be located the git repo with templates and articles the directory where will be located the git repo with templates and articles
* `view_engine` (default: ejs) * `view_engine` (default: ejs)
@@ -232,6 +270,12 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
* `plantuml` (default: true) * `plantuml` (default: true)
activate PlantUML diagram rendering activate PlantUML diagram rendering
* `home` * `home`
* `title` (default: GitBlog.md)
the title of your blog, **strongly advised to be changed**
given to the template to complete page title and metadata
* `description` (default: A static blog using Markdown pulled from your git repository)
the description of your blog, **strongly advised to be changed**
given to the template to complete page title and metadata
* `index` (default: index.ejs) * `index` (default: index.ejs)
the name of the home page template on the data directory the name of the home page template on the data directory
it will receive `articles`, a list of articles for rendering it will receive `articles`, a list of articles for rendering
+12 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "gitblog.md", "name": "gitblog.md",
"version": "1.1.2", "version": "1.2.2",
"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": {
@@ -46,5 +46,16 @@
"!src/postinstall.js", "!src/postinstall.js",
"!src/lib/*.js" "!src/lib/*.js"
] ]
},
"nodemonConfig": {
"ignore": [
"test/*",
"sample_data/*",
"data/*",
"uml/*",
"*.log",
"README.md"
],
"delay": "2500"
} }
} }
+2 -2
View File
@@ -2,8 +2,8 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Error <%= error %></title> <title><%= info.title %> - Error <%= error %></title>
<link rel="stylesheet" type="text/css" href="/style.css"> <%- include('head'); %>
</head> </head>
<body> <body>
<main> <main>
+2 -2
View File
@@ -1,6 +1,6 @@
<hr> <hr>
<footer> <footer>
<small><a href="/rss">RSS feed</a> - @<%= new Date().getFullYear() %> - Made with <a <small><a href="/rss">RSS feed</a> - <%= new Date().getFullYear() %> - Made with <a
href="https://github.com/klemek/gitblog.md">GitBlog.md</a> (v<%= info.version %>) href="https://github.com/klemek/gitblog.md">GitBlog.md</a> (v<%= info.version %>)
</small> </small>
</footer> </footer>
+25
View File
@@ -0,0 +1,25 @@
<meta name="twitter:card" content="summary_large_image">
<% if(locals.article){ %>
<%- `<meta property="og:title" content="${info.title} - ${article.title}">` %>
<%- `<meta property="twitter:title" content="${info.title} - ${article.title}">` %>
<%- `<meta property="og:description" content="${info.description}">` %>
<%- `<meta property="twitter:description" content="${info.description}">` %>
<%- `<meta property="org:url" content="${info.host + article.url}">` %>
<% if (article.thumbnail) { %>
<%- `<meta property="og:image" content="${info.host}/${article.thumbnail}">` %>
<%- `<meta property="twitter:image" content="${info.host}/${article.thumbnail}">` %>
<% } %>
<link rel="stylesheet" type="text/css" href="/prism.css">
<% } else { %>
<%- `<meta property="og:title" content="${info.title} - Home">` %>
<%- `<meta property="twitter:title" content="${info.title} - Home">` %>
<%- `<meta property="og:description" content="${info.description}">` %>
<%- `<meta property="twitter:description" content="${info.description}">` %>
<%- `<meta property="org:url" content="${info.host}/">` %>
<% } %>
<link rel="alternate" type="application/rss+xml" title="RSS feed" href="/rss"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="/style.css">
+4 -4
View File
@@ -2,13 +2,13 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>GitBlog.md - Home</title> <title><%= info.title %> - Home</title>
<link rel="stylesheet" type="text/css" href="style.css"> <%- include('head'); %>
</head> </head>
<body> <body>
<main> <main>
<h1>GitBlog.md</h1> <h1><%= info.title %></h1>
A static blog using Markdown pulled from your git repository <%= info.description %>
<h2>Articles in this blog :</h2> <h2>Articles in this blog :</h2>
<% articles.forEach((article) => { %> <% articles.forEach((article) => { %>
<div class="article"> <div class="article">
+15 -7
View File
@@ -8,7 +8,7 @@ body, html {
} }
body { body {
font: 14px/1.45 -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font: 15px sans-serif;
color: #111; color: #111;
-webkit-text-size-adjust: none; -webkit-text-size-adjust: none;
background-color: #F5F5F5; background-color: #F5F5F5;
@@ -16,8 +16,8 @@ body {
} }
main { main {
max-width: 75ch; max-width: 42rem;
padding: 2ch; padding: 2rem;
margin: auto; margin: auto;
background-color: #F0F0F0; background-color: #F0F0F0;
min-height: 100vh; min-height: 100vh;
@@ -58,7 +58,7 @@ blockquote {
border-left: 0.5em solid #ccc; border-left: 0.5em solid #ccc;
padding-left: 1em; padding-left: 1em;
margin: 0.25em 0; margin: 0.25em 0;
color: #333; color: #555;
} }
blockquote > p { blockquote > p {
@@ -108,7 +108,7 @@ main.article div.header a.link-home {
line-height: 2.4; line-height: 2.4;
} }
main.article div.header h1, main.article div.header h2, div.article h3 { main.article div.header h1, main.article div.header h2 {
margin-top: 0.85em; margin-top: 0.85em;
margin-bottom: 0.25em; margin-bottom: 0.25em;
font-size: 2em; font-size: 2em;
@@ -123,11 +123,19 @@ main.article div.header span.time, div.article span.time {
} }
div.article { div.article {
margin-left: 1em; margin: 0 1em 1em 1em;
} }
div.article h3 { div.article h3 {
font-size: 1.3em; font-size: 1.3em;
margin:0;
}
div.article img{
max-width: 100%;
height: auto;
margin-right:1em;
margin-top:0.25em;
} }
#text { #text {
@@ -142,4 +150,4 @@ div.article h3 {
#text img { #text img {
max-width: 100%; max-width: 100%;
height: auto; height: auto;
} }
+3 -4
View File
@@ -2,9 +2,8 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>GitBlog.md - <%= article.title %></title> <title><%= info.title %> - <%= article.title %></title>
<link rel="stylesheet" type="text/css" href="/prism.css"> <%- include('head'); %>
<link rel="stylesheet" type="text/css" href="/style.css">
</head> </head>
<body> <body>
<main class="article"> <main class="article">
@@ -19,4 +18,4 @@
<%- include('footer'); %> <%- include('footer'); %>
</main> </main>
</body> </body>
</html> </html>
+34 -19
View File
@@ -36,6 +36,7 @@ module.exports = (config) => {
const articles = {}; const articles = {};
let lastRSS = ''; let lastRSS = '';
let host = config['host'];
/** /**
* Fetch articles from the data folder and send success as a response * Fetch articles from the data folder and send success as a response
@@ -66,14 +67,20 @@ module.exports = (config) => {
/** /**
* Render the page with the view engine and catch errors * Render the page with the view engine and catch errors
* @param req
* @param res * @param res
* @param vPath - path of the view * @param vPath - path of the view
* @param data - data to pass to the view * @param data - data to pass to the view
* @param code - code to send along the page * @param code - code to send along the page
*/ */
const render = (res, vPath, data, code = 200) => { const render = (req, res, vPath, data, code = 200) => {
data.info = { data.info = {
version: pjson.version title: config['home']['title'],
description: config['home']['description'],
host: host,
version: pjson.version,
request: req,
config: config
}; };
res.render(vPath, data, (err, html) => { res.render(vPath, data, (err, html) => {
if (err) { if (err) {
@@ -86,20 +93,28 @@ module.exports = (config) => {
/** /**
* Show an error with the correct page * Show an error with the correct page
* @param resPath - the page of the original error * @param req
* @param code - error code
* @param res * @param res
* @param code - error code
*/ */
const showError = (resPath, code, res) => { const showError = (req, res, code) => {
const errorPath = path.join(config['data_dir'], config['home']['error']); const errorPath = path.join(config['data_dir'], config['home']['error']);
fs.access(errorPath, fs.constants.R_OK, (err) => { fs.access(errorPath, fs.constants.R_OK, (err) => {
if (err) if (err)
res.sendStatus(code); res.sendStatus(code);
else else
render(res, errorPath, {error: code, path: resPath}, code); render(req, res, errorPath, {error: code, path: req.path}, code);
}); });
}; };
app.use((req, res, next) => {
if (!host) {
host = 'http://' + req.headers.host;
console.log(cons.ok, 'Currently hosted on ' + host);
}
next();
});
//log request at result end //log request at result end
app.use((req, res, next) => { app.use((req, res, next) => {
if (config['access_log']) { if (config['access_log']) {
@@ -121,9 +136,9 @@ module.exports = (config) => {
const homePath = path.join(config['data_dir'], config['home']['index']); const homePath = path.join(config['data_dir'], config['home']['index']);
fs.access(homePath, fs.constants.R_OK, (err) => { fs.access(homePath, fs.constants.R_OK, (err) => {
if (err) if (err)
showError(req.path, 404, res); showError(req, res, 404);
else else
render(res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))}); render(req, res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))});
}); });
}); });
@@ -134,23 +149,23 @@ module.exports = (config) => {
const feed = new Rss({ const feed = new Rss({
'title': config['rss']['title'], 'title': config['rss']['title'],
'description': config['rss']['description'], 'description': config['rss']['description'],
'feed_url': 'http://' + req.headers.host + req.url, 'feed_url': host + req.url,
'site_url': 'http://' + req.headers.host 'site_url': host
}); });
Object.values(articles) Object.values(articles)
.slice(0, config['rss']['length']) .slice(0, config['rss']['length'])
.forEach((article) => { .forEach((article) => {
feed.item({ feed.item({
title: article.title, title: article.title,
url: 'http://' + req.headers.host + article.url, url: host + article.url,
date: article.date date: article.date
}); });
}); });
lastRSS = feed.xml(); lastRSS = feed.xml();
} }
res.type('rss').send(lastRSS); res.type(req.headers['user-agent'].match(/Mozilla/) ? 'text/xml' : 'rss').send(lastRSS);
} else { } else {
showError(req.path, 404, res); showError(req, res, 404);
} }
}); });
@@ -193,21 +208,21 @@ module.exports = (config) => {
const articlePath = req.path.substr(1, 10); const articlePath = req.path.substr(1, 10);
const article = articles[articlePath]; const article = articles[articlePath];
if (!article) if (!article)
showError(req.path, 404, res); showError(req, res, 404);
else { else {
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => { renderer.render(path.join(article.realPath, config['article']['index']), (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.path, 500, res); return showError(req, res, 500);
} }
article.content = html; article.content = html;
const templatePath = path.join(config['data_dir'], config['article']['template']); const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => { fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) { if (err) {
console.log(cons.error, `no template found at ${templatePath}`); console.log(cons.error, `no template found at ${templatePath}`);
showError(req.path, 500, res); showError(req, res, 500);
} else } else
render(res, templatePath, {article: article}); render(req, res, templatePath, {article: article});
}); });
}); });
} }
@@ -219,7 +234,7 @@ module.exports = (config) => {
// catch all hidden file type and return 404 // catch all hidden file type and return 404
app.get('*', (req, res, next) => { app.get('*', (req, res, next) => {
if (config['home']['hidden'].includes(path.extname(req.path))) if (config['home']['hidden'].includes(path.extname(req.path)))
showError(req.path, 404, res); showError(req, res, 404);
else else
next(); next();
}); });
@@ -228,7 +243,7 @@ module.exports = (config) => {
app.get('*', express.static(path.join(__dirname, '..', config['data_dir']))); app.get('*', express.static(path.join(__dirname, '..', config['data_dir'])));
// catch express.static errors (mostly not found) by displaying 404 // catch express.static errors (mostly not found) by displaying 404
app.get('*', (req, res) => { app.get('*', (req, res) => {
showError(req.path, 404, res); showError(req, res, 404);
}); });
// catch all other methods and return 400 // catch all other methods and return 400
+5 -2
View File
@@ -1,5 +1,6 @@
{ {
"node_port": 3000, "node_port": 3000,
"host": "",
"data_dir": "data", "data_dir": "data",
"view_engine": "ejs", "view_engine": "ejs",
"access_log": "access.log", "access_log": "access.log",
@@ -12,6 +13,8 @@
"plantuml": true "plantuml": true
}, },
"home": { "home": {
"title": "GitBlog.md",
"description": "A static blog using Markdown pulled from your git repository",
"index": "index.ejs", "index": "index.ejs",
"error": "error.ejs", "error": "error.ejs",
"hidden": [ "hidden": [
@@ -35,7 +38,7 @@
"endpoint": "/webhook", "endpoint": "/webhook",
"secret": "", "secret": "",
"signature_header": "", "signature_header": "",
"pull_command": "git pull" "pull_command": "git pull origin master"
}, },
"showdown": { "showdown": {
"parseImgDimensions": true, "parseImgDimensions": true,
@@ -52,4 +55,4 @@
"plantuml": { "plantuml": {
"output_format": "svg" "output_format": "svg"
} }
} }
+4
View File
@@ -10,6 +10,10 @@ const fs = require('fs');
const merge = (ref, src) => { const merge = (ref, src) => {
if (typeof ref !== typeof src) { if (typeof ref !== typeof src) {
return ref; return ref;
} else if (ref.length && !src.length) {
return ref;
} else if (ref.length && src.length) {
return src;
} else if (typeof ref === 'object') { } else if (typeof ref === 'object') {
const out = {}; const out = {};
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key])); Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
+8
View File
@@ -175,11 +175,19 @@ describe('Test RSS feed', () => {
test('200 empty rss', (done) => { test('200 empty rss', (done) => {
request(app).get('/rsstest').then((response) => { request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/rss+xml');
expect(response.text.length).toBeGreaterThan(0); expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(1); expect(response.text.split('<item>').length).toBe(1);
done(); done();
}); });
}); });
test('200 Mozilla fix', (done) => {
request(app).get('/rsstest').set('user-agent', 'Mozilla Firefox 64.0').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.type).toBe('text/xml');
done();
});
});
test('200 rss cache', (done) => { test('200 rss cache', (done) => {
request(app).get('/rsstest').then(() => { request(app).get('/rsstest').then(() => {
request(app).get('/rsstest').then((response) => { request(app).get('/rsstest').then((response) => {
+14
View File
@@ -64,4 +64,18 @@ test('wrong config fixed', () => {
expect(config).toBeDefined(); expect(config).toBeDefined();
expect(config['node_port']).toBe(3000); expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data2'); expect(config['data_dir']).toBe('data2');
});
test('array parsing', () => {
fs.writeFileSync(configFile, '{"home":{"hidden":["item1","item2"]}}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['home']['hidden']).toEqual(['item1', 'item2']);
});
test('array fix', () => {
fs.writeFileSync(configFile, '{"home":{"hidden":{}}}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['home']['hidden']).toEqual(['.ejs']);
}); });