Sources/OrgApp/HTMLPreview.swift

15 lines · 876 bytes

 1import Foundation
 2
 3/// Export to HTML and Open where the page is shown from a temporary file (iOS, in Quick Look):
 4/// the images go in the page, as nothing is beside it there.
 5public enum HTMLPreview {
 6    /// Writes `html` (an `HTMLExport.page`) as `name` in a new temporary folder. `directory` is
 7    /// the Org file's folder, where relative image links are read from.
 8    public static func write(_ html: String, directory: URL?, name: String) throws -> URL {
 9        let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-html-\(UUID().uuidString)")
10        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
11        let url = folder.appendingPathComponent(name)
12        try PDFExport.inliningImages(html, directory: directory).write(to: url, atomically: true, encoding: .utf8)
13        return url
14    }
15}