all repos — kushiyaki @ d17890b7045c4b8bed81b5fcff23149d0d75f408

🍢 A tiny static site generator for grilling markdown files to perfection

index.js (view raw)

 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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
#!/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 { createSiteIndex } from "./createSiteIndex.js";
import { emptyDir } from "fs-extra";
import cpy from "cpy";
import { glob } from "glob";
import { resolve } from "path";

const inputDir = resolve(process.cwd(), "input");
const outputDir = resolve(process.cwd(), "out");
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();

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;
}

for (const file of await glob(`${inputDir}/**/*.md`, { nodir: true })) {
  await write(await kushiyaki(file));
}