Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve flexibility in the way ADRs are acquired and indexed #35

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 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'],
string: ['p'],
alias: {h: 'help'}
});
Expand All @@ -40,6 +41,7 @@ if (!args.d && !args.i && !args.p && (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>',
' -p: Path prefix for each ADR file path written in log',
' (Default is empty)',
'',
Expand Down Expand Up @@ -67,9 +69,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';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a change in behaviour that causes all md files to be indexed, not just those with the prefix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relates to the discussion at adr/madr#28

Another thing: Could you try log4brain's adr command? I am short of time currently, but the project seems promising. I am thinking of stopping this project and to recommend that one. WDYT?

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 @@ -55,32 +85,32 @@ function generate(options) {
};
}

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()}](${options.pathPrefix}${tokenPath}) - ${title + options.newline}`
res.content += `* [ADR-${index.trim()}](${options.pathPrefix}${tokenPath}) - ${title + options.newline}`
}
res.content = res.content.trim();
return res;
Expand Down