feat: load JSX via unified plugin
trickyni trickyniv56@gmail.com
Sat, 09 May 2026 11:29:50 +0300
3 files changed,
35 insertions(+),
16 deletions(-)
A
insertJSX.js
@@ -0,0 +1,8 @@
+import { visit } from "unist-util-visit"; +import { select } from "hast-util-select"; +export default function insertJSX(componentName, data = {}) { + return async (tree) => { + const fn = await import(`./input/templates/${componentName}.jsx`); //FIX avoid strict filestruct past "input" + tree.children.unshift(fn.default(data)); + }; +}
D
writeComponent.js
@@ -1,7 +0,0 @@
-import { unified } from "unified"; -import rehypeStringify from "rehype-stringify"; - -export async function writeComponent(componentName, data = {}) { - const fn = await import(`./input/templates/${componentName}.jsx`); //FIX avoid strict filestruct past "input" - return unified().use(rehypeStringify).stringify(fn.default(data)); -}
M
writePage.js
→
writePage.js
@@ -1,5 +1,5 @@
const yaml = require("js-yaml"); -import double from "./addons/double.js"; //NOTE delete from release +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";@@ -11,9 +11,14 @@ import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype"; import { readFile } from "fs-extra"; import { unified } from "unified"; -import { writeComponent } from "./writeComponent.js"; +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)@@ -24,35 +29,46 @@ 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) + .use(rehypeStringify) //HACK should return HAST .process(await readFile(filepath, "utf-8")); page.main = processor.value; - - // console.log(page); + return page.main; +} +/* + component maker/joiner + NOTE I could move this into a plugin + */ - let recipe = ["before", "header", "main", "footer", "after"]; +/* + 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 = ""; + 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)); + union += unified().use(rehypeStringify).stringify(fn.default(page)); //FIX use tree-visit } } - const processor2 = unified() + const processor2 = unified() //HACK merge trees via unist tree visit .use(rehypeParse, { fragment: true }) .use(rehypeDocument, { css: page.css ?? "style.css",@@ -65,3 +81,5 @@
const output = await processor2.process(union); return output.value; } +*/ +console.log(await writePage("./input/content/cookie.md"));