SwiftUI: which Markdown specification is supported?

Hi, thanks for adding markdown support in SwiftUI's Text with Xcode 13 and iOS 15! :)

Even formatting like strikethrough works!

Text("Hello ~~World~~")

So which specification is supported?? This goes apparently beyond Commonmark (as strikethrough is not part of Commonmark specification). Is it GitHub Flavored Markdown or even a different spec?

Answered by Engineer in 678926022

Yes, it is GitHub flavored markdown. AttributedString converts both inline and block styles. SwiftUI renders inline styles (but not images at this time). We use the fantastic cmark-gfm library to parse the markdown string.

SwiftUI’s Markdown support is really Foundation.AttributedString’s Markdown support, so I would recommend you add the “Foundation” tag to this question so the team who can answer it will find it.

Accepted Answer

Yes, it is GitHub flavored markdown. AttributedString converts both inline and block styles. SwiftUI renders inline styles (but not images at this time). We use the fantastic cmark-gfm library to parse the markdown string.

I can get to the code blocks if I do this:

var aString: AttributedString = try? AttributedString(markdown: m)

for r in aString.runs {
    if let pi = r.presentationIntent {
        for i in pi.components {
        }
    }
}

But - then I lose all the new line characters in my initial string.

So I can preserve the new line characters like this:

var aString: AttributedString = try? AttributedString(markdown: m, options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))

But - now there are no code block annotations.

Is there a way that I can preserve new lines AND get code block annotations? I need the code block annotations so I can set my own formatting. (At least until Apple renders code blocks properly).

Any solution for this yet? I am trying add indentation to my paragraph lines which is not being rendered either.

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byWordWrapping
    paragraphStyle.firstLineHeadIndent = 20
    paragraphStyle.lineSpacing = 0
    paragraphStyle.headIndent = 20
    return paragraphStyle
  }()
SwiftUI: which Markdown specification is supported?
 
 
Q