Sources/OrgstarQuickLook/PreviewProvider.swift
18 lines · 876 bytes
2 symbols in this file
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}