Allow page id references with section

This commit is contained in:
Christoph Hagen 2023-07-28 13:59:08 +02:00
parent bd57c6fbf5
commit 2608e870cc
2 changed files with 11 additions and 3 deletions

View File

@ -56,6 +56,11 @@ extension String {
return parts.dropLast().joined(separator: separator) + content + separator + parts.last! return parts.dropLast().joined(separator: separator) + content + separator + parts.last!
} }
/**
Remove everything behind the first separator.
Also removes the separator itself. If the separator is not contained in the string, then the full string is returned.
*/
func dropAfterFirst<T>(_ separator: T) -> String where T: StringProtocol { func dropAfterFirst<T>(_ separator: T) -> String where T: StringProtocol {
components(separatedBy: separator).first! components(separatedBy: separator).first!
} }

View File

@ -4,6 +4,8 @@ import Splash
struct PageContentGenerator { struct PageContentGenerator {
private let pageLinkMarker = "page:"
private let largeImageIndicator = "*large*" private let largeImageIndicator = "*large*"
private let factory: TemplateFactory private let factory: TemplateFactory
@ -51,15 +53,16 @@ struct PageContentGenerator {
private func handleLink(page: Element, language: String, html: String, markdown: Substring) -> String { private func handleLink(page: Element, language: String, html: String, markdown: Substring) -> String {
let file = markdown.between("(", and: ")") let file = markdown.between("(", and: ")")
if file.hasPrefix("page:") { if file.hasPrefix(pageLinkMarker) {
let pageId = file.replacingOccurrences(of: "page:", with: "") let textToChange = file.dropAfterFirst("#")
let pageId = textToChange.replacingOccurrences(of: pageLinkMarker, with: "")
guard let pagePath = results.getPagePath(for: pageId, source: page.path, language: language) else { guard let pagePath = results.getPagePath(for: pageId, source: page.path, language: language) else {
// Remove link since the page can't be found // Remove link since the page can't be found
return markdown.between("[", and: "]") return markdown.between("[", and: "]")
} }
// Adjust file path to get the page url // Adjust file path to get the page url
let url = page.relativePathToOtherSiteElement(file: pagePath) let url = page.relativePathToOtherSiteElement(file: pagePath)
return html.replacingOccurrences(of: file, with: url) return html.replacingOccurrences(of: textToChange, with: url)
} }
if let filePath = page.nonAbsolutePathRelativeToRootForContainedInputFile(file) { if let filePath = page.nonAbsolutePathRelativeToRootForContainedInputFile(file) {