feat(list): added some additional formatting for list

This commit is contained in:
Dylan R. E. Moonfire 2023-01-26 22:19:45 -06:00
parent 65c18ad226
commit 93c786eb23
5 changed files with 833 additions and 39 deletions

View file

@ -63,6 +63,21 @@ Options:
* `-y`, `--yaml`: Extract the data as YAML instead of JSON (default).
* `--content PROPERTY`: Extract the content as a property of the given name instead of just removing it. Defaults to not extracting the content.
### list
> Displays each file as a Markdown ordered list.
```sh
markdowny list *.markdown
```
Options:
* `-o`, `--output`: Direct the output to a file or (default) standard out with `-`.
* `-s`, `--trim-whitespace`: If included, then newlines between the fields will be collapsed down to a single space and the resulting string trimmed at both ends.
* `-t`, `--template`: The Handlebars template using the YAML metadata for each file in addition to a `_number` which is the one-based number of the file. The default value is `{{_number}}. {{{title}}}: {{{summary}}}`.
* Some additional conditional operations are allowed such as `ge`, `le`, `gt`, `ge`, `eq`, and `ne`. They can be used like `{{#if (lt _number 10)}}0{{/if}}`.
### sections
> Displays each file as a heading 1 section with the title and contents provided.

782
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -38,6 +38,7 @@
},
"dependencies": {
"add-commas": "0.0.4",
"handlebars": "^4.7.7",
"lodash": "^4.16.5",
"markdown-table": "^1.0.0",
"read-pkg-up": "^4.0.0",

View file

@ -1,19 +1,21 @@
import * as _ from "lodash";
import * as yargs from "yargs";
import * as scanner from "../scanner";
import * as handlebars from "handlebars";
import * as fs from "fs";
export var command = "list";
export var describe = "Extracts a YAML field into an order list";
export var describe = "Extracts a YAML field into an ordered list";
export function builder(yargs: yargs.Arguments) {
return yargs
.help("help")
.alias("f", "field")
.default("field", "summary")
.alias("t", "template")
.default("template", "{{_number}}. {{{title}}}: {{{summary}}}")
.alias("t", "title")
.default("title", "title")
.alias("s", "trim-whitespace")
.boolean("trim-whitspace")
.alias("o", "output")
.default("output", "-")
@ -27,21 +29,54 @@ export function handler(argv: any) {
render(argv, data);
}
export function render(argv, data) {
for (var key in data) {
var number = 1 + parseInt(key.toString());
var item = data[key];
var title = _.get(item, argv.title);
var value = _.get(item, argv.field);
export function render(argv, files) {
// Compile the handlebars template.
handlebars.registerHelper("eq", function (a, b) {
return a === b;
});
handlebars.registerHelper("gt", function (a, b) {
return a > b;
});
handlebars.registerHelper("gte", function (a, b) {
return a >= b;
});
handlebars.registerHelper("lt", function (a, b) {
return a < b;
});
handlebars.registerHelper("lte", function (a, b) {
return a <= b;
});
handlebars.registerHelper("ne", function (a, b) {
return a !== b;
});
// Build up the parts so we can do this as a single line.
const parts = [`${number}. `, title];
const template = handlebars.compile(argv.template);
if (value) {
parts.push(": " + value.replace(/\n/g, " "));
// Go through each of the files and create a row from each one.
const trim = /\s+/g;
let output: string[] = [];
for (var key in files) {
// Combine everything into a single parameters object including `_number`
// to represent the one-based index from the beginning.
const number = 1 + parseInt(key.toString());
let params = { ...files[key], _number: number };
// Render the results and write it out.
let result = template(params);
if (argv.trimWhitespace) {
result = result.replace(trim, " ").trim();
}
// Write out the line.
console.log(parts.join(""));
output.push(result);
}
// Write out the results as requested.
if (argv.output === "-") {
console.log(output.join("\n"));
} else {
fs.writeFileSync(argv.output, Buffer.from(output.join("\n"), "utf-8"));
}
}

View file

@ -190,8 +190,3 @@ function parse(argv, titles, fields): Column[] {
return columns;
}
/* Notes
Comma-separated list of lists. I like spaces.
*/