Skip to content

Commit

Permalink
Merge pull request #35 from wyrfel/feature/improve-adr-file-gathering
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor authored Oct 21, 2020
2 parents bd2f93e + b48989f commit 07b7acf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
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';
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

0 comments on commit 07b7acf

Please sign in to comment.