Compare commits

...

46 Commits

Author SHA1 Message Date
Klemek de26feb05c Merge pull request #16 from Klemek/dev
v1.2.5
2019-07-01 23:17:56 +02:00
Klemek 8bb455b576 Fixed draft rendering bug 2019-07-01 23:15:13 +02:00
Klemek 378ed438b6 Merge pull request #15 from Klemek/dev
v1.2.4
2019-07-01 23:03:23 +02:00
Klemek 3b07b6b9c5 Drafted articles 2019-07-01 22:18:40 +02:00
Klemek b6afcd4992 Update README.md 2019-06-26 21:20:45 +02:00
Klemek 35fcdc7320 Merge pull request #14 from Klemek/dev
v1.2.3
2019-06-26 21:03:49 +02:00
Klemek dfb93b6764 [skip CI]Updated nodemon config 2019-06-26 21:03:39 +02:00
Klemek 6af4012522 Hidden files path matching 2019-06-26 20:59:42 +02:00
Klemek 1b91002c03 Updated templates meta tags 2019-06-26 20:09:08 +02:00
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
Klemek 97dab302d8 Merge pull request #7 from Klemek/dev
v1.1.2
2019-06-23 14:19:24 +02:00
Klemek 55e258e093 Fixed Express.static mime type 2019-06-23 14:14:35 +02:00
Klemek 7b22a4773d Merge pull request #6 from Klemek/dev
v1.1.1
2019-06-23 13:58:34 +02:00
Klemek 14cd1436c3 Better without infinite loop 2019-06-23 13:56:31 +02:00
Klemek c112e1ea62 Merge pull request #5 from Klemek/dev
v1.1
2019-06-23 13:52:24 +02:00
Klemek bd8385ea60 [skip CI] updated version 2019-06-23 13:51:07 +02:00
Klemek 6dbc7f359b PlantUML integration 2019-06-23 13:48:28 +02:00
25 changed files with 2242 additions and 126 deletions
+1
View File
@@ -3,6 +3,7 @@
/config.json
/config.example.json
/data
/data/*
/test_data
/access.log
/error.log
+2
View File
@@ -0,0 +1,2 @@
/node_modules
/src/lib
-5
View File
@@ -6,13 +6,8 @@ cache:
npm: true
directories:
- node_modules
addons:
apt:
packages:
- graphviz
install:
- npm install
- npm install node-plantuml
before_script:
- npm install -g jshint
script:
+59 -5
View File
@@ -81,7 +81,7 @@ On the `/rss` endpoint, the servers gives you a RSS feed based on the list of ar
#### 1. Download and install the latest version from the repo
```bash
git clone https://github.com/klemek/gitblog.md.git
npm install
npm install --production
```
#### 2. Create your config file
```bash
@@ -125,6 +125,34 @@ 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 :
<details>
<summary>details (click)</summary>
<p>
* `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) : the error code
</p>
</details>
#### 5. Create and init your git source
You need to [create a new repository](https://github.com/new) on your favorite Git service.
@@ -132,7 +160,10 @@ You need to [create a new repository](https://github.com/new) on your favorite G
```bash
#gitblog.md/
cd data
git init
git remote add origin <url_of_your_repo.git>
git add .
git commit -m "initial commit"
git push -u origin master
```
@@ -157,7 +188,7 @@ Here are the steps for Github, if you use another platform adapt it your way (he
```json
"webhook": {
"endpoint": "/webhook",
"secret": "sha1=<value>",
"secret": "<value>",
"signature_header": "X-Hub-Signature"
},
```
@@ -165,6 +196,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
* 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
[back to top](#gitblog-md)
@@ -203,13 +242,18 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
* **Prism**
It highlight code blocks to be more readable (more info [here](https://prismjs.com/), you will need the corresponding CSS file on your templates)
* **MathJax**
It allows you to add math equations to your articles by simply writing LaTeX between $$ for full size (and between $ for inline) (more info [here](https://www.mathjax.org/))
It allows you to add math equations to your articles by simply writing LaTeX between `$$` for full size (and between $ for inline) (more info [here](https://www.mathjax.org/))
* **PlantUML**
It allows you to add UML diagrams with PlantUML Syntax between `@startuml` and `@enduml` (more info [here](http://www.plantuml.com))
## Configuration
[back to top](#gitblog-md)
* `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)
@@ -227,18 +271,28 @@ Any URL like `/year/month/day/anything/` will redirect to this article (and link
activate Prism code highlighting
* `mathjax` (default: true)
activate MathJax equations formatting
* `plantuml` (default: true)
activate PlantUML diagram rendering
* `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)
the name of the home page template on the data directory
it will receive `articles`, a list of articles for rendering
* `error` (default: error.ejs)
the name of the error page template on the data directory
it will receive `error`, the error code
* `hidden` (default: `[.ejs]`)
file extensions to be returned 404 when reached
* `hidden` (default: `[*.ejs,/.git*]`)
path matches to be returned 404 when reached
* `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)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "gitblog.md",
"version": "1.0.2",
"version": "1.0.3",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
+14 -2
View File
@@ -1,6 +1,6 @@
{
"name": "gitblog.md",
"version": "1.0.3",
"version": "1.2.5",
"description": "A static blog using Markdown pulled from your git repository.",
"main": "src/server.js",
"dependencies": {
@@ -43,7 +43,19 @@
"src/**/*.js",
"!/node_modules/",
"!src/server.js",
"!src/postinstall.js"
"!src/postinstall.js",
"!src/lib/*.js"
]
},
"nodemonConfig": {
"verbose": true,
"ignore": [
"test/*",
"sample_data/*",
"data/*",
"uml/*",
"*.log",
"README.md"
]
}
}
+28
View File
@@ -18,6 +18,7 @@ If you see this page, that means it's working
* [Check Boxes](#checkboxes)
* [Spoilers](#spoilers)
* [Math Equations](#mathequations)
* [UML](#uml)
* [Youtube Videos](#youtubevideos)
### Headers
@@ -224,6 +225,33 @@ $$
Where $\alpha$ is cool
### UML
[Back to top](#top)
You can use PlantUML diagrams with `@startuml` and `@enduml` tags :
@startuml
title Article
cloud web
node nodejs {
TCP -right- [express]
[showdown]
}
package data {
package "2019/06/18" {
component index [
index.md
image.png
...
]
}
}
web -down-> TCP : 1. /2019/06/18/title
express -down-> index : 2. fetch
index -up-> showdown : 3. markdown
showdown -left-> express : 4. html
express -up-> web : 5. html
@enduml
### Youtube Videos
[Back to top](#top)
+2 -3
View File
@@ -1,9 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error <%= error %></title>
<link rel="stylesheet" type="text/css" href="/style.css">
<%- include('head'); %>
<title><%= info.title %> - Error <%= error %></title>
</head>
<body>
<main>
+1 -1
View File
@@ -1,6 +1,6 @@
<hr>
<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 %>)
</small>
</footer>
+30
View File
@@ -0,0 +1,30 @@
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
<meta name="twitter:card" content="summary_large_image">
<%- `<meta property="og:description" content="${info.description}">` %>
<%- `<meta property="twitter:description" content="${info.description}">` %>
<% if(locals.article){ %>
<%- `<meta property="org:url" content="${info.host + article.url}">` %>
<%- `<meta property="og:title" content="${info.title} - ${article.title}">` %>
<%- `<meta property="twitter:title" content="${info.title} - ${article.title}">` %>
<% 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="/css/prism.css">
<% } else { %>
<%- `<meta property="org:url" content="${info.host}/">` %>
<%- `<meta property="og:title" content="${info.title} - Home">` %>
<%- `<meta property="twitter:title" content="${info.title} - Home">` %>
<%- `<meta property="description" content="${info.description}">` %>
<% } %>
<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 -5
View File
@@ -1,14 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GitBlog.md - Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
<%- include('head'); %>
<title><%= info.title %> - Home</title>
</head>
<body>
<main>
<h1>GitBlog.md</h1>
A static blog using Markdown pulled from your git repository
<h1><%= info.title %></h1>
<%= info.description %>
<h2>Articles in this blog :</h2>
<% articles.forEach((article) => { %>
<div class="article">
+14 -6
View File
@@ -8,7 +8,7 @@ body, html {
}
body {
font: 14px/1.45 -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif;
font: 15px sans-serif;
color: #111;
-webkit-text-size-adjust: none;
background-color: #F5F5F5;
@@ -16,8 +16,8 @@ body {
}
main {
max-width: 75ch;
padding: 2ch;
max-width: 42rem;
padding: 2rem;
margin: auto;
background-color: #F0F0F0;
min-height: 100vh;
@@ -58,7 +58,7 @@ blockquote {
border-left: 0.5em solid #ccc;
padding-left: 1em;
margin: 0.25em 0;
color: #333;
color: #555;
}
blockquote > p {
@@ -108,7 +108,7 @@ main.article div.header a.link-home {
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-bottom: 0.25em;
font-size: 2em;
@@ -123,11 +123,19 @@ main.article div.header span.time, div.article span.time {
}
div.article {
margin-left: 1em;
margin: 0 1em 1em 1em;
}
div.article h3 {
font-size: 1.3em;
margin:0;
}
div.article img{
max-width: 100%;
height: auto;
margin-right:1em;
margin-top:0.25em;
}
#text {
+2 -4
View File
@@ -1,10 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GitBlog.md - <%= article.title %></title>
<link rel="stylesheet" type="text/css" href="/prism.css">
<link rel="stylesheet" type="text/css" href="/style.css">
<%- include('head'); %>
<title><%= info.title %> - <%= article.title %></title>
</head>
<body>
<main class="article">
+70 -45
View File
@@ -26,6 +26,28 @@ const cons = {
};
module.exports = (config) => {
/**
* Fetch articles from the data folder and send success as a response
* @param success
* @param error
*/
let reload;
/**
* 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
*/
let render;
/**
* Show an error with the correct page
* @param req
* @param res
* @param code - error code
*/
let showError;
const fw = require('./file_walker')(config);
const renderer = require('./renderer')(config);
@@ -36,13 +58,9 @@ module.exports = (config) => {
const articles = {};
let lastRSS = '';
let host = config['host'];
/**
* Fetch articles from the data folder and send success as a response
* @param success
* @param error
*/
const reload = (success, error) => {
reload = (success, error) => {
fw.fetchArticles((err, dict) => {
if (err) {
console.error(cons.error, 'error loading articles : ' + err);
@@ -51,8 +69,9 @@ module.exports = (config) => {
Object.keys(articles).forEach((key) => delete articles[key]);
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
const nb = Object.keys(articles).length;
const dnb = Object.values(articles).filter(a => a.draft).length;
if (nb > 0)
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''} (${dnb} drafted)`);
else
console.log(cons.warn, `no articles loaded, check your configuration`);
@@ -64,42 +83,45 @@ module.exports = (config) => {
if (config['test'])
app.reload = reload;
/**
* Render the page with the view engine and catch errors
* @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) => {
render = (req, res, vPath, data, code = 200) => {
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) => {
if (err) {
if (err && vPath !== path.join(config['data_dir'], config['home']['error'])) {
console.log(cons.error, `failed to render page ${vPath} : ${err}`);
showError(req, res, 500);
} else if (err) {
res.sendStatus(500);
console.log(cons.error, `failed to render ${vPath} : ${err}`);
console.log(cons.error, `failed to render error page : ${err}`);
} else
res.status(code).send(html);
});
};
/**
* Show an error with the correct page
* @param resPath - the page of the original error
* @param code - error code
* @param res
*/
const showError = (resPath, code, res) => {
showError = (req, res, code) => {
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}, 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
app.use((req, res, next) => {
if (config['access_log']) {
@@ -121,9 +143,13 @@ module.exports = (config) => {
const homePath = path.join(config['data_dir'], config['home']['index']);
fs.access(homePath, fs.constants.R_OK, (err) => {
if (err)
showError(req.path, 404, res);
showError(req, res, 404);
else
render(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))
});
});
});
@@ -134,23 +160,23 @@ module.exports = (config) => {
const feed = new Rss({
'title': config['rss']['title'],
'description': config['rss']['description'],
'feed_url': 'http://' + req.headers.host + req.url,
'site_url': 'http://' + req.headers.host
'feed_url': host + req.url,
'site_url': host
});
Object.values(articles)
.slice(0, config['rss']['length'])
.forEach((article) => {
feed.item({
title: article.title,
url: 'http://' + req.headers.host + article.url,
url: host + article.url,
date: article.date
});
});
lastRSS = feed.xml();
}
res.type('rss').send(lastRSS);
res.type(req.headers['user-agent'].match(/Mozilla/) ? 'text/xml' : 'rss').send(lastRSS);
} else {
showError(req.path, 404, res);
showError(req, res, 404);
}
});
@@ -193,21 +219,21 @@ module.exports = (config) => {
const articlePath = req.path.substr(1, 10);
const article = articles[articlePath];
if (!article)
showError(req.path, 404, res);
showError(req, res, 404);
else {
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
renderer.render(article.realPath, (err, html) => {
if (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;
const templatePath = path.join(config['data_dir'], config['article']['template']);
fs.access(templatePath, fs.constants.R_OK, (err) => {
if (err) {
console.log(cons.error, `no template found at ${templatePath}`);
showError(req.path, 500, res);
showError(req, res, 500);
} else
render(res, templatePath, {article: article});
render(req, res, templatePath, {article: article});
});
});
}
@@ -217,18 +243,17 @@ module.exports = (config) => {
});
// catch all hidden file type and return 404
app.get('*', (req, res, next) => {
if (config['home']['hidden'].includes(path.extname(req.path)))
showError(req.path, 404, res);
else
next();
config['home']['hidden'].forEach(pathMatcher => {
app.get(pathMatcher, (req, res) => {
showError(req, res, 404);
});
});
// serve all static files via get
app.get('*', express.static(config['data_dir']));
app.get('*', express.static(path.join(__dirname, '..', config['data_dir'])));
// catch express.static errors (mostly not found) by displaying 404
app.get('*', (req, res) => {
showError(req.path, 404, res);
showError(req, res, 404);
});
// catch all other methods and return 400
+12 -3
View File
@@ -1,5 +1,6 @@
{
"node_port": 3000,
"host": "",
"data_dir": "data",
"view_engine": "ejs",
"access_log": "access.log",
@@ -8,17 +9,22 @@
"rss": true,
"webhook": true,
"prism": true,
"mathjax": true
"mathjax": true,
"plantuml": true
},
"home": {
"title": "GitBlog.md",
"description": "A static blog using Markdown pulled from your git repository",
"index": "index.ejs",
"error": "error.ejs",
"hidden": [
".ejs"
"*.ejs",
"/.git*"
]
},
"article": {
"index": "index.md",
"draft": "draft.md",
"template": "template.ejs",
"thumbnail_tag": "thumbnail",
"default_title": "Untitled",
@@ -34,7 +40,7 @@
"endpoint": "/webhook",
"secret": "",
"signature_header": "",
"pull_command": "git pull"
"pull_command": "git pull origin master"
},
"showdown": {
"parseImgDimensions": true,
@@ -47,5 +53,8 @@
"mathjax": {
"output_format": "svg",
"speak_text": true
},
"plantuml": {
"output_format": "svg"
}
}
+4
View File
@@ -10,6 +10,10 @@ const fs = require('fs');
const merge = (ref, src) => {
if (typeof ref !== typeof src) {
return ref;
} else if (ref.length && !src.length) {
return ref;
} else if (ref.length && src.length) {
return src;
} else if (typeof ref === 'object') {
const out = {};
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
+8 -6
View File
@@ -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
@@ -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, {});
@@ -81,7 +81,8 @@ module.exports = (config) => {
paths.forEach((p) => {
const article = {
path: joinUrl(p[0], p[1], p[2]),
realPath: path.join(config['data_dir'], p[0], p[1], p[2]),
draft: p[3] === config['article']['draft'],
realPath: path.join(config['data_dir'], p[0], p[1], p[2], p[3]),
year: parseInt(p[0]),
month: parseInt(p[1]),
day: parseInt(p[2])
@@ -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(article.realPath, 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);
File diff suppressed because it is too large Load Diff
+29 -10
View File
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
const showdown = require('showdown');
module.exports = (config) => {
@@ -21,12 +22,27 @@ module.exports = (config) => {
while ((match = codeRegex.exec(data))) {
const lang = match[1].trim();
const code = match[2].trim();
try {
const block = Prism.highlight(code, Prism.languages[lang] || Prism.languages.autoit, lang);
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
} catch (err) {
console.error(err);
}
const block = Prism.highlight(code, Prism.languages[lang] || Prism.languages.autoit, lang);
data = data.slice(0, match.index) + `<pre><code class="${lang} language-${lang}">` + block + '</code></pre>' + data.slice(match.index + match[0].length);
}
cb(data);
};
if (config['modules']['plantuml']) {
require('./script_loader')(path.join(__dirname, 'lib', 'plantuml_synchro.js'));
}
const renderPlantUML = (data, cb) => {
if (!config['modules']['plantuml'])
return cb(data);
const umlRegex = /@startuml\r?\n((?:(?!@enduml)[\s\S])*)\r?\n@enduml/m;
let match;
while ((match = umlRegex.exec(data))) {
const code = match[1].trim();
const s = unescape(encodeURIComponent(code)); // jshint ignore:line
const compressed = global['zip_deflate'](s);
const url = `http://www.plantuml.com/plantuml/${config['plantuml']['output_format']}/${encode64(compressed)}`;// jshint ignore:line
data = data.slice(0, match.index) + `<img alt="generated PlantUML diagram" src="${url}">` + data.slice(match.index + match[0].length);
}
cb(data);
};
@@ -81,16 +97,19 @@ module.exports = (config) => {
return {
renderShowDown: config['test'] ? renderShowDown : undefined,
renderPrism: config['test'] ? renderPrism : undefined,
renderPlantUML: config['test'] ? renderPlantUML : undefined,
renderMathJax: config['test'] ? renderMathJax : undefined,
render: (file, cb) => {
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
renderPrism(data, (data2) => {
renderMathJax(data2, (data3) => {
renderShowDown(data3, (html) => {
cb(null, html);
renderPrism(data, (data) => {
renderPlantUML(data, (data) => {
renderMathJax(data, (data) => {
renderShowDown(data, (html) => {
cb(null, html);
});
});
});
});
+10
View File
@@ -0,0 +1,10 @@
const fs = require('fs');
/**
* Import client-side script into the "global" var
* @param scriptPath
*/
module.exports = (scriptPath) => {
eval.call(global, fs.readFileSync(scriptPath, {encoding: 'UTF-8'}));
};
+71 -22
View File
@@ -16,16 +16,15 @@ config['data_dir'] = dataDir;
config['webhook']['endpoint'] = '/webhooktest';
config['rss']['endpoint'] = '/rsstest';
config['rss']['length'] = 2;
config['home']['index'] = testIndex;
config['home']['error'] = testError;
config['article']['template'] = testTemplate;
const app = require('../src/app')(config);
beforeEach((done, fail) => {
config['home']['index'] = testIndex;
config['data_dir'] = dataDir;
config['article']['index'] = 'index.md';
config['home']['hidden'] = ['.ejs', '.test'];
config['access_log'] = '';
config['error_log'] = '';
config['modules']['rss'] = true;
@@ -93,20 +92,20 @@ describe('Test request logging', () => {
describe('Test error logging', () => {
test('test no log', (done) => {
config['home']['hidden'] = null;
request(app).get('/somefile.txt').then(() => {
config['home']['index'] = null;
request(app).get('/').then(() => {
expect(fs.existsSync(path.join(dataDir, 'error.log'))).toBe(false);
done();
});
});
test('test null error ', (done) => {
config['home']['hidden'] = null;
config['home']['index'] = null;
config['error_log'] = path.join(dataDir, 'error.log');
request(app).get('/somefile.txt').then(() => {
request(app).get('/').then(() => {
fs.readFile(path.join(dataDir, 'error.log'), {encoding: 'UTF-8'}, (err, data) => {
expect(err).toBeNull();
const start = data.split('\n').slice(0, 2).join('\n');
const expected = '500 GET /somefile.txt ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError: Cannot read property \'includes\' of null';
const expected = '500 GET / ' + new Date().toUTCString() + ' ::ffff:127.0.0.1\nTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object';
expect(start).toBe(expected);
done();
});
@@ -122,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();
});
});
@@ -136,6 +135,23 @@ describe('Test root path', () => {
done();
});
});
test('500 render error with page', (done) => {
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %>');
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(500);
expect(response.text).toBe('error 500');
done();
});
});
test('500 render error with failing page', (done) => {
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= null.length %>');
fs.writeFileSync(path.join(dataDir, testError), 'error <%= null.error %>');
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(500);
done();
});
});
test('200 no articles', (done) => {
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
request(app).get('/').then((response) => {
@@ -144,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(() => {
@@ -175,11 +194,19 @@ describe('Test RSS feed', () => {
test('200 empty rss', (done) => {
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/rss+xml');
expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(1);
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) => {
request(app).get('/rsstest').then(() => {
request(app).get('/rsstest').then((response) => {
@@ -295,12 +322,11 @@ describe('Test articles rendering', () => {
});
});
test('500 no index', (done, fail) => {
test('500 fail to render', (done, fail) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- articl.content %><%- `<a href="${article.url}">reload</a>` %>');
app.reload(() => {
config['article']['index'] = 'invalid.md';
request(app).get('/2019/05/05/hello/').then((response) => {
expect(response.statusCode).toBe(500);
done();
@@ -332,6 +358,19 @@ describe('Test articles rendering', () => {
}, fail);
});
test('200 rendered draft', (done, fail) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'draft.md'), '# Hello');
fs.writeFileSync(path.join(dataDir, testTemplate), '<%- article.content %><%- `<a href="${article.url}">reload</a>` %>');
app.reload(() => {
request(app).get('/2019/05/05/hello/').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('<h1 id="hello">Hello</h1><a href="/2019/05/05/hello/">reload</a>');
done();
});
}, fail);
});
test('200 other url', (done, fail) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
@@ -370,24 +409,34 @@ 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();
});
});
test('404 hidden file', (done) => {
fs.writeFileSync(path.join(dataDir, 'somefile.test'), '');
request(app).get('/somefile.test').then((response) => {
utils.createEmptyDirs([path.join(dataDir, 'tmp')]);
fs.writeFileSync(path.join(dataDir, 'tmp', 'somefile.ejs'), '');
request(app).get('/tmp/somefile.ejs').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('404 hidden folder', (done) => {
utils.createEmptyDirs([path.join(dataDir, '.git')]);
fs.writeFileSync(path.join(dataDir, '.git', 'file.txt'), '');
request(app).get('/.git/file.txt').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('200 valid file', (done) => {
fs.writeFileSync(path.join(dataDir, 'somefile.txt'), 'filecontent');
request(app).get('/somefile.txt').then((response) => {
fs.writeFileSync(path.join(dataDir, 'somefile.css'), 'filecontent');
request(app).get('/somefile.css').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.type).toBe('text/css');
expect(response.text).toBe('filecontent');
done();
});
+14
View File
@@ -65,3 +65,17 @@ test('wrong config fixed', () => {
expect(config['node_port']).toBe(3000);
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', '/.git*']);
});
+65 -3
View File
@@ -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'
@@ -235,9 +236,10 @@ describe('Test article fetching', () => {
expect(Object.keys(dict).length).toBe(1);
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
path: joinUrl('2019', '05', '05'),
realPath: dir,
realPath: file,
year: 2019,
month: 5,
draft: false,
day: 5,
date: date,
title: 'Untitled',
@@ -265,10 +267,11 @@ describe('Test article fetching', () => {
expect(Object.keys(dict).length).toBe(1);
expect(dict[joinUrl('2019', '05', '05')]).toEqual({
path: joinUrl('2019', '05', '05'),
realPath: dir,
realPath: file,
year: 2019,
month: 5,
day: 5,
draft: false,
date: date,
title: 'Title with : info !',
thumbnail: joinUrl('2019', '05', '05', './thumbnail.jpg'),
@@ -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: file,
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: file,
year: 2019,
month: 5,
draft: false,
day: 5,
date: date,
title: 'Untitled',
thumbnail: 'default.png',
escapedTitle: 'untitled',
url: '/' + joinUrl('2019', '05', '05', 'untitled') + '/',
});
done();
});
});
});
+31 -1
View File
@@ -10,7 +10,8 @@ const config = {
'test': true,
'modules': {
'prism': true,
'mathjax': true
'mathjax': true,
'plantuml': true
},
'showdown': {
'simplifiedAutoLink': true,
@@ -19,6 +20,9 @@ const config = {
'mathjax': {
'output_format': 'html',
'speak_text': false
},
'plantuml': {
'output_format': 'svg'
}
};
@@ -27,6 +31,7 @@ const renderer = require('../src/renderer')(config);
beforeEach(() => {
config['modules']['prism'] = true;
config['modules']['mathjax'] = true;
config['modules']['plantuml'] = true;
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
@@ -91,6 +96,31 @@ describe('Test Prism', () => {
});
});
describe('Test PlantUML', () => {
test('no plantuml', (done) => {
config['modules']['plantuml'] = false;
renderer.renderPlantUML('@startuml\nBob -> Alice : hello\n@enduml', (data) => {
expect(data).toBe('@startuml\nBob -> Alice : hello\n@enduml');
done();
});
});
test('plantuml correct', (done) => {
renderer.renderPlantUML('@startuml\nBob -> Alice : hello\n@enduml', (data) => {
expect(data).toBe('<img alt="generated PlantUML diagram" src="http://www.plantuml.com/plantuml/svg/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000">');
done();
});
});
test('plantuml multiple uml', (done) => {
renderer.renderPlantUML('@startuml\nBob -> Alice : hello\n@enduml\n@startuml\nBob -> Alice : hello\n@enduml', (data) => {
expect(data).toBe('<img alt="generated PlantUML diagram" src="http://www.plantuml.com/plantuml/svg/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000">\n<img alt="generated PlantUML diagram" src="http://www.plantuml.com/plantuml/svg/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000">');
done();
});
});
});
describe('Test MathJax', () => {
test('no mathjax', (done) => {
config['modules']['mathjax'] = false;
+51
View File
@@ -0,0 +1,51 @@
/* jshint -W117 */
const fs = require('fs');
const path = require('path');
const utils = require('./test_utils');
const dataDir = 'test_data';
beforeEach(() => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
test('load 1 script', () => {
const file = path.join(dataDir, 'test.js');
fs.writeFileSync(file, `
var a = 5;
function b(){
return a;
}`);
require('../src/script_loader')(file);
expect(global['b']).toBeDefined();
expect(global['b']()).toBe(5);
});
test('load 2 script', () => {
const file1 = path.join(dataDir, 'test.js');
fs.writeFileSync(file1, `
var a = 5;
function b(){
return a;
}`);
const file2 = path.join(dataDir, 'test2.js');
fs.writeFileSync(file2, `
var a = 9;
function b(){
return a;
}`);
require('../src/script_loader')(file1);
expect(global['b']).toBeDefined();
expect(global['b']()).toBe(5);
require('../src/script_loader.js')(file2);
expect(global['b']).toBeDefined();
expect(global['b']()).toBe(9);
});