Refactor page content generators
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
import Ink
|
||||
|
||||
struct MarkdownCodeProcessor: MarkdownProcessor {
|
||||
|
||||
static let modifier: Modifier.Target = .codeBlocks
|
||||
|
||||
private let results: PageGenerationResults
|
||||
|
||||
private let blocks: [ContentBlock : BlockProcessor]
|
||||
|
||||
private let other: OtherCodeProcessor
|
||||
|
||||
init(content: Content, results: PageGenerationResults, language: ContentLanguage) {
|
||||
self.results = results
|
||||
self.other = .init(results: results)
|
||||
|
||||
self.blocks = ContentBlock.allCases.reduce(into: [:]) { blocks, block in
|
||||
blocks[block] = block.processor.init(content: content, results: results, language: language)
|
||||
}
|
||||
}
|
||||
|
||||
private let codeHighlightFooter = "<script>hljs.highlightAll();</script>"
|
||||
|
||||
func process(html: String, markdown: Substring) -> String {
|
||||
let input = String(markdown)
|
||||
let rawBlockId = input.dropAfterFirst("\n").dropBeforeFirst("```").trimmed
|
||||
guard let blockId = ContentBlock(rawValue: rawBlockId) else {
|
||||
return other.process(html: html)
|
||||
}
|
||||
return blocks[blockId]!.process(markdown)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user