Skip to content

Instantly share code, notes, and snippets.

@artisonian
Last active Jan 26, 2020
Embed
What would you like to do?
import { parse } from "https://deno.land/std@v0.30.0/flags/mod.ts";
import { EOL } from "https://deno.land/std@v0.30.0/path/mod.ts";
import marked from "https://raw.githubusercontent.com/denolib/marked/master/main.ts";
const args = parse(Deno.args);
if (args.h || args.help) {
printUsage();
Deno.exit(0);
}
if (!args._.length) {
printUsage();
Deno.exit(1);
}
const decoder = new TextDecoder();
const md = decoder.decode(await Deno.readFile(args._[0]));
const tokens = marked.lexer(md);
const codeBlocks = [];
for (const token of tokens) {
if (token.type !== "code" || !token.lang) continue;
codeBlocks.push(token.text);
}
const src = codeBlocks.join(EOL + EOL);
const dest = args.o || args.outfile
if (dest) {
const encoder = new TextEncoder()
await Deno.writeFile(dest, encoder.encode(src))
} else {
console.log(src);
}
function printUsage() {
console.error(`Usage: erudite [options] path/to/filename
-h, --help show this help text
-o, --outfile write to the given file path (default: stdout)`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment