chore(build): separate markdown and html handling into two separate stages (#1675)
This commit is contained in:
parent
b7a945e034
commit
c90dbacab0
4 changed files with 103 additions and 37 deletions
|
@ -1,7 +1,8 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
import workerpool from "workerpool"
|
import workerpool from "workerpool"
|
||||||
const cacheFile = "./.quartz-cache/transpiled-worker.mjs"
|
const cacheFile = "./.quartz-cache/transpiled-worker.mjs"
|
||||||
const { parseFiles } = await import(cacheFile)
|
const { parseMarkdown, processHtml } = await import(cacheFile)
|
||||||
workerpool.worker({
|
workerpool.worker({
|
||||||
parseFiles,
|
parseMarkdown,
|
||||||
|
processHtml,
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
import { Node, Parent } from "hast"
|
import { Root as HtmlRoot } from "hast"
|
||||||
|
import { Root as MdRoot } from "mdast"
|
||||||
import { Data, VFile } from "vfile"
|
import { Data, VFile } from "vfile"
|
||||||
|
|
||||||
export type QuartzPluginData = Data
|
export type QuartzPluginData = Data
|
||||||
export type ProcessedContent = [Node, VFile]
|
export type MarkdownContent = [MdRoot, VFile]
|
||||||
|
export type ProcessedContent = [HtmlRoot, VFile]
|
||||||
|
|
||||||
export function defaultProcessedContent(vfileData: Partial<QuartzPluginData>): ProcessedContent {
|
export function defaultProcessedContent(vfileData: Partial<QuartzPluginData>): ProcessedContent {
|
||||||
const root: Parent = { type: "root", children: [] }
|
const root: HtmlRoot = { type: "root", children: [] }
|
||||||
const vfile = new VFile("")
|
const vfile = new VFile("")
|
||||||
vfile.data = vfileData
|
vfile.data = vfileData
|
||||||
return [root, vfile]
|
return [root, vfile]
|
||||||
|
|
|
@ -4,18 +4,20 @@ import remarkRehype from "remark-rehype"
|
||||||
import { Processor, unified } from "unified"
|
import { Processor, unified } from "unified"
|
||||||
import { Root as MDRoot } from "remark-parse/lib"
|
import { Root as MDRoot } from "remark-parse/lib"
|
||||||
import { Root as HTMLRoot } from "hast"
|
import { Root as HTMLRoot } from "hast"
|
||||||
import { ProcessedContent } from "../plugins/vfile"
|
import { MarkdownContent, ProcessedContent } from "../plugins/vfile"
|
||||||
import { PerfTimer } from "../util/perf"
|
import { PerfTimer } from "../util/perf"
|
||||||
import { read } from "to-vfile"
|
import { read } from "to-vfile"
|
||||||
import { FilePath, QUARTZ, slugifyFilePath } from "../util/path"
|
import { FilePath, FullSlug, QUARTZ, slugifyFilePath } from "../util/path"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import workerpool, { Promise as WorkerPromise } from "workerpool"
|
import workerpool, { Promise as WorkerPromise } from "workerpool"
|
||||||
import { QuartzLogger } from "../util/log"
|
import { QuartzLogger } from "../util/log"
|
||||||
import { trace } from "../util/trace"
|
import { trace } from "../util/trace"
|
||||||
import { BuildCtx } from "../util/ctx"
|
import { BuildCtx } from "../util/ctx"
|
||||||
|
|
||||||
export type QuartzProcessor = Processor<MDRoot, MDRoot, HTMLRoot>
|
export type QuartzMdProcessor = Processor<MDRoot, MDRoot, MDRoot>
|
||||||
export function createProcessor(ctx: BuildCtx): QuartzProcessor {
|
export type QuartzHtmlProcessor = Processor<undefined, MDRoot, HTMLRoot>
|
||||||
|
|
||||||
|
export function createMdProcessor(ctx: BuildCtx): QuartzMdProcessor {
|
||||||
const transformers = ctx.cfg.plugins.transformers
|
const transformers = ctx.cfg.plugins.transformers
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -24,14 +26,20 @@ export function createProcessor(ctx: BuildCtx): QuartzProcessor {
|
||||||
.use(remarkParse)
|
.use(remarkParse)
|
||||||
// MD AST -> MD AST transforms
|
// MD AST -> MD AST transforms
|
||||||
.use(
|
.use(
|
||||||
transformers
|
transformers.flatMap((plugin) => plugin.markdownPlugins?.(ctx) ?? []),
|
||||||
.filter((p) => p.markdownPlugins)
|
) as unknown as QuartzMdProcessor
|
||||||
.flatMap((plugin) => plugin.markdownPlugins!(ctx)),
|
// ^ sadly the typing of `use` is not smart enough to infer the correct type from our plugin list
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createHtmlProcessor(ctx: BuildCtx): QuartzHtmlProcessor {
|
||||||
|
const transformers = ctx.cfg.plugins.transformers
|
||||||
|
return (
|
||||||
|
unified()
|
||||||
// MD AST -> HTML AST
|
// MD AST -> HTML AST
|
||||||
.use(remarkRehype, { allowDangerousHtml: true })
|
.use(remarkRehype, { allowDangerousHtml: true })
|
||||||
// HTML AST -> HTML AST transforms
|
// HTML AST -> HTML AST transforms
|
||||||
.use(transformers.filter((p) => p.htmlPlugins).flatMap((plugin) => plugin.htmlPlugins!(ctx)))
|
.use(transformers.flatMap((plugin) => plugin.htmlPlugins?.(ctx) ?? []))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,8 +83,8 @@ async function transpileWorkerScript() {
|
||||||
|
|
||||||
export function createFileParser(ctx: BuildCtx, fps: FilePath[]) {
|
export function createFileParser(ctx: BuildCtx, fps: FilePath[]) {
|
||||||
const { argv, cfg } = ctx
|
const { argv, cfg } = ctx
|
||||||
return async (processor: QuartzProcessor) => {
|
return async (processor: QuartzMdProcessor) => {
|
||||||
const res: ProcessedContent[] = []
|
const res: MarkdownContent[] = []
|
||||||
for (const fp of fps) {
|
for (const fp of fps) {
|
||||||
try {
|
try {
|
||||||
const perf = new PerfTimer()
|
const perf = new PerfTimer()
|
||||||
|
@ -100,10 +108,32 @@ export function createFileParser(ctx: BuildCtx, fps: FilePath[]) {
|
||||||
res.push([newAst, file])
|
res.push([newAst, file])
|
||||||
|
|
||||||
if (argv.verbose) {
|
if (argv.verbose) {
|
||||||
console.log(`[process] ${fp} -> ${file.data.slug} (${perf.timeSince()})`)
|
console.log(`[markdown] ${fp} -> ${file.data.slug} (${perf.timeSince()})`)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
trace(`\nFailed to process \`${fp}\``, err as Error)
|
trace(`\nFailed to process markdown \`${fp}\``, err as Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createMarkdownParser(ctx: BuildCtx, mdContent: MarkdownContent[]) {
|
||||||
|
return async (processor: QuartzHtmlProcessor) => {
|
||||||
|
const res: ProcessedContent[] = []
|
||||||
|
for (const [ast, file] of mdContent) {
|
||||||
|
try {
|
||||||
|
const perf = new PerfTimer()
|
||||||
|
|
||||||
|
const newAst = await processor.run(ast as MDRoot, file)
|
||||||
|
res.push([newAst, file])
|
||||||
|
|
||||||
|
if (ctx.argv.verbose) {
|
||||||
|
console.log(`[html] ${file.data.slug} (${perf.timeSince()})`)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
trace(`\nFailed to process html \`${file.data.filePath}\``, err as Error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,6 +143,7 @@ export function createFileParser(ctx: BuildCtx, fps: FilePath[]) {
|
||||||
|
|
||||||
const clamp = (num: number, min: number, max: number) =>
|
const clamp = (num: number, min: number, max: number) =>
|
||||||
Math.min(Math.max(Math.round(num), min), max)
|
Math.min(Math.max(Math.round(num), min), max)
|
||||||
|
|
||||||
export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<ProcessedContent[]> {
|
export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<ProcessedContent[]> {
|
||||||
const { argv } = ctx
|
const { argv } = ctx
|
||||||
const perf = new PerfTimer()
|
const perf = new PerfTimer()
|
||||||
|
@ -126,9 +157,8 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
|
||||||
log.start(`Parsing input files using ${concurrency} threads`)
|
log.start(`Parsing input files using ${concurrency} threads`)
|
||||||
if (concurrency === 1) {
|
if (concurrency === 1) {
|
||||||
try {
|
try {
|
||||||
const processor = createProcessor(ctx)
|
const mdRes = await createFileParser(ctx, fps)(createMdProcessor(ctx))
|
||||||
const parse = createFileParser(ctx, fps)
|
res = await createMarkdownParser(ctx, mdRes)(createHtmlProcessor(ctx))
|
||||||
res = await parse(processor)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.end()
|
log.end()
|
||||||
throw error
|
throw error
|
||||||
|
@ -140,17 +170,27 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
|
||||||
maxWorkers: concurrency,
|
maxWorkers: concurrency,
|
||||||
workerType: "thread",
|
workerType: "thread",
|
||||||
})
|
})
|
||||||
|
const errorHandler = (err: any) => {
|
||||||
const childPromises: WorkerPromise<ProcessedContent[]>[] = []
|
console.error(`${err}`.replace(/^error:\s*/i, ""))
|
||||||
for (const chunk of chunks(fps, CHUNK_SIZE)) {
|
process.exit(1)
|
||||||
childPromises.push(pool.exec("parseFiles", [ctx.buildId, argv, chunk, ctx.allSlugs]))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const results: ProcessedContent[][] = await WorkerPromise.all(childPromises).catch((err) => {
|
const mdPromises: WorkerPromise<[MarkdownContent[], FullSlug[]]>[] = []
|
||||||
const errString = err.toString().slice("Error:".length)
|
for (const chunk of chunks(fps, CHUNK_SIZE)) {
|
||||||
console.error(errString)
|
mdPromises.push(pool.exec("parseMarkdown", [ctx.buildId, argv, chunk]))
|
||||||
process.exit(1)
|
}
|
||||||
})
|
const mdResults: [MarkdownContent[], FullSlug[]][] =
|
||||||
|
await WorkerPromise.all(mdPromises).catch(errorHandler)
|
||||||
|
|
||||||
|
const childPromises: WorkerPromise<ProcessedContent[]>[] = []
|
||||||
|
for (const [_, extraSlugs] of mdResults) {
|
||||||
|
ctx.allSlugs.push(...extraSlugs)
|
||||||
|
}
|
||||||
|
for (const [mdChunk, _] of mdResults) {
|
||||||
|
childPromises.push(pool.exec("processHtml", [ctx.buildId, argv, mdChunk, ctx.allSlugs]))
|
||||||
|
}
|
||||||
|
const results: ProcessedContent[][] = await WorkerPromise.all(childPromises).catch(errorHandler)
|
||||||
|
|
||||||
res = results.flat()
|
res = results.flat()
|
||||||
await pool.terminate()
|
await pool.terminate()
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,23 +3,46 @@ sourceMapSupport.install(options)
|
||||||
import cfg from "../quartz.config"
|
import cfg from "../quartz.config"
|
||||||
import { Argv, BuildCtx } from "./util/ctx"
|
import { Argv, BuildCtx } from "./util/ctx"
|
||||||
import { FilePath, FullSlug } from "./util/path"
|
import { FilePath, FullSlug } from "./util/path"
|
||||||
import { createFileParser, createProcessor } from "./processors/parse"
|
import {
|
||||||
|
createFileParser,
|
||||||
|
createHtmlProcessor,
|
||||||
|
createMarkdownParser,
|
||||||
|
createMdProcessor,
|
||||||
|
} from "./processors/parse"
|
||||||
import { options } from "./util/sourcemap"
|
import { options } from "./util/sourcemap"
|
||||||
|
import { MarkdownContent, ProcessedContent } from "./plugins/vfile"
|
||||||
|
|
||||||
// only called from worker thread
|
// only called from worker thread
|
||||||
export async function parseFiles(
|
export async function parseMarkdown(
|
||||||
buildId: string,
|
buildId: string,
|
||||||
argv: Argv,
|
argv: Argv,
|
||||||
fps: FilePath[],
|
fps: FilePath[],
|
||||||
allSlugs: FullSlug[],
|
): Promise<[MarkdownContent[], FullSlug[]]> {
|
||||||
) {
|
// this is a hack
|
||||||
|
// we assume markdown parsers can add to `allSlugs`,
|
||||||
|
// but don't actually use them
|
||||||
|
const allSlugs: FullSlug[] = []
|
||||||
const ctx: BuildCtx = {
|
const ctx: BuildCtx = {
|
||||||
buildId,
|
buildId,
|
||||||
cfg,
|
cfg,
|
||||||
argv,
|
argv,
|
||||||
allSlugs,
|
allSlugs,
|
||||||
}
|
}
|
||||||
const processor = createProcessor(ctx)
|
return [await createFileParser(ctx, fps)(createMdProcessor(ctx)), allSlugs]
|
||||||
const parse = createFileParser(ctx, fps)
|
}
|
||||||
return parse(processor)
|
|
||||||
|
// only called from worker thread
|
||||||
|
export function processHtml(
|
||||||
|
buildId: string,
|
||||||
|
argv: Argv,
|
||||||
|
mds: MarkdownContent[],
|
||||||
|
allSlugs: FullSlug[],
|
||||||
|
): Promise<ProcessedContent[]> {
|
||||||
|
const ctx: BuildCtx = {
|
||||||
|
buildId,
|
||||||
|
cfg,
|
||||||
|
argv,
|
||||||
|
allSlugs,
|
||||||
|
}
|
||||||
|
return createMarkdownParser(ctx, mds)(createHtmlProcessor(ctx))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue