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?

  • 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.

Add a Comment

Accepted Reply

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.

  • There is a similar question here, which mentions that Lists and 3 backtics are not supported either.

    https://developer.apple.com/forums/thread/682771

    is this a bug or are they not supported by Github Flavored Markdown? I’d be surprised if the backticks weren’t supported.

  • AttributedString will annotate the ranges with an attribute (PresentationIntent) but Text will not render those (or other block styles like block quotes and tables).

  • Is there a SwiftUI or UIKit component that will render block styles like code blocks, block quotes and tables?

Replies

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.

  • There is a similar question here, which mentions that Lists and 3 backtics are not supported either.

    https://developer.apple.com/forums/thread/682771

    is this a bug or are they not supported by Github Flavored Markdown? I’d be surprised if the backticks weren’t supported.

  • AttributedString will annotate the ranges with an attribute (PresentationIntent) but Text will not render those (or other block styles like block quotes and tables).

  • Is there a SwiftUI or UIKit component that will render block styles like code blocks, block quotes and tables?

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).

  • It is possible that some newline characters may be turned into line breaks or soft breaks. Those breaks are annotated with the InlinePresentationIntent attribute in the attributed string created from the markdown. Do you see those attributes being applied?

  • Yes, I see this in the console:

    (lldb) po pi [paragraph (id 1)] ▿ components : 1 element ▿ 0 : paragraph (id 1) - kind : paragraph - identity : 1

    What's the best way to get my new lines back? Should I just insert "\n" everywhere I find a paragraph presentationIntent?

Add a Comment

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
  }()