index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import fs from "fs/promises"
  2. import { minify } from "html-minifier-terser"
  3. import matter from "gray-matter"
  4. import { getHref, slugifyString, writeFile } from "../../util/index.js"
  5. import { createMarkdownRenderer } from "../_shared/markdown.js"
  6. import { getTemplateByName, loadPartials } from "../_shared/template-cache.js"
  7. export async function renderMarkdownWithTemplate({ config: actionConfig, meta }) {
  8. const filePath = actionConfig.filePath
  9. const fileOutputPath = actionConfig.fileOutputPath
  10. const content = await fs.readFile(filePath, "utf8")
  11. const { data, content: markdown } = matter(content)
  12. const templateName = data.template || actionConfig.defaultTemplate
  13. const href = getHref(fileOutputPath, meta)
  14. const partialPaths = await loadPartials(actionConfig.partialDirs)
  15. const template = await getTemplateByName(actionConfig.templateDirs, templateName)
  16. const renderer = createMarkdownRenderer(meta)
  17. const html = template.renderer({
  18. ...data,
  19. ...meta,
  20. href,
  21. content: renderer(markdown),
  22. })
  23. const minifiedHtml = await minify(html, {
  24. collapseWhitespace: true,
  25. removeComments: true,
  26. removeRedundantAttributes: true,
  27. removeEmptyAttributes: true,
  28. minifyCSS: true,
  29. minifyJS: true,
  30. })
  31. await writeFile(fileOutputPath, minifiedHtml)
  32. return {
  33. detail: { ...data, href },
  34. paths: [fileOutputPath],
  35. deps: {
  36. paths: [template.path, ...partialPaths],
  37. },
  38. ref: slugifyString(fileOutputPath),
  39. }
  40. }