Gemini/GeminiIntents/GemtextToMarkdownIntentHandler.swift
2021-10-09 10:17:49 -04:00

29 lines
1.0 KiB
Swift

//
// GemtextToMarkdownIntentHandler.swift
// GeminiIntents
//
// Created by Shadowfacts on 10/1/21.
//
import Intents
import GeminiFormat
class GemtextToMarkdownIntentHandler: NSObject, GemtextToMarkdownIntentHandling {
func handle(intent: GemtextToMarkdownIntent, completion: @escaping (GemtextToMarkdownIntentResponse) -> Void) {
guard let response = intent.response,
let text = response.body,
let url = response.url else {
completion(GemtextToMarkdownIntentResponse(code: .failure, userActivity: nil))
return
}
let doc = GeminiParser.parse(text: text, baseURL: url)
let renderer = GeminiMarkdownRenderer()
let html = renderer.renderDocumentToMarkdown(doc)
let intentResp = GemtextToMarkdownIntentResponse(code: .success, userActivity: nil)
intentResp.markdown = INFile(data: html.data(using: .utf8)!, filename: "converted_gemtext.md", typeIdentifier: "net.daringfireball.markdown")
completion(intentResp)
}
}