Add typed shorthand markdown commands

This commit is contained in:
Christoph Hagen 2022-09-18 17:49:50 +02:00
parent 53500c31f6
commit 6c21d8c857
2 changed files with 40 additions and 11 deletions

View File

@ -72,6 +72,7 @@ struct PageContentGenerator {
private func processMarkdownImage(markdown: Substring, html: String, page: Element) -> String { private func processMarkdownImage(markdown: Substring, html: String, page: Element) -> String {
// Split the markdown ![alt](file title) // Split the markdown ![alt](file title)
// There are several known shorthand commands
// For images: ![left_title](file right_title) // For images: ![left_title](file right_title)
// For videos: ![option1,option2,...](file) // For videos: ![option1,option2,...](file)
// For svg with custom area: ![x,y,width,height](file.svg) // For svg with custom area: ![x,y,width,height](file.svg)
@ -80,17 +81,8 @@ struct PageContentGenerator {
// External pages: ![external](url1, text1; url2, text2, ...) // External pages: ![external](url1, text1; url2, text2, ...)
let fileAndTitle = markdown.between("(", and: ")") let fileAndTitle = markdown.between("(", and: ")")
let alt = markdown.between("[", and: "]").nonEmpty let alt = markdown.between("[", and: "]").nonEmpty
switch alt { if let alt = alt, let command = ShorthandMarkdownKey(rawValue: alt) {
case "download": return handleShortHandCommand(command, page: page, content: fileAndTitle)
return handleDownloadButtons(page: page, content: fileAndTitle)
case "external":
return handleExternalButtons(page: page, content: fileAndTitle)
case "html":
return handleExternalHTML(page: page, file: fileAndTitle)
case "box":
return handleSimpleBox(page: page, content: fileAndTitle)
default:
break
} }
let file = fileAndTitle.dropAfterFirst(" ") let file = fileAndTitle.dropAfterFirst(" ")
@ -109,6 +101,19 @@ struct PageContentGenerator {
return handleFile(page: page, file: file, fileExtension: fileExtension) return handleFile(page: page, file: file, fileExtension: fileExtension)
} }
private func handleShortHandCommand(_ command: ShorthandMarkdownKey, page: Element, content: String) -> String {
switch command {
case .downloadButtons:
return handleDownloadButtons(page: page, content: content)
case .externalLink:
return handleExternalButtons(page: page, content: content)
case .includedHtml:
return handleExternalHTML(page: page, file: content)
case .box:
return handleSimpleBox(page: page, content: content)
}
}
private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String { private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String {
let imagePath = page.pathRelativeToRootForContainedInputFile(file) let imagePath = page.pathRelativeToRootForContainedInputFile(file)

View File

@ -0,0 +1,24 @@
import Foundation
enum ShorthandMarkdownKey: String {
/**
A variable number of download buttons for file downloads
*/
case downloadButtons = "download"
/**
A large button to an external page.
*/
case externalLink = "external"
/**
Additional HTML code include verbatim into the page.
*/
case includedHtml = "html"
/**
A box with a heading and a text description
*/
case box = "box"
}