Skip to content

Commit c0cc79f

Browse files
committed
add contributor list to each document
1 parent 9b6ec5b commit c0cc79f

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

v2/lib/load/docs/metadata.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs-extra');
22
const path = require('path');
33
const {getSubFolder, idx, parse} = require('../utils');
4+
const execSync = require("child_process").execSync;
45

56
function getLanguage(filepath, refDir, env) {
67
const translationEnabled = idx(env, ['translation', 'enabled']);
@@ -60,6 +61,32 @@ module.exports = async function processMetadata(
6061
metadata.title = metadata.id;
6162
}
6263

64+
/* set metadata author */
65+
const authorRegex = /(\d+) author (.+)$/g;
66+
const results = execSync(
67+
`git blame --line-porcelain ${filepath} \
68+
| grep -I "^author " | sort | uniq -c | sort -nr; \
69+
`
70+
).toString().split('\n');
71+
let authorData;
72+
const authors = [];
73+
let totalLineCount = 0;
74+
results.forEach(result => {
75+
if ((authorData = authorRegex.exec(result)) !== null) {
76+
const lineCount = parseInt(authorData[1]);
77+
const name = authorData[2];
78+
authors.push({
79+
lineCount,
80+
name,
81+
});
82+
totalLineCount += lineCount;
83+
}
84+
authorRegex.lastIndex = 0;
85+
});
86+
87+
metadata.authors = authors;
88+
metadata.totalLineCount = totalLineCount;
89+
6390
/* language */
6491
const language = getLanguage(filepath, refDir, env);
6592
metadata.language = language;
@@ -77,7 +104,7 @@ module.exports = async function processMetadata(
77104
const versionPart =
78105
(version && version !== latestVersion && `${version}/`) || '';
79106

80-
/*
107+
/*
81108
Convert temporarily metadata.id to the form of dirname/id without version/lang prefix
82109
ex: file `versioned_docs/version-1.0.0/en/foo/bar.md` with id `version-1.0.0-bar` => `foo/bar`
83110
*/
@@ -105,16 +132,16 @@ module.exports = async function processMetadata(
105132
}
106133
}
107134

108-
/*
135+
/*
109136
The docs absolute file source
110-
e.g: `/end/docs/hello.md` or `/end/website/versioned_docs/version-1.0.0/hello.md`
137+
e.g: `/end/docs/hello.md` or `/end/website/versioned_docs/version-1.0.0/hello.md`
111138
*/
112139
metadata.source = path.join(refDir, source);
113140

114141
/* Build the permalink */
115142
const {baseUrl, docsUrl} = siteConfig;
116143

117-
/*
144+
/*
118145
if user has own custom permalink defined in frontmatter
119146
e.g: :baseUrl:docsUrl/:langPart/:versionPart/endiliey/:id
120147
*/

v2/lib/theme/Docs/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ export default class Docs extends React.Component {
7373
</Link>
7474
)}
7575
</div>
76-
<div className={styles.mainContainer}>{this.props.children}</div>
76+
<div className={styles.mainContainer}>
77+
{this.props.children}
78+
{metadata &&
79+
metadata.authors &&
80+
metadata.totalLineCount && (
81+
<div className={styles.authorList}>
82+
{metadata.authors
83+
.map(({name, lineCount}) => {
84+
const contribution =
85+
((lineCount / metadata.totalLineCount) * 100).toFixed(2) +
86+
'%';
87+
return `${name} (${contribution})`;
88+
})
89+
.join(', ')}
90+
</div>
91+
)}
92+
</div>
7793
</Layout>
7894
);
7995
}

v2/lib/theme/Docs/styles.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
margin-left: auto;
88
margin-right: auto;
99
justify-content: center;
10-
}
10+
}
11+
.authorList {
12+
min-width: 100%;
13+
text-align: center;
14+
}

0 commit comments

Comments
 (0)