Add command for pretty page links

This commit is contained in:
Christoph Hagen
2022-09-25 17:19:07 +02:00
parent 66dcd43082
commit f2ee06b1d7
7 changed files with 144 additions and 7 deletions

View File

@ -16,7 +16,7 @@ struct PageContentGenerator {
var hasCodeContent = false
let imageModifier = Modifier(target: .images) { html, markdown in
processMarkdownImage(markdown: markdown, html: html, page: page)
processMarkdownImage(markdown: markdown, html: html, page: page, language: language)
}
let codeModifier = Modifier(target: .codeBlocks) { html, markdown in
if markdown.starts(with: "```swift") {
@ -70,7 +70,7 @@ struct PageContentGenerator {
return html
}
private func processMarkdownImage(markdown: Substring, html: String, page: Element) -> String {
private func processMarkdownImage(markdown: Substring, html: String, page: Element, language: String) -> String {
// Split the markdown ![alt](file title)
// There are several known shorthand commands
// For images: ![left_title](file right_title)
@ -78,11 +78,12 @@ struct PageContentGenerator {
// For svg with custom area: ![x,y,width,height](file.svg)
// For downloads: ![download](file1, text1; file2, text2, ...)
// For a simple boxes: ![box](title;body)
// A fancy page link: ![page](page_id)
// External pages: ![external](url1, text1; url2, text2, ...)
let fileAndTitle = markdown.between("(", and: ")")
let alt = markdown.between("[", and: "]").nonEmpty
if let alt = alt, let command = ShorthandMarkdownKey(rawValue: alt) {
return handleShortHandCommand(command, page: page, content: fileAndTitle)
return handleShortHandCommand(command, page: page, language: language, content: fileAndTitle)
}
let file = fileAndTitle.dropAfterFirst(" ")
@ -101,7 +102,7 @@ struct PageContentGenerator {
return handleFile(page: page, file: file, fileExtension: fileExtension)
}
private func handleShortHandCommand(_ command: ShorthandMarkdownKey, page: Element, content: String) -> String {
private func handleShortHandCommand(_ command: ShorthandMarkdownKey, page: Element, language: String, content: String) -> String {
switch command {
case .downloadButtons:
return handleDownloadButtons(page: page, content: content)
@ -111,6 +112,8 @@ struct PageContentGenerator {
return handleExternalHTML(page: page, file: content)
case .box:
return handleSimpleBox(page: page, content: content)
case .pageLink:
return handlePageLink(page: page, language: language, pageId: content)
}
}
@ -250,4 +253,45 @@ struct PageContentGenerator {
let text = parts.dropFirst().joined(separator: ";")
return factory.makePlaceholder(title: title, text: text)
}
private func handlePageLink(page: Element, language: String, pageId: String) -> String {
guard let linkedPage = siteRoot.find(pageId) else {
log.add(warning: "Page id '\(pageId)' not found", source: page.path)
// Remove link since the page can't be found
return ""
}
var content = [PageLinkTemplate.Key: String]()
content[.url] = page.relativePathToOtherSiteElement(file: linkedPage.fullPageUrl(for: language))
content[.title] = linkedPage.title(for: language)
let fullThumbnailPath = linkedPage.thumbnailFilePath(for: language)
let relativeImageUrl = page.relativePathToOtherSiteElement(file: fullThumbnailPath)
let metadata = linkedPage.localized(for: language)
if linkedPage.state.hasThumbnailLink {
let fullPageUrl = linkedPage.fullPageUrl(for: language)
let relativePageUrl = page.relativePathToOtherSiteElement(file: fullPageUrl)
content[.url] = "href=\"\(relativePageUrl)\""
}
content[.image] = relativeImageUrl
if let suffix = metadata.thumbnailSuffix {
content[.title] = factory.html.make(title: metadata.title, suffix: suffix)
} else {
content[.title] = metadata.title
}
content[.image2x] = relativeImageUrl.insert("@2x", beforeLast: ".")
let path = linkedPage.makePath(language: language, from: siteRoot)
content[.path] = factory.pageLink.makePath(components: path)
content[.description] = metadata.relatedContentText
if let parent = linkedPage.findParent(from: siteRoot), parent.thumbnailStyle == .large {
content[.className] = " related-page-link-large"
}
// We assume that the thumbnail images are already required by overview pages.
return factory.pageLink.generate(content)
}
}

View File

@ -21,4 +21,9 @@ enum ShorthandMarkdownKey: String {
A box with a heading and a text description
*/
case box = "box"
/**
A pretty link to another page on the site.
*/
case pageLink = "page"
}