From c0cc79f9955dd1cd0aa46f88a4b2c49745fcf2c2 Mon Sep 17 00:00:00 2001 From: Fienny Angelina Date: Tue, 25 Sep 2018 19:18:42 +0800 Subject: [PATCH 01/15] add contributor list to each document --- v2/lib/load/docs/metadata.js | 35 +++++++++++++++++++++++++++++++---- v2/lib/theme/Docs/index.js | 18 +++++++++++++++++- v2/lib/theme/Docs/styles.css | 6 +++++- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/v2/lib/load/docs/metadata.js b/v2/lib/load/docs/metadata.js index 4f147743c548..44d2fdb8d04e 100644 --- a/v2/lib/load/docs/metadata.js +++ b/v2/lib/load/docs/metadata.js @@ -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']); @@ -60,6 +61,32 @@ 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'); + 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; @@ -77,7 +104,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` */ @@ -105,16 +132,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 */ diff --git a/v2/lib/theme/Docs/index.js b/v2/lib/theme/Docs/index.js index 3aff930ce506..733eb26ff869 100644 --- a/v2/lib/theme/Docs/index.js +++ b/v2/lib/theme/Docs/index.js @@ -73,7 +73,23 @@ export default class Docs extends React.Component { )} -
{this.props.children}
+
+ {this.props.children} + {metadata && + metadata.authors && + metadata.totalLineCount && ( +
+ {metadata.authors + .map(({name, lineCount}) => { + const contribution = + ((lineCount / metadata.totalLineCount) * 100).toFixed(2) + + '%'; + return `${name} (${contribution})`; + }) + .join(', ')} +
+ )} +
); } diff --git a/v2/lib/theme/Docs/styles.css b/v2/lib/theme/Docs/styles.css index 52f2cff1653c..d5c2782850b6 100644 --- a/v2/lib/theme/Docs/styles.css +++ b/v2/lib/theme/Docs/styles.css @@ -7,4 +7,8 @@ margin-left: auto; margin-right: auto; justify-content: center; -} \ No newline at end of file +} +.authorList { + min-width: 100%; + text-align: center; +} From a76a8879018769b1736bd3aad13ee7881fc44b8e Mon Sep 17 00:00:00 2001 From: Fienny Angelina Date: Tue, 25 Sep 2018 20:11:30 +0800 Subject: [PATCH 02/15] handle case where there is no github repo --- v2/lib/load/docs/metadata.js | 37 +++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/v2/lib/load/docs/metadata.js b/v2/lib/load/docs/metadata.js index 44d2fdb8d04e..4783b48c10a2 100644 --- a/v2/lib/load/docs/metadata.js +++ b/v2/lib/load/docs/metadata.js @@ -68,24 +68,27 @@ module.exports = async function processMetadata( | 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; - }); + /* 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; + metadata.authors = authors; + metadata.totalLineCount = totalLineCount; + } /* language */ const language = getLanguage(filepath, refDir, env); From 370f39837587dda238871c01398e34a52761a634 Mon Sep 17 00:00:00 2001 From: Fienny Angelina Date: Sat, 29 Sep 2018 10:35:15 +0800 Subject: [PATCH 03/15] Move to v1 --- v1/lib/core/DocsLayout.js | 16 +++++++++++++--- v1/lib/core/utils.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/v1/lib/core/DocsLayout.js b/v1/lib/core/DocsLayout.js index 9fc259935a77..b64df08209a5 100644 --- a/v1/lib/core/DocsLayout.js +++ b/v1/lib/core/DocsLayout.js @@ -17,7 +17,7 @@ const OnPageNav = require('./nav/OnPageNav.js'); const Site = require('./Site.js'); const translation = require('../server/translation.js'); const docs = require('../server/docs.js'); -const {idx, getGitLastUpdated} = require('./utils.js'); +const {idx, getGitLastUpdated, getAuthorInformation } = require('./utils.js'); // component used to generate whole webpage for docs, including sidebar/header/footer class DocsLayout extends React.Component { @@ -45,11 +45,11 @@ class DocsLayout extends React.Component { DocComponent = this.props.Doc; } let updateTime; + const filepath = docs.getFilePath(metadata); if (this.props.config.enableUpdateTime) { - const filepath = docs.getFilePath(metadata); updateTime = getGitLastUpdated(filepath); } - + const { authors, totalLineCount } = getAuthorInformation(filepath); const title = idx(i18n, ['localized-strings', 'docs', id, 'title']) || defaultTitle; const hasOnPageNav = this.props.config.onPageNav === 'separate'; @@ -131,6 +131,16 @@ class DocsLayout extends React.Component { {updateTime}

)} + {authors.length && !!totalLineCount && + (

+ Written by: + {authors.map(({name, lineCount}) => { + const contribution = + ((lineCount / totalLineCount) * 100).toFixed(2); + return `${name} (${contribution}%)`; + }).join(', ')} +

) + } {hasOnPageNav && (