Skip to content

Instantly share code, notes, and snippets.

@jamesporter
Created April 25, 2023 22:28
Show Gist options
  • Save jamesporter/7cb191717af52e45b455de0150f8a7d6 to your computer and use it in GitHub Desktop.
Save jamesporter/7cb191717af52e45b455de0150f8a7d6 to your computer and use it in GitHub Desktop.
import SwiftUI
extension String {
var asAttributedMarkdown: AttributedString {
do {
return try AttributedString(markdown: self, options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))
} catch {
return AttributedString(stringLiteral: self)
}
}
}
struct AttributedText: View {
var text: String
init(_ text: String) {
self.text = text
}
var body: some View {
Text(text.asAttributedMarkdown)
}
}
struct AttributedText_Previews: PreviewProvider {
static var previews: some View {
AttributedText("**test** that this *is* \n```var code```")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment