add contributor list to each document

This commit is contained in:
Fienny Angelina 2018-09-25 19:18:42 +08:00
parent 9b6ec5b9bb
commit c0cc79f995
3 changed files with 53 additions and 6 deletions

View file

@ -1,6 +1,7 @@
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const {getSubFolder, idx, parse} = require('../utils'); const {getSubFolder, idx, parse} = require('../utils');
const execSync = require("child_process").execSync;
function getLanguage(filepath, refDir, env) { function getLanguage(filepath, refDir, env) {
const translationEnabled = idx(env, ['translation', 'enabled']); const translationEnabled = idx(env, ['translation', 'enabled']);
@ -60,6 +61,32 @@ module.exports = async function processMetadata(
metadata.title = metadata.id; metadata.title = metadata.id;
} }
/* set metadata author */
const authorRegex = /(\d+) author (.+)$/g;
const results = execSync(
`git blame --line-porcelain ${filepath} \
| grep -I "^author " | sort | uniq -c | sort -nr; \
`
).toString().split('\n');
let authorData;
const authors = [];
let totalLineCount = 0;
results.forEach(result => {
if ((authorData = authorRegex.exec(result)) !== null) {
const lineCount = parseInt(authorData[1]);
const name = authorData[2];
authors.push({
lineCount,
name,
});
totalLineCount += lineCount;
}
authorRegex.lastIndex = 0;
});
metadata.authors = authors;
metadata.totalLineCount = totalLineCount;
/* language */ /* language */
const language = getLanguage(filepath, refDir, env); const language = getLanguage(filepath, refDir, env);
metadata.language = language; metadata.language = language;
@ -77,7 +104,7 @@ module.exports = async function processMetadata(
const versionPart = const versionPart =
(version && version !== latestVersion && `${version}/`) || ''; (version && version !== latestVersion && `${version}/`) || '';
/* /*
Convert temporarily metadata.id to the form of dirname/id without version/lang prefix Convert temporarily metadata.id to the form of dirname/id without version/lang prefix
ex: file `versioned_docs/version-1.0.0/en/foo/bar.md` with id `version-1.0.0-bar` => `foo/bar` ex: file `versioned_docs/version-1.0.0/en/foo/bar.md` with id `version-1.0.0-bar` => `foo/bar`
*/ */
@ -105,16 +132,16 @@ module.exports = async function processMetadata(
} }
} }
/* /*
The docs absolute file source The docs absolute file source
e.g: `/end/docs/hello.md` or `/end/website/versioned_docs/version-1.0.0/hello.md` e.g: `/end/docs/hello.md` or `/end/website/versioned_docs/version-1.0.0/hello.md`
*/ */
metadata.source = path.join(refDir, source); metadata.source = path.join(refDir, source);
/* Build the permalink */ /* Build the permalink */
const {baseUrl, docsUrl} = siteConfig; const {baseUrl, docsUrl} = siteConfig;
/* /*
if user has own custom permalink defined in frontmatter if user has own custom permalink defined in frontmatter
e.g: :baseUrl:docsUrl/:langPart/:versionPart/endiliey/:id e.g: :baseUrl:docsUrl/:langPart/:versionPart/endiliey/:id
*/ */

View file

@ -73,7 +73,23 @@ export default class Docs extends React.Component {
</Link> </Link>
)} )}
</div> </div>
<div className={styles.mainContainer}>{this.props.children}</div> <div className={styles.mainContainer}>
{this.props.children}
{metadata &&
metadata.authors &&
metadata.totalLineCount && (
<div className={styles.authorList}>
{metadata.authors
.map(({name, lineCount}) => {
const contribution =
((lineCount / metadata.totalLineCount) * 100).toFixed(2) +
'%';
return `${name} (${contribution})`;
})
.join(', ')}
</div>
)}
</div>
</Layout> </Layout>
); );
} }

View file

@ -7,4 +7,8 @@
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
justify-content: center; justify-content: center;
} }
.authorList {
min-width: 100%;
text-align: center;
}