import Ink final class PageCommandExtractor { private var occurences: [(full: String, command: String, arguments: [String])] = [] func findOccurences(of command: ShorthandMarkdownKey, in content: String) -> [(full: String, arguments: [String])] { findOccurences(of: command.rawValue, in: content) } func findOccurences(of command: String, in content: String) -> [(full: String, arguments: [String])] { let parser = MarkdownParser(modifiers: [ Modifier(target: .images, closure: processMarkdownImage), ]) _ = parser.html(from: content) return occurences .filter { $0.command == command } .map { ($0.full, $0.arguments) } } private func processMarkdownImage(html: String, markdown: Substring) -> String { let argumentList = markdown.between(first: "](", andLast: ")").removingPercentEncoding ?? markdown.between(first: "](", andLast: ")") let arguments = argumentList.components(separatedBy: ";") let command = markdown.between("![", and: "]").trimmed occurences.append((full: String(markdown), command: command, arguments: arguments)) return "" } }