From a7fedb149f7bb206714fafea1da662525b1870d1 Mon Sep 17 00:00:00 2001 From: Klemek Date: Wed, 26 Jun 2019 19:28:00 +0200 Subject: [PATCH 1/3] Host from config if specified --- src/app.js | 18 +++++++++++------- src/config.default.json | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/app.js b/src/app.js index b47aa2b..3d7a118 100644 --- a/src/app.js +++ b/src/app.js @@ -36,7 +36,7 @@ module.exports = (config) => { const articles = {}; let lastRSS = ''; - let host; + let host = config['host']; /** * 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 + * @param req * @param res * @param vPath - path of the view * @param data - data to pass to the view * @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 = { title: config['home']['title'], description: config['home']['description'], host: host, - version: pjson.version + version: pjson.version, + request: req, + config: config }; res.render(vPath, data, (err, html) => { if (err) { @@ -90,17 +93,18 @@ module.exports = (config) => { /** * Show an error with the correct page + * @param req * @param resPath - the page of the original error * @param code - error code * @param res */ - const showError = (resPath, code, res) => { + const showError = (req, resPath, code, res) => { const errorPath = path.join(config['data_dir'], config['home']['error']); fs.access(errorPath, fs.constants.R_OK, (err) => { if (err) res.sendStatus(code); else - render(res, errorPath, {error: code, path: resPath}, code); + render(req, res, errorPath, {error: code, path: resPath}, code); }); }; @@ -135,7 +139,7 @@ module.exports = (config) => { if (err) showError(req.path, 404, res); 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))}); }); }); @@ -219,7 +223,7 @@ module.exports = (config) => { console.log(cons.error, `no template found at ${templatePath}`); showError(req.path, 500, res); } else - render(res, templatePath, {article: article}); + render(req, res, templatePath, {article: article}); }); }); } diff --git a/src/config.default.json b/src/config.default.json index c13b358..4918ed6 100644 --- a/src/config.default.json +++ b/src/config.default.json @@ -1,5 +1,6 @@ { "node_port": 3000, + "host": "", "data_dir": "data", "view_engine": "ejs", "access_log": "access.log", From 4b472764846e04c6783c741cb1aefff7cf8b51f5 Mon Sep 17 00:00:00 2001 From: Klemek Date: Wed, 26 Jun 2019 19:28:18 +0200 Subject: [PATCH 2/3] Updated readme --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 8f70641..0466fc2 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,30 @@ Resources are located on the `data` folder and can be referenced as the root of /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 You need to [create a new repository](https://github.com/new) on your favorite Git service. @@ -207,6 +231,9 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link * `node_port` (default: 3000) 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) the directory where will be located the git repo with templates and articles * `view_engine` (default: ejs) From ddf964eb27492ae6968b032a58873d8a12fc1f06 Mon Sep 17 00:00:00 2001 From: Klemek Date: Wed, 26 Jun 2019 19:28:29 +0200 Subject: [PATCH 3/3] Updated version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b4cf32e..1a1d5e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gitblog.md", - "version": "1.2.0", + "version": "1.2.1", "description": "A static blog using Markdown pulled from your git repository.", "main": "src/server.js", "dependencies": {