Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bedd6a2953 | |||
| 52d37d56cd | |||
| fc7bc63c46 | |||
| 4397a76d9b | |||
| ddf964eb27 | |||
| 4b47276484 | |||
| a7fedb149f |
@@ -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.
|
||||||
@@ -168,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)
|
||||||
|
|
||||||
@@ -215,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)
|
||||||
|
|||||||
+12
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gitblog.md",
|
"name": "gitblog.md",
|
||||||
"version": "1.2.0",
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-19
@@ -36,7 +36,7 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const articles = {};
|
const articles = {};
|
||||||
let lastRSS = '';
|
let lastRSS = '';
|
||||||
let host;
|
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
|
||||||
@@ -67,17 +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 = {
|
||||||
title: config['home']['title'],
|
title: config['home']['title'],
|
||||||
description: config['home']['description'],
|
description: config['home']['description'],
|
||||||
host: host,
|
host: host,
|
||||||
version: pjson.version
|
version: pjson.version,
|
||||||
|
request: req,
|
||||||
|
config: config
|
||||||
};
|
};
|
||||||
res.render(vPath, data, (err, html) => {
|
res.render(vPath, data, (err, html) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -90,17 +93,17 @@ 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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -133,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))});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -146,15 +149,15 @@ 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
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -162,7 +165,7 @@ module.exports = (config) => {
|
|||||||
}
|
}
|
||||||
res.type(req.headers['user-agent'].match(/Mozilla/) ? 'text/xml' : '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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -205,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});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -231,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();
|
||||||
});
|
});
|
||||||
@@ -240,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
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user