Skip to content

Instantly share code, notes, and snippets.

@artisonian
Last active January 26, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artisonian/a7ff4923d6b8af729264ea52a5737e4b to your computer and use it in GitHub Desktop.
Save artisonian/a7ff4923d6b8af729264ea52a5737e4b to your computer and use it in GitHub Desktop.
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