-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro-paper.ts
executable file
·127 lines (117 loc) · 3.56 KB
/
astro-paper.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env bun
const { ArgumentParser } = require("argparse");
import async from "async";
import { mkdir, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { inspect, isDeepStrictEqual } from "node:util";
import { read } from "to-vfile";
import { VFile } from "vfile";
import { matter } from "vfile-matter";
import yaml from "yaml";
import { Frontmatter_astro_paper, post_cleanup } from "./lib/astro_paper.js";
import { filelist } from "./lib/filelist";
import { normalize_frontmatter } from "./lib/normalize_frontmatter";
import { FileListItem } from "./lib/types";
const parser = new ArgumentParser({
description: "Batch clean up frontmatters in posts.",
});
parser.add_argument("-w", "--write", {
default: false,
action: "store_true",
help: "whether to write the cleaned up frontmatter back to the file",
});
parser.add_argument("-o", "--out", {
metavar: "FOLDER",
default: "./out",
help: "if `write` is specified, output a copy of files in `FOLDER`; set folder as `-` to overwrite the input files",
});
parser.add_argument("in", {
metavar: "INPUT",
help: "input file or folder",
});
let args = parser.parse_args();
console.log(inspect(args));
// process.exit(0);
const files = await filelist(args.in, {
filter: (name: string) => name.endsWith(".md") || name.endsWith(".mdx"),
});
// console.log(files);
async.mapLimit(
files,
10,
async (item: FileListItem) => {
// console.log(item);
return read(item.path)
.then((vfile) => {
// file reader
matter(vfile, { strip: true });
if (isDeepStrictEqual(vfile.data.matter, {})) {
vfile.data.skip = true;
}
// make a copy of the original frontmatter
vfile.data.orig = Object.assign({}, vfile.data.matter);
return vfile;
})
.then((vfile) => {
// transformer
if (vfile.data.skip) {
return vfile;
}
vfile.data.matter = normalize_frontmatter(
post_cleanup(vfile.data.matter as Frontmatter_astro_paper)
);
return vfile;
})
.then((vfile) => {
// debug printer
if (vfile.data.skip) {
return vfile;
}
const { orig, matter } = vfile.data;
console.log(`${inspect(orig)}\n=> ${inspect(matter)}`);
console.log("=======================");
return vfile;
})
.then(async (vfile) => {
// writer
if (vfile.data.skip || !args.write) {
return vfile;
}
// console.log(yaml.stringify(vfile.data.matter));
// return vfile;
// write vfile to disk
let out_path = vfile.path;
if (args.out != "-") {
await mkdir(args.out, { recursive: true });
out_path = resolve(args.out, vfile.basename!);
}
await writeFile(
out_path,
"---\n" +
yaml.stringify(vfile.data.matter) +
"---\n" +
vfile.toString()
);
return vfile;
})
.catch((err) => {
console.error(`file: ${item.path}`);
console.error(err);
process.exit(1);
});
},
(err, vfiles) => {
if (err) throw err;
// filter out nulls
vfiles = (vfiles as VFile[]).filter((vfile) => vfile && !vfile.data.skip);
if (args.write) {
console.log(
args.out != "-"
? `output folder: [${args.out}]`
: `output folder: (same as input)`
);
}
console.log(`files: ${files.length}, processed: ${vfiles.length}`);
// console.log(contents.map((vfile) => vfile!.data.name).sort());
}
);