| | |
| | | import { trace } from "../util/trace" |
| | | import { BuildCtx } from "../util/ctx" |
| | | |
| | | export type QuartzProcessor = Processor<MDRoot, HTMLRoot, void> |
| | | export type QuartzProcessor = Processor<MDRoot, MDRoot, HTMLRoot> |
| | | export function createProcessor(ctx: BuildCtx): QuartzProcessor { |
| | | const transformers = ctx.cfg.plugins.transformers |
| | | |
| | | return ( |
| | | unified() |
| | | // base Markdown -> MD AST |
| | | let processor = unified().use(remarkParse) |
| | | |
| | | .use(remarkParse) |
| | | // MD AST -> MD AST transforms |
| | | for (const plugin of transformers.filter((p) => p.markdownPlugins)) { |
| | | processor = processor.use(plugin.markdownPlugins!(ctx)) |
| | | } |
| | | |
| | | .use( |
| | | transformers |
| | | .filter((p) => p.markdownPlugins) |
| | | .flatMap((plugin) => plugin.markdownPlugins!(ctx)), |
| | | ) |
| | | // MD AST -> HTML AST |
| | | processor = processor.use(remarkRehype, { allowDangerousHtml: true }) |
| | | |
| | | .use(remarkRehype, { allowDangerousHtml: true }) |
| | | // HTML AST -> HTML AST transforms |
| | | for (const plugin of transformers.filter((p) => p.htmlPlugins)) { |
| | | processor = processor.use(plugin.htmlPlugins!(ctx)) |
| | | } |
| | | |
| | | return processor |
| | | .use(transformers.filter((p) => p.htmlPlugins).flatMap((plugin) => plugin.htmlPlugins!(ctx))) |
| | | ) |
| | | } |
| | | |
| | | function* chunks<T>(arr: T[], n: number) { |
| | |
| | | |
| | | // Text -> Text transforms |
| | | for (const plugin of cfg.plugins.transformers.filter((p) => p.textTransform)) { |
| | | file.value = plugin.textTransform!(ctx, file.value) |
| | | file.value = plugin.textTransform!(ctx, file.value.toString()) |
| | | } |
| | | |
| | | // base data properties that plugins may use |