nazo6 notememo

remarkのプラグイン例

作成:2023/07/03

更新:2023/07/03

remarkプラグイン用テンプレート
import { Plugin, unified } from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import remarkFrontmatter from "remark-frontmatter";
import remarkParseFrontmatter from "remark-parse-frontmatter";
import { visit } from "unist-util-visit";
import { Root } from "mdast";
import * as fs from "fs/promises";

const remarkPlugin: Plugin<[], Root> = () => {
return (tree) => {
visit(tree, (node) => {
console.log(node);
});
};
};

main();

async function main() {
const file = await unified()
.use(remarkParse)
.use(remarkPlugin)
.use(remarkStringify)
.use(remarkFrontmatter, ["yaml", "toml"])
.use(remarkParseFrontmatter)
.process(await fs.readFile("./home.md", { encoding: "utf8" }));

console.log(String(file));
}