Skip to content

Commit 95a1e17

Browse files
committed
refactor: file structure
1 parent 6be4c0a commit 95a1e17

File tree

9 files changed

+116
-4063
lines changed

9 files changed

+116
-4063
lines changed

Jenkinsfile

-11
This file was deleted.

README.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
# gitbook-plugin-git-author
22

3-
[![Greenkeeper badge](https://badges.greenkeeper.io/L3au/gitbook-plugin-git-author.svg)](https://greenkeeper.io/)
4-
5-
This is a plugin for automatically adding author and timestamp to each gitbook article, including creator and last modified user from git commits
3+
This is a plugin for automatically adding author and timestamp to each gitbook page, including creator and last-modified from git commits
64

75
[![npm](https://img.shields.io/npm/v/gitbook-plugin-git-author.svg)](https://www.npmjs.com/package/gitbook-plugin-git-author)
86
[![travis](https://img.shields.io/travis/L3au/gitbook-plugin-git-author.svg)](https://travis-ci.org/L3au/gitbook-plugin-git-author)
97
[![codecov](https://codecov.io/gh/L3au/gitbook-plugin-git-author/branch/master/graph/badge.svg)](https://codecov.io/gh/L3au/gitbook-plugin-git-author)
108
[![npm-downloads](https://img.shields.io/npm/dm/gitbook-plugin-git-author.svg)](https://www.npmjs.com/package/gitbook-plugin-git-author)
119

12-
![git-author-preview](https://raw.githubusercontent.com/L3au/gitbook-plugin-git-author/master/preview.png)
10+
![git-author-preview](https://raw.githubusercontent.com/L3au/gitbook-plugin-git-author/master/src/assets/preview.png)
1311

1412
## Usage
1513

1614
### Recommended Environments
1715

18-
1916
- Node.js 4.0+
2017
- npm 3.0+
2118
- gitbook 3.0+
@@ -62,8 +59,8 @@ content
6259
<p>content</p>
6360

6461
<div class="git-author-container git-author-bottom">
65-
<div class="modified">Last modified by someone 2016-06-06 06:06:06</div>
66-
<div class="created">Created by someone 2016-06-06 06:06:06</div>
62+
<div class="modified">Last modified by someone 2016-06-06 06:06:06</div>
63+
<div class="created">Created by someone 2016-06-06 06:06:06</div>
6764
</div>
6865
```
6966

@@ -73,15 +70,15 @@ content
7370

7471
default: `bottom`
7572

76-
git-author content position in the article. `top` or `bottom`
73+
git-author content position in the article. `top` or `bottom`
7774

7875
this will add a `git-author-{position}` className to `git-author-container`
7976

80-
### `createTpl` `modifyTpl`
77+
### `createTpl` `modifyTpl`
8178

8279
You can use `{user}` `{timeStamp}` as placeholder for username and timeStamp
8380

84-
default:
81+
default:
8582

8683
createTpl: `Created by {user} {timeStamp}`
8784

index.js

-97
This file was deleted.

package.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "gitbook-plugin-git-author",
3-
"description": "show git author for each article of gitbook",
4-
"main": "index.js",
53
"version": "1.1.3",
4+
"description": "A gitbook plugin to show git author info",
5+
"main": "src/index.js",
66
"author": "L3au <leshu.lau@gmail.com>",
77
"engines": {
88
"node": ">4.0.0",
@@ -39,15 +39,15 @@
3939
"url": "https://github.com/L3au/gitbook-plugin-git-author/issues"
4040
},
4141
"dependencies": {
42-
"execa": "^0.6.3",
43-
"moment": "^2.18.1"
42+
"execa": "^1.0.0",
43+
"moment": "^2.24.0"
4444
},
4545
"devDependencies": {
46-
"ava": "^0.19.1",
47-
"husky": "^0.13.3",
48-
"nyc": "^10.2.0",
49-
"sinon": "^2.1.0",
50-
"xo": "^0.18.1"
46+
"ava": "^2.0.0",
47+
"husky": "^2.4.0",
48+
"nyc": "^14.1.1",
49+
"sinon": "^7.3.2",
50+
"xo": "^0.24.0"
5151
},
5252
"xo": {
5353
"space": true,
File renamed without changes.
File renamed without changes.

src/index.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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, (match, name) => {
8+
if (match.charAt(0) === '\\') {
9+
return match.slice(1)
10+
}
11+
12+
return o[name] === undefined ? '' : o[name]
13+
})
14+
}
15+
16+
module.exports = {
17+
website: {
18+
assets: './assets',
19+
css: ['style.css']
20+
},
21+
22+
filters: {
23+
timeFormat(time, format) {
24+
return moment(time).format(format || moment.defaultFormat)
25+
}
26+
},
27+
28+
hooks: {
29+
page(page) {
30+
const defaults = {
31+
position: 'bottom',
32+
createTpl: 'Created by {user} at {timeStamp}',
33+
modifyTpl: 'Last modified by {user} at {timeStamp}',
34+
timeStampFormat: 'YYYY-MM-DD HH:mm:ss'
35+
}
36+
const bookRoot = this.resolve('')
37+
const pluginConfig = this.config.get('pluginsConfig')['git-author']
38+
const options = Object.assign({}, defaults, pluginConfig)
39+
40+
const {position} = options
41+
const {createTpl} = options
42+
const {modifyTpl} = options
43+
const {timeStampFormat} = options
44+
45+
return execa
46+
.shell(`git log --format="%an|%at" -- "${page.rawPath}"`, {
47+
cwd: bookRoot
48+
})
49+
.then(ret => {
50+
// None commit to this file
51+
if (!ret.stdout) {
52+
this.log.debug(`none commit to file ${page.path}`)
53+
return page
54+
}
55+
56+
const commits = ret.stdout.split(/\r?\n/).map(log => {
57+
const arr = log.split('|')
58+
const timeStamp = moment(arr[1] * 1000).format(timeStampFormat)
59+
60+
return {
61+
user: arr[0],
62+
timestamp: timeStamp,
63+
timeStamp
64+
}
65+
})
66+
67+
const lastCommit = commits[0]
68+
const firstCommit = commits.slice(-1)[0]
69+
70+
let gitAuthorContent = `<div class="git-author-container git-author-${position}">`
71+
72+
if (modifyTpl) {
73+
const modifyMsg = substitute(modifyTpl, lastCommit)
74+
gitAuthorContent += `<div class="modified">${modifyMsg}</div>`
75+
}
76+
77+
if (createTpl) {
78+
const createMsg = substitute(createTpl, firstCommit)
79+
gitAuthorContent += `<div class="created">${createMsg}</div>`
80+
}
81+
82+
gitAuthorContent += '</div>'
83+
84+
if (position === 'top') {
85+
page.content = gitAuthorContent + page.content
86+
} else {
87+
page.content += gitAuthorContent
88+
}
89+
90+
return page
91+
})
92+
.catch(error => {
93+
this.log.warn('initialize git repository and commit files firstly')
94+
this.log.warn(error)
95+
return page
96+
})
97+
}
98+
}
99+
}

test.js renamed to test/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const test = require('ava')
44
const execa = require('execa')
55
const sinon = require('sinon')
6-
const plugin = require('./')
6+
const plugin = require('../src')
77

88
test('gitbook plugin structure', t => {
99
t.deepEqual(Object.keys(plugin), ['website', 'filters', 'hooks'])

0 commit comments

Comments
 (0)