Skip to content

Commit f076253

Browse files
author
瓷松
committed
init git-author plugin
0 parents  commit f076253

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[package.json]
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.DS_Store
3+
npm-debug.log
4+
node_modules

README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# gitbook-plugin-git-author
2+
3+
This is a plugin for automatically adding author and timestamp to each gitbook article, including creator and last modified user from git commits
4+
5+
## Example
6+
7+
`README.md` file
8+
9+
Be sure to commit this file to git repository
10+
11+
```markdown
12+
# Title of the Article
13+
14+
content
15+
```
16+
17+
### Output
18+
19+
```html
20+
<h1>Title of the Article</h1>
21+
22+
<p>content</p>
23+
24+
<div class="git-author-container">
25+
<div class="modified">Last modified by someone 2016-06-06 06:06:06</div>
26+
<div class="created">Created by someone 2016-06-06 06:06:06</div>
27+
</div>
28+
```
29+
30+
## Usage
31+
32+
### install
33+
34+
```sh
35+
npm i -D gitbook-plugin-git-author
36+
```
37+
38+
### book.json
39+
40+
```js
41+
{
42+
"plugins": ["git-author"]
43+
"pluginsConfig": {
44+
"git-author":{
45+
"modifyTpl": "Last modified by {user} {timeStamp}",
46+
"createTpl": "Created by {user} {timeStamp}",
47+
"timeStampFormat": "YYYY-MM-DD HH:mm:ss"
48+
}
49+
}
50+
}
51+
```
52+
53+
## hide creator
54+
55+
```
56+
"createTpl": false
57+
```
58+
59+
## custom styles
60+
61+
`book.json`
62+
63+
```js
64+
{
65+
"styles": {
66+
"website": "./website.css"
67+
}
68+
}
69+
```
70+
71+
`website.css`
72+
73+
```
74+
.git-author-container {
75+
font-size: 85%;
76+
}
77+
```
78+
79+

assets/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.git-author-container {
2+
text-align: right;
3+
}

index.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
const execa = require('execa');
4+
const moment = require('moment');
5+
6+
function substitute(str, o) {
7+
return str.replace(/\\?\{([^{}]+)\}/g, function (match, name) {
8+
if (match.charAt(0) === '\\') {
9+
return match.slice(1);
10+
}
11+
return (o[name] === undefined) ? '' : o[name];
12+
});
13+
}
14+
15+
module.exports = {
16+
website: {
17+
assets: './assets',
18+
css: [
19+
'style.css'
20+
]
21+
},
22+
23+
hooks: {
24+
"page:before": function (page) {
25+
const defaults = {
26+
createTpl: 'Created by {user} at {timeStamp}',
27+
modifyTpl: 'Last modified by {user} at {timeStamp}',
28+
timeStampFormat: 'YYYY-MM-DD HH:mm:ss'
29+
};
30+
const pluginConfig = this.config.get('pluginsConfig')['git-author'];
31+
const options = Object.assign({}, defaults, pluginConfig);
32+
33+
const createTpl = options.createTpl;
34+
const modifyTpl = options.modifyTpl;
35+
const timeStampFormat = options.timeStampFormat;
36+
37+
return execa.shell(`git log --format="%an|%at" -- ${page.rawPath}`).then((ret) => {
38+
// none commit to this file
39+
if (!ret.stdout) {
40+
return page;
41+
}
42+
43+
const commits = ret.stdout.split(/\r?\n/).map((log) => {
44+
const arr = log.split('|').map(r => r.trim());
45+
const timeStamp = moment(arr[1] * 1000).format(timeStampFormat);
46+
47+
return {
48+
user: arr[0],
49+
timestamp: timeStamp,
50+
timeStamp: timeStamp
51+
}
52+
});
53+
54+
const lastCommit = commits[0];
55+
const firstCommit = commits.slice(-1)[0];
56+
57+
let gitAuthorContent = '<div class="git-author-container">';
58+
59+
if (modifyTpl) {
60+
const modifyMsg = substitute(modifyTpl, lastCommit);
61+
gitAuthorContent += `<div class="modified">${modifyMsg}</div>`;
62+
}
63+
64+
if (createTpl) {
65+
const createMsg = substitute(createTpl, firstCommit);
66+
gitAuthorContent += `<div class="created">${createMsg}</div>`;
67+
}
68+
69+
gitAuthorContent += '</div>';
70+
71+
page.content += gitAuthorContent;
72+
73+
return page;
74+
}).catch((e) => {
75+
this.log.info(e);
76+
return page;
77+
});
78+
}
79+
}
80+
};

package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "gitbook-plugin-git-author",
3+
"description": "show git author for each article of gitbook",
4+
"main": "index.js",
5+
"version": "0.0.1",
6+
"author": "L3au <leshu.lau@gmail.com>",
7+
"engines": {
8+
"node": ">4.0.0",
9+
"gitbook": ">3.0.0"
10+
},
11+
"keywords": [
12+
"gitbook",
13+
"plugin",
14+
"git",
15+
"author",
16+
"signature",
17+
"modified",
18+
"creator"
19+
],
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/L3au/gitbook-plugin-git-author.git"
23+
},
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/L3au/gitbook-plugin-git-author/issues"
27+
},
28+
"dependencies": {
29+
"execa": "^0.4.0",
30+
"moment": "^2.13.0"
31+
}
32+
}

0 commit comments

Comments
 (0)