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.

Revisions

  1. jamesporter created this gist Apr 25, 2023.
    29 changes: 29 additions & 0 deletions AttributedText.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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```")
    }
    }