import fs from "fs/promises" import { minify } from "html-minifier-terser" import matter from "gray-matter" import { getHref, slugifyString, writeFile } from "../../util/index.js" import { createMarkdownRenderer } from "../_shared/markdown.js" import { getTemplateByName, loadPartials } from "../_shared/template-cache.js" export async function renderMarkdownWithTemplate({ config: actionConfig, meta }) { const filePath = actionConfig.filePath const fileOutputPath = actionConfig.fileOutputPath const content = await fs.readFile(filePath, "utf8") const { data, content: markdown } = matter(content) const templateName = data.template || actionConfig.defaultTemplate const href = getHref(fileOutputPath, meta) const partialPaths = await loadPartials(actionConfig.partialDirs) const template = await getTemplateByName(actionConfig.templateDirs, templateName) const renderer = createMarkdownRenderer(meta) const html = template.renderer({ ...data, ...meta, href, content: renderer(markdown), }) const minifiedHtml = await minify(html, { collapseWhitespace: true, removeComments: true, removeRedundantAttributes: true, removeEmptyAttributes: true, minifyCSS: true, minifyJS: true, }) await writeFile(fileOutputPath, minifiedHtml) return { detail: { ...data, href }, paths: [fileOutputPath], deps: { paths: [template.path, ...partialPaths], }, ref: slugifyString(fileOutputPath), } }