feat(extract): added additional options to extract

- `--no-list` to not write the output as a list of objects
- `--yaml` to write the output as YAML
- `--content` to extract the content of a property of the given name
- Updated read me to include usage of extract
This commit is contained in:
Dylan R. E. Moonfire 2020-08-27 20:54:28 -05:00
parent 910ae7da23
commit a80aeb2f7f
4 changed files with 94 additions and 9 deletions

View file

@ -40,6 +40,20 @@ Options:
* `-t`, `--total`: Add a total line to the bottom that combines.
* `-s`, `--separator`: Comma-separates the numbers in the total.
### extract
> Extract the metadata (front matter) from one or more files.
```sh
markdowny extract *.markdown [options]
```
Options:
* `--no-list`: Don't write out the extract data as a list/sequence of items (default).
* `-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.
### sections
> Displays each file as a heading 1 section with the title and contents provided.

9
package-lock.json generated
View file

@ -1699,6 +1699,15 @@
"integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==",
"dev": true
},
"@types/yaml": {
"version": "1.9.7",
"resolved": "https://registry.npmjs.org/@types/yaml/-/yaml-1.9.7.tgz",
"integrity": "sha512-8WMXRDD1D+wCohjfslHDgICd2JtMATZU8CkhH8LVJqcJs6dyYj5TGptzP8wApbmEullGBSsCEzzap73DQ1HJaA==",
"dev": true,
"requires": {
"yaml": "*"
}
},
"@types/yargs": {
"version": "11.1.6",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-11.1.6.tgz",

View file

@ -54,6 +54,7 @@
"@semantic-release/npm": "^5.0.1",
"@types/lodash": "^4.14.116",
"@types/read-pkg-up": "^3.0.1",
"@types/yaml": "^1.9.7",
"@types/yargs": "^11.1.1",
"barrelsby": "^1.0.2",
"commitizen": "^2.10.1",

View file

@ -1,6 +1,8 @@
import * as _ from "lodash";
import * as yargs from "yargs";
import * as fs from "fs";
import * as scanner from "../scanner";
import * as yaml from "js-yaml";
import * as yargs from "yargs";
export var command = "extract";
export var describe = "Extracts a YAML from a list of files";
@ -10,20 +12,79 @@ export function builder(yargs: yargs.Arguments)
return yargs
.help("help")
.alias("o", "output")
.default("output", "-")
.option(
"list",
{
type: "boolean",
default: true,
describe: "Extract the data as a list of files, otherwise as a single file",
}
)
.option(
"output",
{
alias: "o",
default: "-",
describe: "Write output to a file or `-` for standard out",
}
)
.option(
"yaml",
{
alias: "y",
type: "boolean",
default: false,
describe: "Extract the output as YAML instead of JSON",
}
)
.option(
"content",
{
type: "string",
describe: "Include the content as a property in the extracted data",
}
)
.demand(1);
}
export function handler(argv: any)
{
// Parse through the files and retrieve the metadata.
var files = argv._.splice(1);
var data = scanner.scanFiles(argv, files);
render(argv, data);
}
var data: any = scanner.scanFiles(argv, files);
export function render(argv, data)
{
console.log(JSON.stringify(data));
// Figure out how to handle the content property.
if (!argv.content)
{
// Remove the content from the parsed file.
data.forEach(x => delete x.__content);
} else if (argv.content !== "__content")
{
// Rename the content property.
data.forEach(x => {
x[argv.content] = x.__content;
delete x.__content;
})
}
// Determine if we are displaying a list of items. If we want a list, then
// we wrap the data in another list (list of a list) so it writes out only
// one item. Otherwise, we write out each one.
const items = argv.list
? [data]
: data;
// Figure the output of the results (JSON or YAML).
const output = argv.yaml
? items.map((x: any) => yaml.safeDump(x)).join("\n")
: items.map((x: any) => JSON.stringify(x)).join("\n");
// Figure out where to write.
if (argv.output === "-")
{
console.log(output);
} else {
fs.writeFileSync(argv.output, Buffer.from(output, "utf-8"));
}
}