all repos — kushiyaki @ c4d087483d9dd802b16dc357a1f870aed249c609

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

insertJSX.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
import { select } from "hast-util-select";
import { resolve } from "path";
/*
 * DONE filedata
 * DONE before/after
 * DONE multiple components
 * DONE global data
 * HACK deduplicate
 * HACK make the data validation more elegant
 */
export default function insertJSX(data = {}) {
  const siteIndex = this.data("siteIndex"); // calls siteIndex from the processor
  const templateDir = resolve(process.cwd(), "templates");

  return async (tree) => {
    if (data.before != undefined) {
      for (let x of [data.before].flat().reverse()) {
        const fn = await import(`${templateDir}/${x}.jsx`); //FIX avoid strict filestruct past "input"
        tree.children.unshift(fn.default(data, siteIndex)); //TODO use select on "body"
      }
    }
    if (data.after != undefined) {
      for (let x of [data.after].flat().reverse()) {
        const fn = await import(`${templateDir}/${x}.jsx`); //FIX avoid strict filestruct past "input"
        tree.children.push(fn.default(data, siteIndex)); //TODO use select on "body"
      }
    }
  };
}