11const fs = require ( 'fs-extra' ) ;
22const path = require ( 'path' ) ;
33const { getSubFolder, idx, parse} = require ( '../utils' ) ;
4+ const execSync = require ( "child_process" ) . execSync ;
45
56function 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 + ) a u t h o r ( .+ ) $ / 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 */
0 commit comments