Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add last contributor to each document #980

Merged
merged 17 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions v2/lib/load/docs/metadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs-extra');
const path = require('path');
const {getSubFolder, idx, parse} = require('../utils');
const execSync = require("child_process").execSync;

function getLanguage(filepath, refDir, env) {
const translationEnabled = idx(env, ['translation', 'enabled']);
Expand Down Expand Up @@ -60,6 +61,35 @@ module.exports = async function processMetadata(
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');
/* handle case where it's not github repo */
if (results.length && results[0].length) {
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 */
const language = getLanguage(filepath, refDir, env);
metadata.language = language;
Expand All @@ -77,7 +107,7 @@ module.exports = async function processMetadata(
const versionPart =
(version && version !== latestVersion && `${version}/`) || '';

/*
/*
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`
*/
Expand Down Expand Up @@ -105,16 +135,16 @@ module.exports = async function processMetadata(
}
}

/*
/*
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);

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

/*
/*
if user has own custom permalink defined in frontmatter
e.g: :baseUrl:docsUrl/:langPart/:versionPart/endiliey/:id
*/
Expand Down
18 changes: 17 additions & 1 deletion v2/lib/theme/Docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,23 @@ export default class Docs extends React.Component {
</Link>
)}
</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>
);
}
Expand Down
6 changes: 5 additions & 1 deletion v2/lib/theme/Docs/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
margin-left: auto;
margin-right: auto;
justify-content: center;
}
}
.authorList {
min-width: 100%;
text-align: center;
}