Skip to content

Commit

Permalink
Improve flexibility in the way ADRs are acquired and indexed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wyrfel committed Aug 12, 2020
1 parent f668565 commit f33cbf9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
11 changes: 10 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
});

Expand All @@ -39,6 +40,8 @@ if (!args.d && !args.i && (args._.length==0) || args.h) {
' -d: Scans the given <directory> for .md files.',
' (Without this flag, the current working directory is chosen as default.)',
'',
' -e Exclude any files matching the given <pattern>',
'',
' -h: Shows how to use this program',
''
].join(os.EOL));
Expand All @@ -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
Expand Down
42 changes: 36 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const fs = require('fs');
const path = require('path');
const slash = require('slash');

var counter = 1;

/**
* expose `toc`
*/
Expand All @@ -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.
Expand All @@ -53,32 +83,32 @@ 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();

let tokenPath = slash(
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;
Expand Down

0 comments on commit f33cbf9

Please sign in to comment.