Add image gallery block
This commit is contained in:
46
CHDataManagement/Generator/Blocks/GalleryBlock.swift
Normal file
46
CHDataManagement/Generator/Blocks/GalleryBlock.swift
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
struct GalleryBlock: BlockLineProcessor {
|
||||
|
||||
static let blockId: ContentBlock = .gallery
|
||||
|
||||
let content: Content
|
||||
|
||||
let results: PageGenerationResults
|
||||
|
||||
let language: ContentLanguage
|
||||
|
||||
init(content: Content, results: PageGenerationResults, language: ContentLanguage) {
|
||||
self.content = content
|
||||
self.results = results
|
||||
self.language = language
|
||||
}
|
||||
|
||||
private var imageWidth: Int {
|
||||
content.settings.pages.contentWidth
|
||||
}
|
||||
|
||||
func process(_ lines: [String], markdown: Substring) -> String {
|
||||
var images = [FileResource]()
|
||||
|
||||
for line in lines {
|
||||
let imageId = line.trimmed
|
||||
guard !imageId.isEmpty else { continue }
|
||||
guard let image = content.image(imageId) else {
|
||||
results.missing(file: imageId, source: "Route block")
|
||||
continue
|
||||
}
|
||||
images.append(image)
|
||||
}
|
||||
guard let firstImage = images.first else { return "" }
|
||||
|
||||
let imageSets = images.map {
|
||||
$0.imageSet(width: imageWidth, height: imageWidth, language: language)
|
||||
}
|
||||
imageSets.forEach(results.require)
|
||||
let id = firstImage.id.replacingOccurrences(of: ".", with: "-")
|
||||
let gallery = ImageGallery(id: id, images: imageSets, standalone: true)
|
||||
results.require(footer: gallery.standaloneFooter)
|
||||
results.require(headers: .swiperJs, .swiperCss)
|
||||
return gallery.content
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user