krz/orgstar

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

Sources/OrgstarQuickLook/PreviewProvider.swift

778058aafc7ddacee4a613b1da87c60aa0c8e113
orgstar/Sources/OrgstarQuickLook/PreviewProvider.swift history · blame · raw

18 lines · 876 bytes

 1import Foundation
 2import OrgCore
 3#if canImport(QuickLookUI)
 4import QuickLookUI
 5#else
 6import QuickLook
 7#endif
 8
 9/// Quick Look's preview of an org file: the file as the HTML export renders it.
10@objc(PreviewProvider)
11final class PreviewProvider: QLPreviewProvider, QLPreviewingController {
12    func providePreview(for request: QLFilePreviewRequest) async throws -> QLPreviewReply {
13        let text = try String(contentsOf: request.fileURL, encoding: .utf8)
14        let title = request.fileURL.deletingPathExtension().lastPathComponent
15        let html = HTMLExport.page(text, fallbackTitle: title, settings: OrgSettings.default.resolvingSetupFiles(in: text, path: request.fileURL.path, read: OrgSettings.readFile), path: request.fileURL.path)
16        return QLPreviewReply(dataOfContentType: .html, contentSize: CGSize(width: 800, height: 800)) { _ in Data(html.utf8) }
17    }
18}