Sources/OrgstarSpotlight/ImportExtension.swift

a866e469a3568bf7a2829a8dd2a56317ced4736e
orgstar/Sources/OrgstarSpotlight/ImportExtension.swift history · blame · raw

17 lines · 815 bytes

 1import CoreSpotlight
 2import Foundation
 3import OrgCore
 4
 5/// Spotlight's importer for org files: the text, plus the title, author, tags and headings.
 6@objc(ImportExtension)
 7final class ImportExtension: CSImportExtension {
 8    override func update(_ attributes: CSSearchableItemAttributeSet, forFileAt contentURL: URL) throws {
 9        let text = try String(contentsOf: contentURL, encoding: .utf8)
10        let summary = FileSummary(text)
11        attributes.textContent = text
12        attributes.title = summary.title ?? contentURL.deletingPathExtension().lastPathComponent
13        if let author = summary.author { attributes.authorNames = [author] }
14        if let description = summary.description { attributes.contentDescription = description }
15        attributes.keywords = summary.tags + summary.headings
16    }
17}