const yaml = require("js-yaml"); import double from "./addons/double.js"; //TODO add systemized plugin support import rehypeDocument from "rehype-document"; import rehypeFormat from "rehype-format"; import rehypeParse from "rehype-parse"; import rehypeRaw from "rehype-raw"; import rehypeStringify from "rehype-stringify"; import rehypeWrap from "rehype-wrap"; import remarkFrontmatter from "remark-frontmatter"; import remarkParse from "remark-parse"; import remarkRehype from "remark-rehype"; import { readFile } from "fs-extra"; import { unified } from "unified"; import insertJSX from "./insertJSX.js"; import home from "./input/templates/home.jsx"; export async function writePage(filepath) { /* markdown --> MDAST --> HAST --> html |-> YAML --> {OBJ} - - - - - -> |-> {OBJ, main:html} */ let page; const processor = await unified() .use(remarkParse) .use(remarkFrontmatter, "yaml") .use(() => (tree) => { page = yaml.load(tree.children[0].value, { schema: yaml.FAILSAFE_SCHEMA, }); page.filename = filepath.split("/").pop().split(".").shift(); }) // .data(page) .use(double) .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) .use(rehypeWrap, { wrapper: "main" }) /* * TODO before/after * TODO pass data */ .use(insertJSX, "h2") .use(rehypeFormat) .use(rehypeStringify) //HACK should return HAST .process(await readFile(filepath, "utf-8")); page.main = processor.value; return page.main; } /* component maker/joiner NOTE I could move this into a plugin */ /* let recipe = ["before", "header", "main", "footer", "after"]; //TODO remove header/footer, accept component arrays for (let i of [0, 1, 3, 4]) { if (!Object.keys(page).includes(recipe[i])) { recipe[i] = null; } } let union = ""; for (let i of recipe.filter((x) => !!x)) { if (i === "main") { union += page.main; } else { const fn = await import(`./input/templates/${page[i]}.jsx`); //FIX avoid strict filestruct past "input" union += unified().use(rehypeStringify).stringify(fn.default(page)); //FIX use tree-visit } } const processor2 = unified() //HACK merge trees via unist tree visit .use(rehypeParse, { fragment: true }) .use(rehypeDocument, { css: page.css ?? "style.css", title: page?.title ?? page?.filename ?? "title", link: [{ href: "favicon.png", rel: "icon", type: "image/png" }], }) .use(rehypeFormat) .use(rehypeStringify); const output = await processor2.process(union); return output.value; } */ console.log(await writePage("./input/content/cookie.md"));