Skip to content

Commit 5113f70

Browse files
author
davidliang
committed
first commit
1 parent 5fd0c4f commit 5113f70

File tree

3 files changed

+92
-136
lines changed

3 files changed

+92
-136
lines changed

README.md

100644100755
Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
1-
GitBook Sample Plugin
1+
**GitBook PlantUml Plugin**
22
==============
3+
* inspire by [romanlytkin/gitbook-plantuml](https://github.com/romanlytkin/gitbook-plantuml)
4+
* use [bitjourney/plantuml-service](https://github.com/bitjourney/plantuml-service)
5+
to support plantuml, so you do not need java run time on your host machine.
36

4-
This is a model for GitBook plugins.
5-
6-
## How GitBook plugin works?
7-
8-
A plugin for GitBook is a node package that can be published on [NPM](http://www.npmjs.org). It has to follow the name convention: `gitbook-plugin-*name*`.
9-
10-
### package.json
11-
12-
#### name
13-
14-
The package name should begin with ```gitbook-plugin-```.
15-
16-
Examples: `gitbook-plugin-mixpanel`, `gitbook-plugin-googleanalytics`.
17-
18-
#### engine
19-
20-
The package.json should contain a `engine` field using [the standard norm](https://www.npmjs.org/doc/json.html#engines).
21-
22-
```
23-
"engines": {
24-
"gitbook": "*"
25-
}
26-
```
27-
28-
For example if you want your plugin to supports only GitBook version supperior to 0.3.1:
29-
30-
```
31-
"engines": {
32-
"gitbook": ">=0.3.1"
33-
}
34-
```
35-
36-
### entry point
37-
38-
The plugin entry point should return an object with some metadata.
39-
7+
**How to use it:**
8+
--------------
9+
```$ npm install gitbook-plugin-plantuml-cloud```
4010

11+
***Additional requirements:***
12+
NO

index.js

100644100755
Lines changed: 59 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,61 @@
1-
module.exports = {
2-
// Extend website resources and html
3-
website: {
4-
assets: "./book",
5-
js: [
6-
"test.js"
7-
],
8-
css: [
9-
"test.css"
10-
],
11-
html: {
12-
"html:start": function() {
13-
return "<!-- Start book "+this.options.title+" -->"
14-
},
15-
"html:end": function() {
16-
return "<!-- End of book "+this.options.title+" -->"
17-
},
18-
19-
"head:start": "<!-- head:start -->",
20-
"head:end": "<!-- head:end -->",
21-
22-
"body:start": "<!-- body:start -->",
23-
"body:end": "<!-- body:end -->"
24-
}
25-
},
26-
27-
// Extend ebook resources and html
28-
website: {
29-
assets: "./book",
30-
js: [
31-
"test.js"
32-
],
33-
css: [
34-
"test.css"
35-
],
36-
html: {
37-
"html:start": function() {
38-
return "<!-- Start book "+this.options.title+" -->"
39-
},
40-
"html:end": function() {
41-
return "<!-- End of book "+this.options.title+" -->"
42-
},
43-
44-
"head:start": "<!-- head:start -->",
45-
"head:end": "<!-- head:end -->",
1+
var fs = require('fs');
2+
var re = /^```uml((.*\n)+?)?```$/img;
3+
var crypto = require('crypto');
4+
var path = require('path');
5+
var https = require('https');
6+
var qs = require('querystring');
467

47-
"body:start": "<!-- body:start -->",
48-
"body:end": "<!-- body:end -->"
49-
}
50-
},
8+
require('shelljs/global');
519

52-
// Extend templating blocks
53-
blocks: {
54-
// Author will be able to write "{% myTag %}World{% endMyTag %}"
55-
myTag: {
56-
process: function(blk) {
57-
return "Hello "+blk.body;
58-
}
59-
}
60-
},
61-
62-
// Extend templating filters
63-
filters: {
64-
// Author will be able to write "{{ 'test'|myFilter }}"
65-
myFilter: function(s) {
66-
return "Hello "+s;
67-
}
68-
},
69-
70-
// Hook process during build
71-
hooks: {
72-
// For all the hooks, this represent the current generator
73-
74-
// This is called before the book is generated
75-
"init": function() {
76-
console.log("init!");
77-
},
78-
79-
// This is called after the book generation
80-
"finish": function() {
81-
console.log("finish!");
82-
}
83-
}
84-
};
10+
module.exports = {
11+
hooks: {
12+
"init": function () {
13+
var output = this.output;
14+
var umlPath = output.resolve('assets/images/uml');
15+
16+
mkdir('-p', umlPath);
17+
},
18+
"page:before": function (page) {
19+
var output = this.output;
20+
var content = page.content;
21+
22+
var umls = [];
23+
while ((match = re.exec(content))) {
24+
var rawBlock = match[0];
25+
var umlBlock = match[1];
26+
var md5 = crypto.createHash('md5').update(umlBlock).digest('hex');
27+
var svgPath = path.join('assets', 'images', 'uml', md5 + '.svg');
28+
umls.push({
29+
rawBlock: match[0],
30+
umlBlock: match[1],
31+
svgPath: svgPath
32+
});
33+
}
34+
35+
Promise.all([umls.map(uml => {
36+
var svgTag = '![](/' + uml.svgPath + ')';
37+
page.content = content = content.replace(uml.rawBlock, svgTag);
38+
39+
return output.hasFile(uml.svgPath).then(exists => {
40+
if (!exists) {
41+
return new Promise((resolve, reject) => {
42+
https.request({
43+
host: 'plantuml-service.herokuapp.com',
44+
path: '/svg/' + qs.escape(uml.umlBlock)
45+
}, (res) => {
46+
var ws = fs.createWriteStream(output.resolve(uml.svgPath));
47+
res.pipe(ws);
48+
res.on('end', resolve);
49+
}).end();
50+
});
51+
}
52+
})
53+
})]);
54+
55+
return page;
56+
},
57+
58+
"page": function (page) { return page; },
59+
"page:after": function (page) { return page; }
60+
}
61+
};

package.json

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
{
2-
"name": "gitbook-plugin",
3-
"description": "Sample plugin for GitBook",
4-
"main": "index.js",
5-
"version": "0.0.2",
6-
"engines": {
7-
"gitbook": "*"
8-
},
9-
"homepage": "https://github.com/GitbookIO/plugin",
10-
"repository": {
11-
"type": "git",
12-
"url": "https://github.com/GitbookIO/plugin.git"
13-
},
14-
"license": "Apache 2",
15-
"bugs": {
16-
"url": "https://github.com/GitbookIO/plugin/issues"
17-
}
18-
}
2+
"name": "gitbook-plugin-plantuml-cloud",
3+
"description": "Sample plugin for GitBook",
4+
"main": "index.js",
5+
"version": "0.0.2",
6+
"engines": {
7+
"gitbook": "*"
8+
},
9+
"homepage": "https://github.com/GitbookIO/plugin",
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/GitbookIO/plugin.git"
13+
},
14+
"license": "Apache-2.0",
15+
"bugs": {
16+
"url": "https://github.com/GitbookIO/plugin/issues"
17+
},
18+
"scripts": {
19+
"test": "echo \"Error: no test specified\" && exit 1"
20+
},
21+
"author": "dalia <dy93_@hotmail.com>",
22+
"dependencies": {
23+
"shelljs": "^0.7.6"
24+
}
25+
}

0 commit comments

Comments
 (0)