From 559dd00a4d236885227824ebf67ac7bde7f99c2c Mon Sep 17 00:00:00 2001 From: Tomas Srna Date: Sun, 10 Sep 2023 08:29:11 +0200 Subject: [PATCH] Add `MarkdownContent` HTML rendering (#253) --- Sources/MarkdownUI/DSL/Blocks/MarkdownContent.swift | 5 +++++ Sources/MarkdownUI/Parser/MarkdownParser.swift | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/Sources/MarkdownUI/DSL/Blocks/MarkdownContent.swift b/Sources/MarkdownUI/DSL/Blocks/MarkdownContent.swift index 65a60068..7bcf9fa2 100644 --- a/Sources/MarkdownUI/DSL/Blocks/MarkdownContent.swift +++ b/Sources/MarkdownUI/DSL/Blocks/MarkdownContent.swift @@ -108,4 +108,9 @@ public struct MarkdownContent: Equatable, MarkdownContentProtocol { let result = self.blocks.renderPlainText() return result.hasSuffix("\n") ? String(result.dropLast()) : result } + + /// Renders this Markdown content value as HTML code. + public func renderHTML() -> String { + self.blocks.renderHTML() + } } diff --git a/Sources/MarkdownUI/Parser/MarkdownParser.swift b/Sources/MarkdownUI/Parser/MarkdownParser.swift index 45c8165b..ff6b6655 100644 --- a/Sources/MarkdownUI/Parser/MarkdownParser.swift +++ b/Sources/MarkdownUI/Parser/MarkdownParser.swift @@ -20,6 +20,12 @@ extension Array where Element == BlockNode { String(cString: cmark_render_plaintext(document, CMARK_OPT_DEFAULT, 0)) } ?? "" } + + func renderHTML() -> String { + UnsafeNode.makeDocument(self) { document in + String(cString: cmark_render_html(document, CMARK_OPT_DEFAULT, nil)) + } ?? "" + } } extension BlockNode {