-
Notifications
You must be signed in to change notification settings - Fork 1
/
remark-format.js
executable file
·68 lines (65 loc) · 2.16 KB
/
remark-format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env -S deno run --allow-read --allow-write
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { remark } from "npm:remark";
import remarkToc from "npm:remark-toc";
import remarkFrontmatter from "npm:remark-frontmatter";
import remarkStringify from "npm:remark-stringify";
import remarkGfm from "npm:remark-gfm";
import { read, write } from "npm:to-vfile";
function main(parsedArgs) {
const filenames = parsedArgs["_"] || [];
const maxDepth = Number(parsedArgs["maxdepth"]) || 6;
const heading = parsedArgs["heading"] ||
"toc|table[ -]of[ -]contents?|contents|agenda";
const promises = [];
for (const i in filenames) {
promises.push(
read(filenames[i])
.then((data) =>
// See https://github.com/remarkjs/remark
remark()
.use(remarkToc, {
// See https://github.com/remarkjs/remark-toc
heading,
maxDepth,
ordered: true,
tight: true,
})
.use(remarkStringify, {
// See https://github.com/remarkjs/remark/tree/main/packages/remark-stringify
// INFO: make the format as close as possible to `deno fmt`
bullet: "-",
bulletOther: "*",
listItemIndent: "one",
emphasis: "_",
rule: "-",
strong: "*",
tightDefinitions: true,
fences: false,
})
.use(
// See https://github.com/remarkjs/remark-frontmatter
// Support for yaml frontmatter
remarkFrontmatter,
["yaml", "toml"],
)
.use(
// See https://github.com/remarkjs/remark-gfm
// Support for github markdown
remarkGfm
)
.process(data)
)
.then(write).then((res) => console.log("formatted", res.history[0])),
);
}
return Promise.all(promises);
}
const args = parse(Deno.args);
if (args["h"] || args["help"]) {
console.log(
"Usage:\nremark-format.js [--maxdepth=Number|--heading=TocHeading] [FILENAMES]",
);
} else {
await main(args);
}