Add 3d model short command

This commit is contained in:
Christoph Hagen 2023-12-16 10:55:36 +01:00
parent 81fa5c38de
commit 1d97560c40
2 changed files with 30 additions and 0 deletions

View File

@ -104,6 +104,7 @@ struct PageContentGenerator {
// For svg with custom area: ![x,y,width,height](file.svg)
// For downloads: ![download](file1, text1; file2, text2, (download-name); ...)
// For a simple box: ![box](title;body)
// For 3D models: ![3d](file;Description)
// A fancy page link: ![page](page_id)
// External pages: ![external](url1, text1; url2, text2, ...)
guard let fileAndTitle = markdown.between(first: "](", andLast: ")").removingPercentEncoding else {
@ -147,6 +148,8 @@ struct PageContentGenerator {
return handleSimpleBox(page: page, content: content)
case .pageLink:
return handlePageLink(page: page, language: language, pageId: content)
case .model3d:
return handle3dModel(page: page, content: content)
}
}
@ -399,4 +402,26 @@ struct PageContentGenerator {
// We assume that the thumbnail images are already required by overview pages.
return factory.pageLink.generate(content)
}
private func handle3dModel(page: Element, content: String) -> String {
let parts = content.components(separatedBy: ";")
guard parts.count > 1 else {
results.warning("Invalid 3d model specification", source: page.path)
return ""
}
let file = parts[0]
guard file.hasSuffix(".glb") else {
results.warning("Invalid 3d model file \(file) (must be .glb)", source: page.path)
return ""
}
// Ensure that file is available
let filePath = page.pathRelativeToRootForContainedInputFile(file)
results.require(file: filePath, source: page.path)
let description = parts.dropFirst().joined(separator: ";")
return """
<model-viewer alt="\(description)" src="\(file)" ar shadow-intensity="1" camera-controls touch-action="pan-y"></model-viewer>
"""
}
}

View File

@ -26,4 +26,9 @@ enum ShorthandMarkdownKey: String {
A pretty link to another page on the site.
*/
case pageLink = "page"
/**
A 3D model to display
*/
case model3d = "3d"
}