From d3eb21e7bf9a78e0fff7f16777253c6144cd6c66 Mon Sep 17 00:00:00 2001 From: "Dylan R. E. Moonfire" Date: Thu, 27 Aug 2020 21:17:16 -0500 Subject: [PATCH] feat(content): added a new command to extract content from a file --- README.md | 8 ++++++++ src/tools/content.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/tools/content.ts diff --git a/README.md b/README.md index c8a40c1..e5f6861 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,14 @@ markdown verb *.markdown markdowny version ``` +### content + +> Extract the content from one or more files. + +```sh +markdowny content *.markdown +``` + ### count > Counts the number of words excluding the YAML header and displays it. diff --git a/src/tools/content.ts b/src/tools/content.ts new file mode 100644 index 0000000..6393949 --- /dev/null +++ b/src/tools/content.ts @@ -0,0 +1,40 @@ +import * as _ from "lodash"; +import * as fs from "fs"; +import * as scanner from "../scanner"; +import * as yargs from "yargs"; + +export var command = "content"; +export var describe = "Extracts the content of the file without metadata"; + +export function builder(yargs: yargs.Arguments) +{ + return yargs + .help("help") + + .option( + "output", + { + alias: "o", + default: "-", + describe: "Write output to a file or `-` for standard out", + } + ) + + .demand(1); +} + +export function handler(argv: any) +{ + // Parse through the files and retrieve the metadata. + var files = argv._.splice(1); + var data: any = scanner.scanFiles(argv, files); + var output = data.map(x => x.__content).join("\n"); + + // Figure out where to write. + if (argv.output === "-") + { + console.log(output); + } else { + fs.writeFileSync(argv.output, Buffer.from(output, "utf-8")); + } +}