From f33cbf91f02fbfe89342465d86a3821950d11a54 Mon Sep 17 00:00:00 2001 From: Andre Wyrwa Date: Thu, 13 Aug 2020 06:01:05 +1000 Subject: [PATCH] Improve flexibility in the way ADRs are acquired and indexed Improves the way ADRs are acquired and indexed in the following ways - search recursively underneath the given directory - allow date prefixes as well as number prefixes - allow specification of `index` or `date` properties in frontmatter - fallback to auto-numbering for ADRs without filename prefixes or frontmatter - add -e option to specify glob pattern for files to be excluded --- cli.js | 11 ++++++++++- index.js | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index 0788990..94b9ee0 100755 --- a/cli.js +++ b/cli.js @@ -21,6 +21,7 @@ var path = require('path'); var args = utils.minimist(process.argv.slice(2), { boolean: ['i'], string: ['d'], + string: ['e'], alias: {h: 'help'} }); @@ -39,6 +40,8 @@ if (!args.d && !args.i && (args._.length==0) || args.h) { ' -d: Scans the given for .md files.', ' (Without this flag, the current working directory is chosen as default.)', '', + ' -e Exclude any files matching the given ', + '', ' -h: Shows how to use this program', '' ].join(os.EOL)); @@ -62,9 +65,15 @@ console.log("adr log file:", adrLogFile); console.log("adr log dir:", adrLogDir); var headings = ''; -const globPattern = '!(' + adrLogFileName.slice(0,-3) + '*).md'; +const globPattern = '**/*.md'; console.log("glob pattern:", globPattern); var filenames = glob.sync(globPattern, {cwd: adrLogDir}); +filenames = filenames.filter(filename => filename !== adrLogFile.replace(adrLogDir + '/', '')); +if (args.e) { + var excluded = glob.sync(args.e, {cwd: adrLogDir}); + console.log("excluded: ", excluded); + filenames = filenames.filter(filename => !excluded.includes(filename)); +} console.log("filenames:", filenames); // determine log entries diff --git a/index.js b/index.js index 42c0dbb..4968d0b 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ const fs = require('fs'); const path = require('path'); const slash = require('slash'); +var counter = 1; + /** * expose `toc` */ @@ -33,6 +35,34 @@ function toc(str, options) { */ toc.insertAdrToc = require('./lib/insert').insertAdrToc; +/** + * extract index number or date from either filename or front matter + */ +function getIndex(basename, data) { + const numb = basename.match(/^[\d-_\\\/]*\d/m); + if (numb !== null && numb !== undefined) { + return numb[0]; + } + + if (data.index) { + return '' + data.index; + } + + if (data.date) { + let date = data.date; + + if (typeof(date) == 'object') { + date = date.getFullYear() + "-" + + ("0" + (date.getMonth() + 1)).substr(-2) + "-" + + ("0" + date.getDate()).substr(-2) + } + + return date; + } + + return '' + (counter++); +} + /** * Generate a markdown table of contents. This is the * function that does all of the main work with Remarkable. @@ -53,11 +83,6 @@ function generate(options) { return {content: ''}; } - const numb = token.content.match(/^\d+/m); - if (numb === null || numb === undefined) { - continue; - } - let contentPath = `${options.dir}/${token.content}` let content = fs.readFileSync(contentPath).toString(); @@ -65,20 +90,25 @@ function generate(options) { path.relative(options.tocDir, contentPath) ); + let data = {}; + // does the file have front-matter? if (/^---/.test(content)) { // extract it temporarily so the syntax // doesn't get mistaken for a heading let obj = utils.matter(content); + data = obj.data; content = obj.content; } + let index = getIndex(path.parse(token.content).base, data); + const newline = utils.determineNewline(content); let title = content.split(newline)[0].substr(2); console.log("title before decimal removal: ", title); title = title.replace(/^\d+\. /, ''); console.log("title after decimal removal: ", title); - res.content += `- [ADR-${numb[0].trim()}](${tokenPath}) - ${title + options.newline}` + res.content += `- [ADR-${index.trim()}](${tokenPath}) - ${title + options.newline}` } res.content = res.content.trim(); return res;