import Ink struct MarkdownHeadlineProcessor: MarkdownProcessor { static let modifier: Modifier.Target = .headings let content: Content let results: PageGenerationResults let language: ContentLanguage init(content: Content, results: PageGenerationResults, language: ContentLanguage) { self.content = content self.results = results self.language = language } /** Modify headlines by extracting an id from the headline and adding it into the html element Format: ###<id> The id is created by lowercasing the string, removing all special characters, and replacing spaces with scores */ func process(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: ">") } }