Merge pull request #1 from Klemek/dev

v1.0
This commit is contained in:
Klemek
2019-06-21 16:31:29 +02:00
committed by GitHub
28 changed files with 2363 additions and 259 deletions
+4 -1
View File
@@ -1,4 +1,7 @@
/.idea
/node_modules
/config.json
/data
/config.example.json
/data
/test_data
/git_secret
+1 -1
View File
@@ -16,5 +16,5 @@ install:
before_script:
- npm install -g jshint
script:
- npm test
- jest --silent --coverage --coverageReporters=text-lcov | coveralls
- jshint ./src
+260 -2
View File
@@ -1,6 +1,264 @@
# GitBlog.md (WIP)
> This is a work in progress, development are in the branch 'dev'
# GitBlog.md
[![Build Status](https://img.shields.io/travis/Klemek/GitBlog.md.svg?branch=master)](https://travis-ci.org/Klemek/GitBlog.md)
[![Coverage Status](https://img.shields.io/coveralls/github/Klemek/GitBlog.md.svg?branch=master)](https://coveralls.io/github/Klemek/GitBlog.md?branch=master)
A static blog using Markdown pulled from your git repository.
* **[How it works](#how-it-works)**
* **[Installation](#installation)**
* **[Writing an article](#writing-an-article)**
* **[Configuration](#configuration)**
## How it works
[back to top](#gitblog-md)
There are 4 majors features of this project :
#### 1. Home page
<details>
<summary>diagram (click)</summary>
<p>
![root](./uml/root.png)
</p>
</details>
When you access the root url of your blog, the app will fetch the template and inject the list of currently available articles.
#### 2. Article page
<details>
<summary>diagram (click)</summary>
<p>
![article](./uml/article.png)
</p>
</details>
As you access an article link, the server will fetch it's `index.md` Markdown file and render it in plain HTML using Showdown.
#### 3. Git webhook
<details>
<summary>diagram (click)</summary>
<p>
![webhook](./uml/webhook.png)
</p>
</details>
As you configured your data repository, when you push any data, it will trigger the webhook that will perform a `git pull` then refresh the data you just committed.
#### 4. RSS feed
<details>
<summary>diagram (click)</summary>
<p>
![rss](./uml/rss.png)
</p>
</details>
On the `/rss` endpoint, the servers gives you a RSS feed based on the list of articles which you can bookmark.
## Installation
[back to top](#gitblog-md)
#### 1. Download and install the latest version from the repo
```bash
git clone https://github.com/klemek/gitblog.md.git
npm install
```
#### 2. Create your config file
```bash
cd gitblog.md
cp config.example.json config.json
```
then edit the config.json file with your custom values.
For example, you might want to change the app's port with :
```json
{
"node_port": 3030
}
```
See [Configuration](#configuration) for more info.
#### 3. Start your server
```bash
npm run
#or
node src/server.js
```
You can check that it's up and running at [http://localhost:3000/](http://localhost:3000/)
You might want to use something like screen to separate the process from your current terminal session.
#### 4. Customize the blog's style
At `npm install` a first article will be created for the current date.
You can see it as an example of rendering of your blog.
Use it to edit your templates and styles located on the `data` folder.
At first, home page and articles are rendered using EJS engine but you can customize that into the configuration.
Resources are located on the `data` folder and can be referenced as the root of your blog.
```
/styles/main.css => data/styles/main.css
```
#### 5. Create and init your git source
You need to [create a new repository](https://github.com/new) on your favorite Git service.
```bash
#gitblog.md/
cd data
git remote add origin <url_of_your_repo.git>
git push -u origin master
```
Now you just have to edit a local copy of your articles and, when you push them, to perform a simple `git pull` on that data folder.
#### 6. Refresh content with a webhook (optional)
Create a webhook on your git source (On GitHub, in the `Settings/Webhooks` part of the repository.) with the following parameters :
* Payload URL : `https://<url_of_your_server>/webhook`
* Content type : `application/json`
* Events : Just the push event
Now the server will perform the `git pull` task for you after a successful push on GitHub.
#### 7. Securize your webhook (optional)
Here are the steps for Github, if you use another platform adapt it your way (header format on the config) :
* Create a password or random secret
* Edit your configuration to add webhook info
```json
"webhook": {
"endpoint": "/webhook",
"secret": "sha1=<value>",
"signature_header": "X-Hub-Signature"
},
```
* Launch the server
* Update your webhook on github to include the secret
* Check if Github successfully reached the endpoint
## Writing an article
[back to top](#gitblog-md)
You need to write your article (and templates) on the git repository but **keep the data directory on the server untouched** to prevent any changes to harm the git pull normal behavior.
To be referenced, an article need to be on a specific path containing its date and have a Markdown index file :
```
data/year/month/day/index.md
```
> note that month and day need to be 0 padded (`5th of june 2019 => 2019/06/05`)
On your Markdown file you can write anything but some informations will be fetched automatically :
* Title : first level 1 header (#)
* Thumbnail : first thumbnail tagged image (like `![thumbnail](url)`)
On that same folder, you can place resources like images and reference them in relative paths :
```
![](./image.png) => data/year/month/day/image.png
```
> note that you cannot place resources on subfolders
Any URL like `/year/month/day/anything/` will redirect to this article (and link to correct resources)
## Configuration
[back to top](#gitblog-md)
* `node_port` (default: 3000)
the port the server is listening to
* `data_dir` (default: data)
the directory where will be located the git repo with templates and articles
* `view_engine` (default: ejs)
the Express view engine used to render pages from templates
* `modules`
* `rss` (default: true)
activate the RSS endpoint and its features
* `webhook` (default: true)
activate the webhook endpoint and its features
* `prism` (default: true)
activate Prism code highlighting
* `home`
* `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
* `article`
* `index` (default: index.md)
the name of the Markdown page of the article on the `/year/month/day/` directory
* `template` (default: template.ejs)
the name of the article page template on the data directory
* `thumbnail_tag`: (default: thumbnail)
the alt text searched to get the thumbnail image on the article
as in `![<thumbnail_tag>](<url of the image>)`
* `default_title`: (default: Untitled)
the title of the article in case a level 1 title was not found
* `default_thumbnail`: (default: none)
the path of the default thumbnail to get from the data directory
* `rss`
* `title`: (default: mygitblog RSS feed)
* `description`: (default: a generated RSS feed from my articles)
* `endpoint`: (default: /rss)
* `length`: (default: 10)
how many last articles will be present in the feed
* `webhook`
* `endpoint`: (default: /webhook)
* `secret`: (default: none)
see [above](#7-securize-your-webhook-optional-)
* `signature_header`: (default: none)
see [above](#7-securize-your-webhook-optional-)
* `pull_command`: (default: git pull)
the command used by the server on webhook trigger
* `showdown`
Options to be applied to Showdown renderer (see [showdown options](https://github.com/showdownjs/showdown#valid-options) for more info)
-23
View File
@@ -1,23 +0,0 @@
{
"nodePort": 3000,
"dataDir": "data",
"modules" : {
"plantuml" : false,
"rss": true,
"webhook": true
},
"home" : {
"index" : "index.ejs"
},
"article" : {
"index" : "index.md"
},
"rss" : {
"endpoint" : "/rss",
"length" : 10
},
"webhook" : {
"endpoint": "/webhook",
"secretFile": "git_secret"
}
}
+311 -187
View File
File diff suppressed because it is too large Load Diff
+11 -3
View File
@@ -1,23 +1,31 @@
{
"nodePort": 5000,
"name": "gitblog.md",
"version": "1.0.0",
"description": "A static blog using Markdown pulled from your git repository.",
"main": "src/server.js",
"dependencies": {
"body-parser": "^1.19.0",
"crypto": "^1.0.1",
"ejs": "^2.6.2",
"express": "^4.17.1",
"ncp": "^2.0.0",
"showdown": "^1.9.0",
"xml": "^1.0.1"
"node-prismjs": "^0.1.2",
"prismjs": "^1.16.0",
"rss": "^1.2.2",
"showdown": "^1.9.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"coveralls": "^3.0.4",
"jest": "^24.8.0",
"superagent": "^5.1.0",
"supertest": "^4.0.2"
},
"scripts": {
"test": "jest",
"start": "node src/server.js",
"test": "jest --silent",
"install": "node src/postinstall.js"
},
"repository": {
+171
View File
@@ -0,0 +1,171 @@
# Welcome to your new blog
If you see this page, that means it's working
## Guide to Markdown formatting
### Headers
# H1
## H2
### H3
#### H4
##### H5
###### H6
Alternatively, for H1 and H2, an underline-ish style:
Alt-H1
======
Alt-H2
------
### Emphasis
Emphasis, aka italics, with *asterisks* or _underscores_.
Strong emphasis, aka bold, with **asterisks** or __underscores__.
Combined emphasis with **asterisks and _underscores_**.
Strikethrough uses two tildes. ~~Scratch this.~~
### Lists
1. First ordered list item
2. Another item
* Unordered sub-list.
1. Actual numbers don't matter, just that it's a number
1. Ordered sub-list
4. And another item.
You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
To have a line break without a paragraph, you will need to use two trailing spaces.⋅⋅
Note that this line is separate, but within the same paragraph.⋅⋅
(This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)
* Unordered list can use asterisks
- Or minuses
+ Or pluses
### Links
[I'm an inline-style link](https://www.google.com)
[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
[I'm a reference-style link][Arbitrary case-insensitive reference text]
[I'm a relative reference to a repository file](../blob/master/LICENSE)
[You can use numbers for reference-style link definitions][1]
Or leave it empty and use the [link text itself].
URLs and URLs in angle brackets will automatically get turned into links.
http://www.example.com or <http://www.example.com> and sometimes
example.com (but not on Github, for example).
Some text to show that the reference links can follow later.
[arbitrary case-insensitive reference text]: https://www.mozilla.org
[1]: http://slashdot.org
[link text itself]: http://www.reddit.com
### Images
Here's our logo (hover to see the title text):
Inline-style:
![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")
Reference-style:
![alt text][logo]
[logo]: https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 2"
### Code and Syntax Highlighting
Inline `code` has `back-ticks around` it.
```javascript
var s = "JavaScript syntax highlighting";
alert(s);
```
```python
s = "Python syntax highlighting"
print s
```
```
No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>.
```
### Tables
Colons can be used to align columns.
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
There must be at least 3 dashes separating each header cell.
The outer pipes (|) are optional, and you don't need to make the
raw Markdown line up prettily. You can also use inline Markdown.
Markdown | Less | Pretty
--- | --- | ---
*Still* | `renders` | **nicely**
1 | 2 | 3
### Blockquotes
> Blockquotes are very handy in email to emulate reply text.
> This line is part of the same quote.
Quote break.
> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
### Inline HTML
<dl>
<dt>Definition list</dt>
<dd>Is something people use sometimes.</dd>
<dt>Markdown in HTML</dt>
<dd>Does *not* work **very** well. Use HTML <em>tags</em>.</dd>
</dl>
### Horizontal Rule
Three or more...
---
Hyphens
***
Asterisks
___
Underscores
### Line Breaks
Here's a line for us to start with.
This line is separated from the one above by two newlines, so it will be a *separate paragraph*.
This line is also a separate paragraph, but...
This line is only separated by a single newline, so it's a separate line in the *same paragraph*.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 898 KiB

+19
View File
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error <%= error %></title>
<link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
<main>
<h1>Somehing went wrong
<small>(Error <%= error %>)</small>
</h1>
It means the resource you're trying to access is unavailable right now.<br>
We're terribly sorry that you encountered this error.<br><br>
<a href="/">Back to home</a>
<%- include('footer'); %>
</main>
</body>
</html>
+5
View File
@@ -0,0 +1,5 @@
<hr>
<footer>
<small>@<%= new Date().getFullYear() %> - Made with <a href="https://github.com/klemek/gitblog.md">GitBlog.md</a>
</small>
</footer>
+25
View File
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GitBlog.md - Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<main>
<h1>GitBlog.md</h1>
A static blog using Markdown pulled from your git repository
<h2>Articles in this blog :</h2>
<% articles.forEach((article) => { %>
<div class="article">
<h3><%- `<a href="${article.url}">${article.title}</a>` %></h3>
<span class="time"><span>Published on</span> <%= article.year + '-' + article.month + '-' + article.day %></span>
<% if(article.thumbnail){ %>
<%- `<img alt="thumbnail" src=${article.thumbnail}>` %>
<% } %>
</div>
<% }); %>
<%- include('footer'); %>
</main>
</body>
</html>
+143
View File
@@ -0,0 +1,143 @@
/* PrismJS 1.16.0
https://prismjs.com/download.html#themes=prism&languages=markup+css+clike+javascript+abap+abnf+actionscript+ada+apacheconf+apl+applescript+c+arff+asciidoc+asm6502+csharp+autohotkey+autoit+bash+basic+batch+bison+bnf+brainfuck+bro+cpp+aspnet+arduino+cil+coffeescript+cmake+clojure+ruby+csp+css-extras+d+dart+diff+markup-templating+docker+ebnf+eiffel+ejs+elixir+elm+erb+erlang+fsharp+flow+fortran+gcode+gedcom+gherkin+git+glsl+gml+go+graphql+groovy+less+handlebars+haskell+haxe+hcl+http+hpkp+hsts+ichigojam+icon+inform7+ini+io+j+java+scala+php+javastacktrace+jolie+jq+javadoclike+n4js+json+jsonp+json5+julia+keyman+kotlin+latex+markdown+liquid+lisp+livescript+lolcode+lua+makefile+crystal+django+matlab+mel+mizar+monkey+n1ql+typescript+nand2tetris-hdl+nasm+nginx+nim+nix+nsis+objectivec+ocaml+opencl+oz+parigp+parser+pascal+perl+jsdoc+phpdoc+php-extras+sql+powershell+processing+prolog+properties+protobuf+scss+puppet+pure+python+q+qore+r+js-extras+jsx+renpy+reason+vala+rest+rip+roboconf+textile+rust+sas+sass+stylus+javadoc+scheme+shell-session+smalltalk+smarty+plsql+soy+twig+swift+yaml+tcl+haml+toml+tt2+pug+tsx+t4-templating+visual-basic+t4-cs+regex+vbnet+velocity+verilog+vhdl+vim+t4-vb+wasm+wiki+xeora+xojo+xquery+tap */
/**
* prism.js default theme for JavaScript, CSS and HTML
* Based on dabblet (http://dabblet.com)
* @author Lea Verou
*/
code[class*="language-"],
pre[class*="language-"] {
color: black;
background: none;
text-shadow: 0 1px white;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
font-size: 1em;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.5;
-moz-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;
-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}
pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
text-shadow: none;
background: #b3d4fc;
}
pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
code[class*="language-"]::selection, code[class*="language-"] ::selection {
text-shadow: none;
background: #b3d4fc;
}
@media print {
code[class*="language-"],
pre[class*="language-"] {
text-shadow: none;
}
}
/* Code blocks */
pre[class*="language-"] {
padding: 1em;
margin: .5em 0;
overflow: auto;
}
:not(pre) > code[class*="language-"],
pre[class*="language-"] {
background: #f5f2f0;
}
/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
}
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: slategray;
}
.token.punctuation {
color: #999;
}
.namespace {
opacity: .7;
}
.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol,
.token.deleted {
color: #905;
}
.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
color: #690;
}
.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
color: #9a6e3a;
background: hsla(0, 0%, 100%, .5);
}
.token.atrule,
.token.attr-value,
.token.keyword {
color: #07a;
}
.token.function,
.token.class-name {
color: #DD4A68;
}
.token.regex,
.token.important,
.token.variable {
color: #e90;
}
.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}
.token.entity {
cursor: help;
}
+145
View File
@@ -0,0 +1,145 @@
body, html {
padding: 0;
margin: 0;
}
* {
box-sizing: border-box;
}
body {
font: 14px/1.45 -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif;
color: #111;
-webkit-text-size-adjust: none;
background-color: #F5F5F5;
height: 100vh;
}
main {
max-width: 75ch;
padding: 2ch;
margin: auto;
background-color: #F0F0F0;
min-height: 100vh;
}
/* hide redundant text in article */
#text h1:first-child {
display: none;
}
hr {
background: #e1e4e8;
border: 0;
height: 0.25em;
margin: 1em 0;
}
a {
color: #3C3CA1;
}
a:hover {
color: #8484C6;
}
pre, code {
font-size: 96%;
background: #f8f8f8;
}
pre {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
padding: 10px 16px;
}
blockquote {
border-left: 0.5em solid #ccc;
padding-left: 1em;
margin: 0.25em 0;
color: #333;
}
blockquote > p {
margin: 0.6rem 0;
}
table td {
vertical-align: baseline;
padding-left: 8px;
}
table td:first-of-type {
padding-left: 0;
}
#text table td, #text table th {
border: 1px solid #ccc;
padding: 0.25em 0.5em;
}
details {
background: #f8f8f8;
margin: 0.25em 0;
padding: 0;
}
details > summary {
cursor: pointer;
padding: 0.5em 1em;
}
details > p {
background: #f5f5f5;
padding: 0.5em 0.5em 0.5em 2em;
margin: 0;
}
main.article div.header span.time span, div.article span.time span {
color: #888;
font-family: serif;
font-style: italic;
}
main.article div.header a.link-home {
text-decoration: none;
float: right;
line-height: 2.4;
}
main.article div.header h1, main.article div.header h2, div.article h3 {
margin-top: 0.85em;
margin-bottom: 0.25em;
font-size: 2em;
}
main.article div.header h1 a, main.article div.header h2 a, div.article h3 a {
text-decoration: none;
}
main.article div.header span.time, div.article span.time {
display: block;
}
div.article {
margin-left: 1em;
}
div.article h3 {
font-size: 1.3em;
}
#text {
text-align: justify;
hyphens: auto;
}
#text li, #text table, #text blockquote {
text-align: left;
}
#text img {
max-width: 100%;
height: auto;
}
+22
View File
@@ -0,0 +1,22 @@
<!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">
</head>
<body>
<main class="article">
<div class="header">
<a class="link-home" href="/">↑</a>
<h1><%= article.title %></h1>
<span class="time"><span>Published on</span> <%= article.year + '-' + article.month + '-' + article.day %></span>
</div>
<div id="text"><%- article.content %></div>
<br>
<a href="#top">Go to top</a> - <a href="/">Back to home</a>
<%- include('footer'); %>
</main>
</body>
</html>
-6
View File
@@ -1,6 +0,0 @@
# Welcome to your new blog
## If you see this page, that means it's working
![missing image](./birthday-cake.png)
+230 -4
View File
@@ -1,12 +1,238 @@
const express = require('express');
const app = express();
const fs = require('fs');
const path = require('path');
module.exports = function(/*config*/){
app.get('/', (req,res) => {
res.status(200).send('Hello World!');
//rss
const Rss = require('rss');
///webhook
const bodyParser = require('body-parser');
const crypto = require('crypto');
const cp = require('child_process');
app.use(bodyParser.json());
/**
* Terminal colors and symbols to display status messages
* @type {{warn: string, ok: string, error: string}}
*/
const cons = {
ok: '\x1b[32m✔\x1b[0m %s',
warn: '\x1b[33m⚠\x1b[0m %s',
error: '\x1b[31m✘\x1b[0m %s',
};
module.exports = (config) => {
const fw = require('./file_walker')(config);
const renderer = require('./renderer')(config);
// set view engine from configuration
app.set('view engine', config['view_engine']);
// reroute the views folder to the root folder
app.set('views', path.join(__dirname, '..'));
const articles = {};
let lastRSS = '';
/**
* Fetch articles from the data folder and send success as a response
* @param success
* @param error
*/
const reload = (success, error) => {
fw.fetchArticles((err, dict) => {
if (err) {
console.error(cons.error, 'error loading articles : ' + err);
return error ? error() : null;
}
Object.keys(articles).forEach((key) => delete articles[key]);
Object.keys(dict).forEach((key) => articles[key] = dict[key]);
const nb = Object.keys(articles).length;
if (nb > 0)
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
else
console.log(cons.warn, `no articles loaded, check your configuration`);
lastRSS = '';
success();
});
};
if (config['test'])
app.reload = reload;
return app;
/**
* 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) => {
res.render(vPath, data, (err, html) => {
if (err) {
res.sendStatus(500);
console.log(cons.error, `failed to render ${vPath} : ${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) => {
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);
});
};
// home endpoint : send the correct index page or error if not existing
app.get('/', (req, res) => {
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);
else
render(res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))});
});
});
//RSS endpoint
app.get(config['rss']['endpoint'], (req, res) => {
if (config['modules']['rss']) {
if (!lastRSS) {
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
});
Object.values(articles)
.slice(0, config['rss']['length'])
.forEach((article) => {
feed.item({
title: article.title,
url: 'http://' + req.headers.host + article.url,
date: article.date
});
});
lastRSS = feed.xml();
}
res.type('rss').send(lastRSS);
} else {
showError(req.path, 404, res);
}
});
//webhook endpoint
app.post(config['webhook']['endpoint'], (req, res) => {
if (config['modules']['webhook']) {
if (config['webhook']['signature_header'] && config['webhook']['secret']) {
const payload = JSON.stringify(req.body);
if (!payload) {
return res.sendStatus(403);
}
const hmac = crypto.createHmac('sha1', config['webhook']['secret']);
const digest = 'sha1=' + hmac.update(payload).digest('hex');
const checksum = req.headers[config['webhook']['signature_header']];
if (!checksum || !digest || checksum !== digest) {
return res.sendStatus(403);
}
}
cp.exec(config['webhook']['pull_command'], {cwd: path.join(__dirname, '..', config['data_dir'])}, (err) => {
if (err) {
console.log(cons.error, `command '${config['webhook']['pull_command']}' failed : ${err}`);
return res.sendStatus(500);
}
reload(() => {
res.sendStatus(200);
});
});
} else {
res.sendStatus(400);
}
});
//rewrite urls to hide articles titles : /2019/05/05/sometitle/img.png => /2019/05/05/img.png
app.use((req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\//.test(req.url))
req.url = req.url.slice(0, 11) + req.url.slice(req.url.lastIndexOf('/'));
next();
});
// catch all article urls and render them
app.get('*', (req, res, next) => {
if (/^\/\d{4}\/\d{2}\/\d{2}\/$/.test(req.path)) {
const articlePath = req.path.substr(1, 10);
const article = articles[articlePath];
if (!article)
showError(req.path, 404, res);
else {
renderer.render(path.join(article.realPath, config['article']['index']), (err, html) => {
if (err) {
console.log(cons.error, `failed to render article ${req.path} : ${err}`);
return showError(req.path, 500, res);
}
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);
} else
render(res, templatePath, {article: article});
});
});
}
} else {
next();
}
});
// 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();
});
// serve all static files via get
app.get('*', express.static(config['data_dir']));
// catch express.static errors (mostly not found) by displaying 404
app.get('*', (req, res) => {
showError(req.path, 404, res);
});
// catch all other methods and return 400
app.all('*', (req, res) => {
res.status(400).send('bad request');
});
app.use((err, req, res, next) => {
console.log(cons.error, `error when handling ${req.path} request : ${err}`);
console.error(err.stack);
next(err);
});
// must be use in a server.js to start the server
app.start = () => {
reload(() => {
app.listen(config['node_port'], () => {
console.log(cons.ok, `gitblog.md server listening on port ${config['node_port']}`);
});
});
};
return app;
};
+44
View File
@@ -0,0 +1,44 @@
{
"node_port": 3000,
"data_dir": "data",
"view_engine": "ejs",
"modules": {
"rss": true,
"webhook": true,
"prism": true
},
"home": {
"index": "index.ejs",
"error": "error.ejs",
"hidden": [
".ejs"
]
},
"article": {
"index": "index.md",
"template": "template.ejs",
"thumbnail_tag": "thumbnail",
"default_title": "Untitled",
"default_thumbnail": null
},
"rss": {
"title": "mygitblog RSS feed",
"description": "a generated RSS feed from my articles",
"endpoint": "/rss",
"length": 10
},
"webhook": {
"endpoint": "/webhook",
"secret": "",
"signature_header": "",
"pull_command": "git pull"
},
"showdown": {
"parseImgDimensions": true,
"strikethrough": true,
"tables": true,
"tasklists": true,
"openLinksInNewWindow": true,
"emoji": true
}
}
+31
View File
@@ -0,0 +1,31 @@
const refConfig = require('./config.default.json');
const fs = require('fs');
/**
* Merge resources by reading object keys and keeping reference value only if it's type is different from the source
* @param ref - reference object/value
* @param src - source object/value
* @returns {*}
*/
const merge = (ref, src) => {
if (typeof ref !== typeof src) {
return ref;
} else if (typeof ref === 'object') {
const out = {};
Object.keys(ref).forEach((key) => out[key] = merge(ref[key], src[key]));
return out;
} else {
return src;
}
};
module.exports = () => {
try {
let configData = fs.readFileSync('config.json', {encoding: 'UTF-8'});
let config = JSON.parse(configData);
return merge(refConfig, config);
} catch (error) {
console.error('Failed to load config.json : ' + error);
return refConfig;
}
};
+109
View File
@@ -0,0 +1,109 @@
const fs = require('fs');
const path = require('path');
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/');
/**
* Get all files path inside a given folder path
* @param dir
* @param cb
*/
const getFileTree = (dir, cb) => {
let list = [];
let remaining = 0;
fs.readdir(dir, {withFileTypes: true}, (err, items) => {
if (err)
return cb(err);
items.forEach((item) => {
if (item.isDirectory()) {
remaining++;
getFileTree(path.join(dir, item.name), (err, out) => {
if (err)
return cb(err);
list.push(...out);
remaining--;
if (remaining === 0)
cb(null, list);
});
} else {
list.push(path.join(dir, item.name));
}
});
if (remaining === 0)
cb(null, list);
});
};
/**
* Tries to read a markdown file and match a title and a thumbnail
* @param path
* @param thumbnailTag - how the thumbnail image desc is given as ![thumbnailTag](url)
* @param cb
*/
const readIndexFile = (path, thumbnailTag, cb) => {
fs.readFile(path, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
let info = {};
const regRes1 = data.match(/(^|[^#])#([^#\r\n]*)\r?\n?$/m);
info.title = regRes1 ? regRes1[2].trim() : undefined;
const thumbnailRegEx = new RegExp(`!\\[${thumbnailTag}]\\(([^)]*)\\)`, 'i');
const regRes2 = data.match(thumbnailRegEx);
info.thumbnail = regRes2 ? regRes2[1].trim() : undefined;
cb(null, info);
});
};
module.exports = (config) => {
return {
fileTree: config['test'] ? getFileTree : undefined,
readIndexFile: config['test'] ? readIndexFile : undefined,
/**
* find and read all articles inside the data directory
* @param cb
*/
fetchArticles: (cb) => {
getFileTree(config['data_dir'], (err, fileList) => {
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'] &&
/^\d{4}$/.test(p[0]) && /^\d{2}$/.test(p[1]) && /^\d{2}$/.test(p[2]));
if (paths.length === 0)
cb(null, {});
const articles = {};
let remaining = 0;
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]),
year: parseInt(p[0]),
month: parseInt(p[1]),
day: parseInt(p[2])
};
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) => {
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;
remaining--;
if (remaining === 0)
cb(null, articles);
});
});
});
}
};
};
+26 -14
View File
@@ -1,18 +1,30 @@
const fs = require('fs');
const path = require('path');
const ncp = require('ncp').ncp;
const pad0 = n => ('0'+n).substr(-2);
const datetime = new Date();
const dir = `./data/${datetime.getFullYear()}/${pad0(datetime.getMonth())}/${pad0(datetime.getDay())}/`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
}
ncp('./sample_data/',dir, function(err){
if(err)
console.error(err);
const copy = (src, dest) => {
ncp(src, dest, function (err) {
if (err)
console.error(err);
else
console.log(`sample data copied to ${dir}`);
});
console.log(`copied ${src} to ${dest}`);
});
};
copy(path.join('src', 'config.default.json'), 'config.example.json');
if (!fs.existsSync('data')) {
fs.mkdirSync('data');
copy(path.join('sample_data', 'home'), 'data');
const pad0 = (n) => ('0' + n).substr(-2);
const datetime = new Date();
const dir = path.join('data', datetime.getFullYear().toString(), pad0(datetime.getMonth() + 1), pad0(datetime.getDate()));
if (!fs.existsSync(dir))
fs.mkdirSync(dir, {recursive: true});
copy(path.join('sample_data', 'article'), dir);
}
+37
View File
@@ -0,0 +1,37 @@
const fs = require('fs');
const showdown = require('showdown');
const Prism = require('node-prismjs');
module.exports = (config) => {
const converter = new showdown.Converter(config['showdown']);
return {
render: (file, cb) => {
fs.readFile(file, {encoding: 'UTF-8'}, (err, data) => {
if (err)
return cb(err);
if (config['modules']['prism']) {
const codeRegex = /```([\w-]+)\n((?:(?!```)[\s\S])*)\n```/m;
let match;
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 html = converter.makeHtml(data);
cb(null, html);
});
}
};
};
+2 -7
View File
@@ -1,9 +1,4 @@
const config = require('../config.json');
const config = require('./config')();
const app = require('./app')(config);
const port = config.nodePort|3000;
app.listen(config.nodePort|3000, () => {
console.log(`gitblog.md server listening on port ${port}`);
});
app.start();
+320 -8
View File
@@ -1,14 +1,326 @@
/* jshint -W117 */
const request = require('supertest');
const config = require('./config.test.json');
const fs = require('fs');
const path = require('path');
const utils = require('./test_utils');
const dataDir = 'test_data';
const testIndex = 'testindex.ejs';
const testError = 'testerror.ejs';
const testTemplate = 'testtemplate.ejs';
const config = require('../src/config')();
config['test'] = true;
config['data_dir'] = dataDir;
config['home']['index'] = testIndex;
config['home']['error'] = testError;
config['article']['template'] = testTemplate;
config['home']['hidden'].push('.test');
config['rss']['endpoint'] = '/rsstest';
config['rss']['length'] = 2;
config['webhook']['endpoint'] = '/webhooktest';
const app = require('../src/app')(config);
describe('Test root path', () => {
test('GET / 200', done => {
request(app).get('/').then(response => {
expect(response.statusCode).toBe(200);
done();
});
});
beforeEach((done, fail) => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
app.reload(done, fail);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
describe('Test root path', () => {
test('404 no index no error', (done) => {
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('404 no index but error page', (done) => {
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(404);
expect(response.text).toBe('error 404 at /');
done();
});
});
test('200 no articles', (done) => {
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('articles 0');
done();
});
});
test('200 2 articles', (done, fail) => {
utils.createEmptyDirs([
path.join(dataDir, '2019', '05', '05'),
path.join(dataDir, '2018', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, '2018', '05', '05', 'index.md')
]);
fs.writeFileSync(path.join(dataDir, testIndex), 'articles <%= articles.length %>');
app.reload(() => {
request(app).get('/').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('articles 2');
done();
});
}, fail);
});
});
describe('Test RSS feed', () => {
test('404 rss deactivated', (done) => {
config['modules']['rss'] = false;
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(404);
config['modules']['rss'] = true;
done();
});
});
test('200 empty rss', (done) => {
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(1);
done();
});
});
test('200 2 rss items', (done, fail) => {
utils.createEmptyDirs([
path.join(dataDir, '2019', '05', '05'),
path.join(dataDir, '2018', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, '2018', '05', '05', 'index.md')
]);
app.reload(() => {
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(3);
done();
});
}, fail);
});
test('200 max rss items', (done, fail) => {
utils.createEmptyDirs([
path.join(dataDir, '2019', '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, '2017', '05', '05', 'index.md')
]);
app.reload(() => {
request(app).get('/rsstest').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text.length).toBeGreaterThan(0);
expect(response.text.split('<item>').length).toBe(3);
done();
});
}, fail);
});
});
describe('Test webhook', () => {
test('400 webhook deactivated', (done) => {
config['modules']['webhook'] = false;
request(app).post('/webhooktest').then((response) => {
expect(response.statusCode).toBe(400);
config['modules']['webhook'] = true;
done();
});
});
test('200 no secret', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate)
]);
config['webhook']['pull_command'] = 'git --help';
request(app).post('/webhooktest').then((response) => {
expect(response.statusCode).toBe(200);
request(app).get('/2019/05/05/').then((response) => {
expect(response.statusCode).toBe(200);
done();
});
});
});
test('500 command failed', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate)
]);
config['webhook']['pull_command'] = 'qzgfqgqz';
request(app).post('/webhooktest').then((response) => {
expect(response.statusCode).toBe(500);
done();
});
});
test('403 no payload', (done) => {
config['webhook']['signature_header'] = 'testheader';
config['webhook']['secret'] = 'testvalue';
request(app).post('/webhooktest').then((response) => {
expect(response.statusCode).toBe(403);
done();
});
});
test('403 wrong secret', (done) => {
config['webhook']['signature_header'] = 'testheader';
config['webhook']['secret'] = 'testvalue';
request(app).post('/webhooktest').set('testheader', 'sha1=invalid').then((response) => {
expect(response.statusCode).toBe(403);
done();
});
});
test('200 valid secret', (done) => {
config['webhook']['signature_header'] = 'testheader';
config['webhook']['secret'] = 'testvalue';
config['webhook']['pull_command'] = 'git --help';
request(app).post('/webhooktest')
.send({})
.set('testheader', 'sha1=d924d5bd4b36faf9d572844ac9c12a09ce3e7134')
.then((response) => {
expect(response.statusCode).toBe(200);
done();
});
});
});
describe('Test articles rendering', () => {
test('404 article not found', (done) => {
request(app).get('/2019/05/06/untitled/').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('500 no template', (done, fail) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'index.md'), '# Hello');
app.reload(() => {
request(app).get('/2019/05/05/hello/').then((response) => {
expect(response.statusCode).toBe(500);
done();
});
}, fail);
});
test('200 rendered article', (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>` %>');
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([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate)
]);
app.reload(() => {
request(app).get('/2019/05/05/anything/').then((response) => {
expect(response.statusCode).toBe(200);
done();
});
}, fail);
});
test('200 other url 2', (done, fail) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate)
]);
app.reload(() => {
request(app).get('/2019/05/05/').then((response) => {
expect(response.statusCode).toBe(200);
done();
});
}, fail);
});
});
describe('Test static files', () => {
test('404 invalid file no error page', (done) => {
request(app).get('/somefile.txt').then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('404 invalid file but error page', (done) => {
fs.writeFileSync(path.join(dataDir, testError), 'error <%= error %> at <%= path %>');
request(app).get('/somefile.txt').then((response) => {
expect(response.statusCode).toBe(404);
expect(response.text).toBe('error 404 at /somefile.txt');
done();
});
});
test('404 hidden file', (done) => {
fs.writeFileSync(path.join(dataDir, 'somefile.test'), '');
request(app).get('/somefile.test').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) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('filecontent');
done();
});
});
test('200 valid resource of article', (done) => {
utils.createEmptyDirs([path.join(dataDir, '2019', '05', '05'),]);
fs.writeFileSync(path.join(dataDir, '2019', '05', '05', 'somefile.txt'), 'filecontent');
request(app).get('/2019/05/05/title/somefile.txt').then((response) => {
expect(response.statusCode).toBe(200);
expect(response.text).toBe('filecontent');
done();
});
});
});
describe('Test other requests', () => {
test('400 POST', (done) => {
request(app).post('/').then((response) => {
expect(response.statusCode).toBe(400);
done();
});
});
test('400 PUT', (done) => {
request(app).put('/').then((response) => {
expect(response.statusCode).toBe(400);
done();
});
});
test('400 DELETE', (done) => {
request(app).delete('/').then((response) => {
expect(response.statusCode).toBe(400);
done();
});
});
});
+54
View File
@@ -0,0 +1,54 @@
/* jshint -W117 */
const fs = require('fs');
const configFile = 'config.json';
const tmpConfigFile = 'config.temp.json';
beforeAll(() => {
if (fs.existsSync(configFile)) {
fs.renameSync(configFile, tmpConfigFile);
}
expect(fs.existsSync(configFile)).toBeFalsy();
});
afterAll(() => {
if (fs.existsSync(tmpConfigFile)) {
fs.renameSync(tmpConfigFile, configFile);
} else if (fs.existsSync(configFile)) {
fs.unlinkSync(configFile); //remove config file if remaining
}
});
test('no config', () => {
if (fs.existsSync(configFile))
fs.unlinkSync(configFile);
expect(fs.existsSync(configFile)).toBeFalsy();
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data');
});
test('invalid config ignored', () => {
fs.writeFileSync(configFile, 'invalid JSON');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data');
});
test('good config merged', () => {
fs.writeFileSync(configFile, '{"node_port":5000}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(5000);
expect(config['data_dir']).toBe('data');
});
test('wrong config fixed', () => {
fs.writeFileSync(configFile, '{"node_port":"hello","data_dir":"data2"}');
const config = require('../src/config')();
expect(config).toBeDefined();
expect(config['node_port']).toBe(3000);
expect(config['data_dir']).toBe('data2');
});
-3
View File
@@ -1,3 +0,0 @@
{
}
+277
View File
@@ -0,0 +1,277 @@
/* jshint -W117 */
const fs = require('fs');
const path = require('path');
const utils = require('./test_utils');
const dataDir = 'test_data';
const testIndex = 'testindex.md';
const joinUrl = (...paths) => path.join(...paths).replace(/\\/g,'/');
const config = {
'test': true,
'data_dir': dataDir,
'article': {
'index': testIndex,
'default_title': 'Untitled',
'default_thumbnail': 'default.png',
'thumbnail_tag': 'thumbnail'
}
};
const fw = require('../src/file_walker')(config);
beforeEach(() => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
describe('Test function fileTree', () => {
test('empty root', (done) => {
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(0);
done();
});
});
test('empty folders', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, 'test', 'test2'),
path.join(dataDir, 'test2')
]);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(0);
done();
});
});
test('simple files', (done) => {
const fileList = [
path.join(dataDir, 'f1.txt'),
path.join(dataDir, 'f2.txt')
];
utils.createEmptyFiles(fileList);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(fileList.length);
expect(list).toEqual(expect.arrayContaining(fileList));
done();
});
});
test('nested files', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, 'test2')
]);
const fileList = [
path.join(dataDir, 'f1.txt'),
path.join(dataDir, 'test', 'f2.txt'),
path.join(dataDir, 'test', 'test', 'f3.txt'),
path.join(dataDir, 'test2', 'f4.txt')
];
utils.createEmptyFiles(fileList);
fw.fileTree(dataDir, (err, list) => {
expect(err).toBeNull();
expect(list).toBeDefined();
expect(list.length).toBe(fileList.length);
expect(list).toEqual(expect.arrayContaining(fileList));
done();
});
});
test('invalid root', (done) => {
fw.fileTree('invalid root', (err, list) => {
expect(err).not.toBeNull();
expect(list).not.toBeDefined();
done();
});
});
});
describe('Test index article reading', () => {
const file = path.join(dataDir, testIndex);
test('invalid file', (done) => {
fw.readIndexFile('invalid file', 'thumbnail', (err, info) => {
expect(err).not.toBeNull();
expect(info).not.toBeDefined();
done();
});
});
test('correct file', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
test('no title', (done) => {
fs.writeFileSync(file, `
## This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
### this is some text
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).not.toBeDefined();
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
test('title at beginning', (done) => {
fs.writeFileSync(file, '#title');
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('title');
expect(info.thumbnail).not.toBeDefined();
done();
});
});
test('no thumbnail', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
`);
fw.readIndexFile(file, 'thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).not.toBeDefined();
done();
});
});
test('multiple thumbnails', (done) => {
fs.writeFileSync(file, `
# This is an awesome title !?¤
![custom_thumbnail](./thumbnail.jpg)
this is some text
![custom_thumbnail](./thumbnail2.jpg)
`);
fw.readIndexFile(file, 'custom_thumbnail', (err, info) => {
expect(err).toBeNull();
expect(info).toBeDefined();
expect(info.title).toBe('This is an awesome title !?¤');
expect(info.thumbnail).toBe('./thumbnail.jpg');
done();
});
});
});
describe('Test article fetching', () => {
test('invalid data dir', (done) => {
config['data_dir'] = 'invalid root';
fw.fetchArticles((err, list) => {
expect(err).not.toBeNull();
expect(list).not.toBeDefined();
config['data_dir'] = dataDir;
done();
});
});
test('empty data dir', (done) => {
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(0);
done();
});
});
test('misplaced index file', (done) => {
utils.createEmptyDirs([
path.join(dataDir, 'test', 'test'),
path.join(dataDir, '2019', '05', '05')
]);
utils.createEmptyFiles([
path.join(dataDir, testIndex),
path.join(dataDir, 'test', 'test', testIndex),
path.join(dataDir, '2019', '05', testIndex)
]);
fw.fetchArticles((err, dict) => {
expect(err).toBeNull();
expect(dict).toBeDefined();
expect(Object.keys(dict).length).toBe(0);
done();
});
});
test('empty index file', (done) => {
const dir = path.join(dataDir, '2019', '05', '05');
const file = path.join(dir, testIndex);
utils.createEmptyDirs([dir]);
utils.createEmptyFiles([file]);
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: dir,
year: 2019,
month: 5,
day: 5,
date: date,
title: 'Untitled',
thumbnail: 'default.png',
escapedTitle: 'untitled',
url: '/' + joinUrl('2019', '05', '05', 'untitled') + '/',
});
done();
});
});
test('correct index file', (done) => {
const dir = path.join(dataDir, '2019', '05', '05');
const file = path.join(dir, testIndex);
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: dir,
year: 2019,
month: 5,
day: 5,
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();
});
});
});
+96
View File
@@ -0,0 +1,96 @@
/* jshint -W117 */
const fs = require('fs');
const path = require('path');
const utils = require('./test_utils');
const dataDir = 'test_data';
const file = path.join(dataDir, 'test.md');
const config = {
'modules': {
'prism': true,
},
'showdown': {
'simplifiedAutoLink': true,
'smartIndentationFix': true
}
};
const renderer = require('../src/renderer')(config);
beforeEach(() => {
utils.deleteFolderSync(dataDir);
fs.mkdirSync(dataDir);
});
afterAll(() => {
if (fs.existsSync(dataDir)) {
utils.deleteFolderSync(dataDir);
}
});
test('invalid file', (done) => {
renderer.render('invalid file', (err, html) => {
expect(err).not.toBeNull();
expect(html).not.toBeDefined();
done();
});
});
test('normal file', (done) => {
fs.writeFileSync(file, `# Hello`);
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<h1 id="hello">Hello</h1>');
done();
});
});
test('custom rules', (done) => {
fs.writeFileSync(file, `www.google.com`);
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<p><a href="http://www.google.com">www.google.com</a></p>');
done();
});
});
test('no prism', (done) => {
config['modules']['prism'] = false;
fs.writeFileSync(file, '```python\nprint("hello")\n```\n\n```python\nprint("hello")\n```');
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<pre><code class="python language-python">print("hello")\n</code></pre>\n<pre><code class="python language-python">print("hello")\n</code></pre>');
config['modules']['prism'] = true;
done();
});
});
test('prism correct', (done) => {
fs.writeFileSync(file, '```python\nprint("hello")\n```');
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).not.toBe('<pre><code class="python language-python">print("hello")\n</code></pre>');
expect(html.indexOf('<pre><code class="python language-python">')).toBe(0);
done();
});
});
test('prism invalid lang', (done) => {
fs.writeFileSync(file, '```pythdon\nprint("hello")\n```');
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).not.toBe('<pre><code class="pythdon language-pythdon">print("hello")\n</code></pre>');
expect(html.indexOf('<pre><code class="pythdon language-pythdon">')).toBe(0);
done();
});
});
test('prism mutliple code blocks', (done) => {
fs.writeFileSync(file, '```python\n\n```\n\n```python\n\n```');
renderer.render(file, (err, html) => {
expect(err).toBeNull();
expect(html).toBe('<pre><code class="python language-python"></code></pre>\n<pre><code class="python language-python"></code></pre>');
done();
});
});
+20
View File
@@ -0,0 +1,20 @@
const fs = require('fs');
const path = require('path');
const deleteFolderSync = (dir) => {
if (!fs.existsSync(dir))
return;
fs.readdirSync(dir, {withFileTypes: true}).forEach((item) => {
if (item.isDirectory())
deleteFolderSync(path.join(dir, item.name));
else
fs.unlinkSync(path.join(dir, item.name));
});
fs.rmdirSync(dir);
};
module.exports = {
deleteFolderSync: deleteFolderSync,
createEmptyDirs: (list) => list.forEach((path) => fs.mkdirSync(path, {recursive: true})),
createEmptyFiles: (list) => list.forEach((file) => fs.writeFileSync(file, '')),
};