krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Sources/OrgstarSpotlight/ImportExtension.swift

ab6ccec56eca03d0dadf2c9332aab10c4490eb62
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}