From 2608e870cc7c907ad69a041ee526de1b8691f666 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 28 Jul 2023 13:59:08 +0200 Subject: [PATCH] Allow page id references with section --- Sources/Generator/Extensions/String+Extensions.swift | 5 +++++ Sources/Generator/Generators/PageContentGenerator.swift | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/Generator/Extensions/String+Extensions.swift b/Sources/Generator/Extensions/String+Extensions.swift index 11236e1..48c28e8 100644 --- a/Sources/Generator/Extensions/String+Extensions.swift +++ b/Sources/Generator/Extensions/String+Extensions.swift @@ -56,6 +56,11 @@ extension String { 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(_ separator: T) -> String where T: StringProtocol { components(separatedBy: separator).first! } diff --git a/Sources/Generator/Generators/PageContentGenerator.swift b/Sources/Generator/Generators/PageContentGenerator.swift index 506b66b..0765fa6 100644 --- a/Sources/Generator/Generators/PageContentGenerator.swift +++ b/Sources/Generator/Generators/PageContentGenerator.swift @@ -4,6 +4,8 @@ import Splash struct PageContentGenerator { + private let pageLinkMarker = "page:" + private let largeImageIndicator = "*large*" private let factory: TemplateFactory @@ -51,15 +53,16 @@ struct PageContentGenerator { private func handleLink(page: Element, language: String, html: String, markdown: Substring) -> String { let file = markdown.between("(", and: ")") - if file.hasPrefix("page:") { - let pageId = file.replacingOccurrences(of: "page:", with: "") + if file.hasPrefix(pageLinkMarker) { + let textToChange = file.dropAfterFirst("#") + let pageId = textToChange.replacingOccurrences(of: pageLinkMarker, with: "") guard let pagePath = results.getPagePath(for: pageId, source: page.path, language: language) else { // Remove link since the page can't be found return markdown.between("[", and: "]") } // Adjust file path to get the page url 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) {