mirror of
https://github.com/alrayyes/wiki.git
synced 2025-05-03 07:18:14 +00:00
plugin integration round 2
This commit is contained in:
parent
a757521313
commit
ad6ce0d73f
29 changed files with 3863 additions and 100 deletions
0
quartz/processors/emit.ts
Normal file
0
quartz/processors/emit.ts
Normal file
0
quartz/processors/filter.ts
Normal file
0
quartz/processors/filter.ts
Normal file
58
quartz/processors/parse.ts
Normal file
58
quartz/processors/parse.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
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 { pathToSlug } from '../path'
|
||||
import path from 'path'
|
||||
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
|
||||
}
|
||||
|
||||
export async function parseMarkdown(processor: QuartzProcessor, baseDir: string, fps: string[], verbose: boolean): Promise<ProcessedContent[]> {
|
||||
const perf = new PerfTimer()
|
||||
const res: ProcessedContent[] = []
|
||||
for (const fp of fps) {
|
||||
const file = await read(fp)
|
||||
|
||||
// base data properties that plugins may use
|
||||
file.data.slug = pathToSlug(path.relative(baseDir, file.path))
|
||||
file.data.filePath = fp
|
||||
|
||||
const ast = processor.parse(file)
|
||||
res.push([await processor.run(ast, file), file])
|
||||
|
||||
if (verbose) {
|
||||
console.log(`[process] ${fp} -> ${file.data.slug}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
console.log(`Parsed and transformed ${res.length} Markdown files in ${perf.timeSince()}`)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue