DEV Community

Cover image for Bringing Your Markdown to Life: A Guide to Rendering Markdown in SwiftUI
Shameem Reza
Shameem Reza

Posted on

Bringing Your Markdown to Life: A Guide to Rendering Markdown in SwiftUI

SwiftUI is a powerful framework for building intuitive and responsive user interfaces on Apple platforms. One of the most common use cases for apps is displaying rich text, and Markdown is a popular format for representing this type of content. In this article, we'll explore how to use the Down library to render Markdown in a SwiftUI app.

First, let's take a look at what Markdown is and why it's useful. Markdown is a lightweight markup language that uses a simple syntax to format text. The syntax is designed to be easy to read and write and can be converted to other formats, such as HTML and PDF. Markdown is often used for documentation, readme files, and blog posts because it allows writers to focus on content, not formatting.

Before we dive into the code, you need to install the Down library using the Swift Package Manager by adding the following line to your Package.swift file.

dependencies: [
    .package(url: "https://github.com/johnxnguyen/Down.git", from: "4.0.0")
]
Enter fullscreen mode Exit fullscreen mode

With the Down library installed, we can create a simple SwiftUI view that displays Markdown. In the following example, we have a ContentView struct that has a TextEditor and a MarkdownView. The TextEditor allows the user to enter some Markdown text, and the MarkdownView displays that text as formatted HTML. The MarkdownView struct uses the Down library to convert the Markdown text to HTML. The toSwiftUI() method provided by Down converts the HTML to a View, which can be displayed by the MarkdownView.

import SwiftUI
import Down

struct ContentView: View {
    @State private var markdown: String = "# Welcome to Dev Community\nHere is some **bold** text."

    var body: some View {
        VStack {
            Text("Enter Markdown:")
            TextEditor(text: $markdown)
            Divider()
            MarkdownView(markdown: markdown)
        }
    }
}

struct MarkdownView: View {
    let markdown: String

    var body: some View {
        let markdown = Down(markdownString: self.markdown)

        return VStack {
            markdown.toSwiftUI().frame(minWidth: 0, maxWidth: .infinity)
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

In the above example, the ContentView struct contains a TextEditor and a MarkdownView. The TextEditor is bound to the markdown property, which is a state variable that stores the Markdown text entered by the user. The MarkdownView struct takes the markdown property as an input and uses the Down library to convert it to HTML. The toSwiftUI() method is then used to convert the HTML to a View, which is displayed in the MarkdownView.

The MarkdownView struct uses the frame modifier to set the minimum and maximum width of the view. This ensures that the view takes up the full width of the screen, regardless of the size of the device or orientation.

It's worth noting that this example is just a starting point, and in a production app, you would likely have more robust error handling and validation in place. Additionally, you could also customize the appearance of the rendered Markdown by using custom CSS styles. You can also add images and videos and provide live web links.

Another thing to consider is handling different types of markdowns. With the rise of different markdown dialects like GitHub Flavored Markdown and MultiMarkdown, some features may not be supported by the Down library. However, with the flexibility of SwiftUI, you can handle this with ease.

To handle different dialects of markdown, you can use any other markdown parsers available for swift like:

  • PEGMarkdown
  • MarkdownKit
  • MarkdownParser
  • MarkdownRenderer

All the libraries mentioned above have slightly different syntax and feature support, so be sure to pick the one that best fits your needs.

Another thing to consider is handling large markdown files; you can load the file using the FileManager and then render it using the markdown parser.

Update:

With the latest iOS 15 update, Apple has included built-in support for Markdown in SwiftUI. This makes it even simpler to incorporate rich text formatting into your app.

struct ContentView: View {
   var body: some View {
       VStack {
           Text("**Thank you**")
               .foregroundColor(.green)
           Text("*for your support*")
           Text("Welcome to [Dev](https://dev.to) community")
       }
   }
}
Enter fullscreen mode Exit fullscreen mode

In conclusion, SwiftUI is a powerful framework that makes it easy to build responsive user interfaces, and Markdown is a popular format for representing rich text. You can easily render Markdown in your SwiftUI app using the Down library. With the flexibility of SwiftUI, you can handle different dialects of markdown and large markdown files as well. With this approach, you can provide a great reading experience to your users.

Top comments (2)

Collapse
 
johannes_k_rexx profile image
johnblommers

The link to the Down Library in the article github.com/JohnSundell/Down goes to 404.

Collapse
 
shameemreza profile image
Shameem Reza

Thanks @johnblommers, I have updated the link and article too.