all repos — kushiyaki @ 8e7c9d2ed42b7aad6b479eb4bb7026f7348b9003

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

deprecated/write_website_flat.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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
import rehypeFormat from "rehype-format";
import rehypeRaw from "rehype-raw";
import rehypeStringify from "rehype-stringify";
import remarkFrontmatter from "remark-frontmatter";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
import { read } from "to-vfile";
import {
  copy,
  emptyDir,
  ensureDir,
  readFile,
  writeFile,
  readdir,
} from "fs-extra";
import { argv0 } from "process";
import baseline from "./input/baseline.jsx";
let frontmatter;

// builds the Markdown --> HTML processor
const processor = unified()
  .use(remarkParse)
  .use(remarkFrontmatter, "yaml")
  // frontmatter
  .use(() => (tree) => {
    frontmatter = tree.children[0].value;
    if (tree.children[0].type == "yaml") {
      frontmatter = Object.fromEntries(
        frontmatter.split("\n").map((line) => line.split(": ")),
      );
      console.log(frontmatter);
    } else {
      console.error("  ERROR: Frontmatter not found");
      process.exit();
    }
  });

  .use(remarkRehype, { allowDangerousHtml: true })
  .use(rehypeRaw)
  .use(rehypeFormat)
  // turn hast tree into string
  // consider: add this tree as child to JSX produced tree
  .use(rehypeStringify);

// clears out the output directory and copies static files into it.
await emptyDir("out", { recursive: true });
// await copy("css", "out/css");
// await copy("static", "out/static");
// Reads the content folder
const input = await readdir("./input/", {
  recursive: true,
  withFileTypes: true,
});
const jsxProcessor = unified()
  .use(rehypeFormat)
  // turn hast tree into string
  // consider: add this tree as child to JSX produced tree
  .use(rehypeStringify);

// Iterates over content files
for (let file of input) {
  if (file.isFile()) {
    let filepath = file.path + "/" + file.name;
    const input_file = await readFile(filepath, "utf8");
    console.log(filepath);

    // Invokes Markdown --> HTML Processor
    console.log("  " + filepath + " parsing");
    let parsed_input = await processor.run(await processor.parse(input_file));
    // builds layout
    // const layoutMain = eval(frontmatter.main)({});

    // parses JSX HACK
    const markdown = () => parsed_input;
    const output = await jsxProcessor.stringify(
      await jsxProcessor.run(
        baseline({
          css: frontmatter.css,
          header: eval(frontmatter.header)?.(frontmatter),
          before: eval(frontmatter.before)?.(frontmatter),
          main: markdown(),
          after: eval(frontmatter.after)?.(frontmatter),
          footer: eval(frontmatter.footer)?.(frontmatter),
        }),
      ),
    );

    // Writes output to a new HTML file
    await writeFile(
      file.path.replace("content", "out") +
        "/" +
        file.name.split(".")[0] +
        ".html",
      String(output),
      "utf8",
    );
    console.log("  " + filepath + " written");
    console.log(frontmatter);
  }
}

console.log("\n----- WRITING COMPLETE -----");