processors.js 960 B

123456789101112131415161718192021222324252627282930313233
  1. import fs from "fs"
  2. import path from "path"
  3. import matter from "gray-matter"
  4. import { marked } from "marked"
  5. import handlebars from "handlebars"
  6. import * as sass from "sass"
  7. import markedCodePreview from "marked-code-preview"
  8. const markedRenderer = marked.use({ gfm: true }).use(markedCodePreview)
  9. export function renderMarkdownWithTemplate(filePath, meta) {
  10. const content = fs.readFileSync(filePath, "utf8")
  11. const { data, content: markdown } = matter(content)
  12. const templateName = data.template || meta.opts.defaultTemplate
  13. const template = handlebars.compile(
  14. fs.readFileSync(
  15. path.join(meta.opts.templatesDir, `${templateName}.hbs`),
  16. "utf8",
  17. ),
  18. )
  19. const html = template({
  20. ...data,
  21. ...meta,
  22. content: markedRenderer(markdown),
  23. })
  24. return { result: html, detail: data }
  25. }
  26. export function compileSass(filePath, meta) {
  27. return {
  28. result: sass.compile(filePath, { style: "compressed" }).css.toString(),
  29. }
  30. }