Add html ids to headlines

This commit is contained in:
Christoph Hagen 2023-02-28 09:31:44 +01:00
parent a2ed35a26d
commit 86440af01f
2 changed files with 21 additions and 1 deletions

View File

@ -82,6 +82,10 @@ extension Substring {
func between(first: String, andLast last: String) -> String {
dropBeforeFirst(first).dropAfterLast(last)
}
func last(after: String) -> String {
components(separatedBy: after).last!
}
}
extension String {

View File

@ -41,8 +41,11 @@ struct PageContentGenerator {
let htmlModifier = Modifier(target: .html) { html, markdown in
handleHTML(page: page, language: language, html: html, markdown: markdown)
}
let headlinesModifier = Modifier(target: .headings) { html, markdown in
handleHeadlines(page: page, language: language, html: html, markdown: markdown)
}
let parser = MarkdownParser(modifiers: [imageModifier, codeModifier, linkModifier, htmlModifier])
let parser = MarkdownParser(modifiers: [imageModifier, codeModifier, linkModifier, htmlModifier, headlinesModifier])
return (parser.html(from: content), hasCodeContent)
}
@ -77,6 +80,19 @@ struct PageContentGenerator {
return html
}
private func handleHeadlines(page: Element, language: String, html: String, markdown: Substring) -> String {
let id = markdown
.last(after: "#")
.trimmed
.filter { $0.isNumber || $0.isLetter || $0 == " " }
.lowercased()
.components(separatedBy: " ")
.filter { $0 != "" }
.joined(separator: "-")
let parts = html.components(separatedBy: ">")
return parts[0] + " id=\"\(id)\">" + parts.dropFirst().joined(separator: ">")
}
private func processMarkdownImage(markdown: Substring, html: String, page: Element, language: String, largeImageCount: inout Int) -> String {
// Split the markdown ![alt](file title)
// There are several known shorthand commands