Last active
January 26, 2020 17:47
-
-
Save artisonian/a7ff4923d6b8af729264ea52a5737e4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { parse } from "https://deno.land/[email protected]/flags/mod.ts"; | |
import { EOL } from "https://deno.land/[email protected]/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