@@ -3,6 +3,7 @@ const fs = require('fs')
33const PATH = require ( 'path' )
44const YAML = require ( 'yamljs' )
55const { execSync } = require ( 'child_process' )
6+ const { replaceFileContent } = require ( 'crd-utils' )
67const { getDigitFromDir, timeFormat } = require ( '../utils' )
78
89const constants = {
@@ -37,14 +38,14 @@ function safeReadDirSync(path) {
3738 return dirData
3839}
3940
40- /**
41- * build directory Tree, fork from https://github.com/mihneadb/node-directory-tree
41+ /** build directory Tree, fork from https://github.com/mihneadb/node-directory-tree
4242 * path: path for file
4343 * options: {
4444 * exclude: RegExp|RegExp[] - A RegExp or an array of RegExp to test for exclusion of directories.
4545 * extensions : RegExp - A RegExp to test for exclusion of files with the matching extension.
46- * mdconf: boolean
47- * prerender: boolean use for prerender
46+ * mdconf: Boolean.
47+ * prerender: Boolean. Used for prerender.
48+ * generate: Boolean. Used for generating info in front-matter.
4849 * }
4950 */
5051function directoryTree ( {
@@ -75,37 +76,41 @@ function directoryTree({
7576 if ( options && options . mdconf ) {
7677 item . type = constants . FILE
7778 const contentStr = fs . readFileSync ( path ) . toString ( )
78- if ( contentStr ) {
79- const contentMatch = contentStr . match ( / ^ < ! - - ( [ ^ > ] * ) - - > / )
80- item . mdconf = contentMatch ? YAML . parse ( contentMatch [ 1 ] ) : { }
81- try {
82- // see https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository/2390382#2390382
83- const result = execSync ( `git log --format=%aD ${ path } | tail -1` )
84- item . birthtime =
85- Buffer . isBuffer ( result ) && timeFormat ( new Date ( result ) )
86- } catch ( error ) {
87- // eslint-disable-next-line no-console
88- console . log ( `error: ${ error . message } ` )
89- }
90- try {
91- // see https://stackoverflow.com/questions/22497597/get-the-last-modification-data-of-a-file-in-git-repo
92- const result = execSync ( `git log -1 --pretty="format:%ci" ${ path } ` )
93- item . mtime = Buffer . isBuffer ( result ) && timeFormat ( new Date ( result ) )
94- } catch ( error ) {
95- // eslint-disable-next-line no-console
96- console . log ( `error: ${ error . message } ` )
97- }
98- item . size = stats . size // File size in bytes
99- item . extension = ext
100- if ( ! options . prerender ) {
101- item . relative = item . path . replace ( process . cwd ( ) , '' )
102- item . isEmpty = contentMatch
103- ? ! String . prototype . trim . call ( contentStr . replace ( contentMatch [ 0 ] , '' ) )
104- : true
105- const uglifyContent = contentStr . replace ( / \s / g, '' )
106- item . content = uglifyContent
79+ if ( ! contentStr ) return
80+ const contentMatch = contentStr . match ( / ^ < ! - - ( [ ^ > ] * ) - - > / )
81+ if ( options . generate ) {
82+ if ( contentMatch && contentMatch [ 1 ] . indexOf ( 'abbrlink' ) === - 1 ) {
83+ replaceFileContent ( path , contentMatch [ 1 ] , `\nabbrlink: 290a4219${ contentMatch [ 1 ] } ` )
84+ console . log ( '✅ replaceFileContent success' )
10785 }
10886 }
87+
88+ item . mdconf = contentMatch ? YAML . parse ( contentMatch [ 1 ] ) : { }
89+ try {
90+ // see https://stackoverflow.com/questions/2390199/finding-the-date-time-a-file-was-first-added-to-a-git-repository/2390382#2390382
91+ const result = execSync ( `git log --format=%aD ${ path } | tail -1` )
92+ item . birthtime =
93+ Buffer . isBuffer ( result ) && timeFormat ( new Date ( result ) )
94+ } catch ( error ) {
95+ console . log ( `❎ error: ${ error . message } ` )
96+ }
97+ try {
98+ // see https://stackoverflow.com/questions/22497597/get-the-last-modification-data-of-a-file-in-git-repo
99+ const result = execSync ( `git log -1 --pretty="format:%ci" ${ path } ` )
100+ item . mtime = Buffer . isBuffer ( result ) && timeFormat ( new Date ( result ) )
101+ } catch ( error ) {
102+ console . log ( `❎ error: ${ error . message } ` )
103+ }
104+ item . size = stats . size // File size in bytes
105+ item . extension = ext
106+ if ( ! options . prerender ) {
107+ item . relative = item . path . replace ( process . cwd ( ) , '' )
108+ item . isEmpty = contentMatch
109+ ? ! String . prototype . trim . call ( contentStr . replace ( contentMatch [ 0 ] , '' ) )
110+ : true
111+ const uglifyContent = contentStr . replace ( / \s / g, '' )
112+ item . content = uglifyContent
113+ }
109114 }
110115 } else if ( stats . isDirectory ( ) ) {
111116 const dirData = safeReadDirSync ( path )
0 commit comments