rizaldy.club/quartz/processors/parse.ts

124 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-06-04 23:35:45 +07:00
import esbuild from 'esbuild'
2023-05-30 22:02:20 +07:00
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { Processor, unified } from "unified"
import { Root as MDRoot } from 'remark-parse/lib'
import { Root as HTMLRoot } from 'hast'
import { ProcessedContent } from '../plugins/vfile'
import { PerfTimer } from '../perf'
import { read } from 'to-vfile'
import { slugify } from '../path'
2023-05-30 22:02:20 +07:00
import path from 'path'
2023-06-04 23:35:45 +07:00
import os from 'os'
import workerpool, { Promise as WorkerPromise } from 'workerpool'
2023-05-30 22:02:20 +07:00
import { QuartzTransformerPlugin } from '../plugins/types'
export type QuartzProcessor = Processor<MDRoot, HTMLRoot, void>
export function createProcessor(transformers: QuartzTransformerPlugin[]): any {
// base Markdown -> MD AST
let processor = unified().use(remarkParse)
// MD AST -> MD AST transforms
for (const plugin of transformers) {
processor = processor.use(plugin.markdownPlugins())
}
// MD AST -> HTML AST
processor = processor.use(remarkRehype, { allowDangerousHtml: true })
// HTML AST -> HTML AST transforms
for (const plugin of transformers) {
processor = processor.use(plugin.htmlPlugins())
}
return processor
}
2023-06-04 23:35:45 +07:00
function* chunks<T>(arr: T[], n: number) {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n)
}
}
async function transpileWorkerScript(verbose: boolean) {
// transpile worker script
const cacheFile = "./.quartz-cache/transpiled-worker.mjs"
const fp = "./quartz/worker.ts"
if (verbose) {
console.log("Transpiling worker script")
}
await esbuild.build({
entryPoints: [fp],
outfile: path.join("quartz", cacheFile),
bundle: true,
keepNames: true,
platform: "node",
format: "esm",
packages: "external",
plugins: [
{
name: 'css-and-scripts-as-text',
setup(build) {
build.onLoad({ filter: /\.scss$/ }, (_) => ({
contents: '',
loader: 'text'
}))
build.onLoad({ filter: /\.inline\.(ts|js)$/ }, (_) => ({
contents: '',
loader: 'text'
}))
}
}
]
})
}
export async function parseMarkdown(transformers: QuartzTransformerPlugin[], baseDir: string, fps: string[], verbose: boolean): Promise<ProcessedContent[]> {
2023-05-30 22:02:20 +07:00
const perf = new PerfTimer()
2023-06-04 23:35:45 +07:00
const CHUNK_SIZE = 128
let concurrency = fps.length < CHUNK_SIZE ? 1 : os.availableParallelism()
2023-05-30 22:02:20 +07:00
const res: ProcessedContent[] = []
2023-06-04 23:35:45 +07:00
if (concurrency === 1) {
// single-thread
const processor = createProcessor(transformers)
for (const fp of fps) {
const file = await read(fp)
2023-05-30 22:02:20 +07:00
2023-06-04 23:35:45 +07:00
// base data properties that plugins may use
file.data.slug = slugify(path.relative(baseDir, file.path))
file.data.filePath = fp
2023-05-30 22:02:20 +07:00
2023-06-04 23:35:45 +07:00
const ast = processor.parse(file)
res.push([await processor.run(ast, file), file])
if (verbose) {
console.log(`[process] ${fp} -> ${file.data.slug}`)
}
}
} else {
await transpileWorkerScript(verbose)
const pool = workerpool.pool(
'./quartz/bootstrap-worker.mjs',
{
minWorkers: 'max',
maxWorkers: concurrency,
workerType: 'thread'
}
)
2023-05-30 22:02:20 +07:00
2023-06-04 23:35:45 +07:00
const childPromises: WorkerPromise<ProcessedContent[]>[] = []
for (const chunk of chunks(fps, CHUNK_SIZE)) {
childPromises.push(pool.exec('parseFiles', [baseDir, chunk, verbose]))
2023-05-30 22:02:20 +07:00
}
2023-06-04 23:35:45 +07:00
const results: ProcessedContent[][] = await WorkerPromise.all(childPromises)
res.push(...results.flat())
await pool.terminate()
2023-05-30 22:02:20 +07:00
}
2023-06-04 23:35:45 +07:00
console.log(`Parsed and transformed ${res.length} Markdown files with ${concurrency} cores in ${perf.timeSince()}`)
2023-05-30 22:02:20 +07:00
return res
}