-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
33 lines (27 loc) · 864 Bytes
/
index.ts
File metadata and controls
33 lines (27 loc) · 864 Bytes
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
import { createRequire } from "module";
import type * as m from "./types.js";
export * from "./types.js";
const require = createRequire(import.meta.url);
const mmd = require("./build/napi/Release/mmd-js.node") as typeof m;
export const parse = (input: Buffer): m.Token => mmd.parse(input);
export const ready = () => Promise.resolve();
export const read = (input: Buffer, token: m.Token) =>
input.subarray(token.start, token.start + token.len).toString();
export const readTitle = (input: Buffer, token: m.Token) =>
read(input, token)
.replace(/^\s*#+\s*/, "")
.replace(/\s*#+\s*$/, "");
export const walk = (
token: m.Token,
fn: (token: m.Token) => void | { stopWalking: boolean }
) => {
if (fn(token)?.stopWalking) {
return;
}
if (token.child) {
walk(token.child, fn);
}
if (token.next) {
walk(token.next, fn);
}
};