#!/usr/bin/env bun import double from "./addons/double.js"; //TODO add systemized plugin support import insertJSX from "./insertJSX.js"; import rehypeDocument from "rehype-document"; import rehypeFormat from "rehype-format"; import rehypePresetMinify from "rehype-preset-minify"; import rehypeRaw from "rehype-raw"; import rehypeStringify from "rehype-stringify"; import rehypeWrap from "rehype-wrap"; import remarkParse from "remark-parse"; import remarkRehype from "remark-rehype"; import { matter } from "vfile-matter"; import { read, write } from "to-vfile"; import { unified } from "unified"; import { emptyDir } from "fs-extra"; import cpy from "cpy"; import { glob } from "glob"; import { resolve } from "path"; // Resolve input/putput directories respecting a project's root folder. const inputDir = resolve(process.cwd(), "input"); const outputDir = resolve(process.cwd(), "out"); async function createSiteIndex() { let site = await glob(`${inputDir}/**/*.md`, { nodir: true }); return Promise.all( site.map(async (page) => { let file = await read(page, "utf-8"); matter(file); file.data.matter.filename = file.stem; return file.data.matter; }), ); } // mise en place emptyDir(`${outputDir}`, { recursive: true }); // clears the output folder await cpy([`${inputDir}/**`, `!${inputDir}/**/*.md`], `${outputDir}`, { flat: true, }); //copies all assets into out/ const siteIndex = await createSiteIndex(); // main function export async function kushiyaki(filepath) { const file = await read(filepath, "utf-8"); matter(file, { strip: true }); file.data.matter.filename = file.stem; const processor = unified() .data("siteIndex", siteIndex) .use(remarkParse) .use(double) .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) .use(rehypeWrap, { wrapper: "main" }) .use(insertJSX, file.data.matter) .use(rehypeDocument, { css: file.data.matter?.css ?? "style.css", title: file.data.matter?.title ?? "untitled", link: [{ href: "favicon.png", rel: "icon", type: "image/png" }], //FIX make filetype-agnostic }) // .use(rehypePresetMinify) //NOTE mainly using this for easier tree traversal .use(rehypeFormat) .use(rehypeStringify); const output = await processor.process(file); output.extname = ".html"; output.dirname = outputDir; return output; } // Loop through function for each page for (const file of await glob(`${inputDir}/**/*.md`, { nodir: true })) { await write(await kushiyaki(file)); }