34 lines
1.1 KiB
Swift
34 lines
1.1 KiB
Swift
|
|
struct CodeBlockProcessor {
|
|
|
|
private let codeHighlightFooter = "<script>hljs.highlightAll();</script>"
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
guard let processor = self.blocks[blockId] else {
|
|
results.invalid(block: blockId, markdown)
|
|
return ""
|
|
}
|
|
return processor.process(markdown)
|
|
}
|
|
}
|