Parsing Markdown in Swift

As Swift becomes increasingly ubiquitous in iOS development, we often find ourselves needing to process different kinds of text data. Today, we’ll focus on a popular plaintext markup language: Markdown. In this tutorial, we dive into parsing Markdown in Swift, providing examples along the way.

Understanding Markdown

Markdown is a lightweight and easy-to-use format for writing content for the web. It is highly readable, writeable, and easily convertible to HTML. For a comprehensive guide to writing in Markdown, consider our Ultimate Guide to Markdown.

The SwiftMarkdown Framework

There are various ways to parse Markdown in Swift, but one of the most effective methods is using a dedicated framework. This tutorial will focus on an open-source third-party library, SwiftMarkdown. SwiftMarkdown is built on top of libcmark, the C reference implementation of CommonMark, which ensures accurate and standardized Markdown rendering.

To start, install SwiftMarkdown through Swift Package Manager:

dependencies: [
  .package(url: "https://github.com/SwiftDocOrg/SwiftMarkdown.git", from: "1.0.0")
]

Parsing Markdown to HTML

With SwiftMarkdown installed, we can now parse a Markdown string into a HTML string. Here is an example:

import SwiftMarkdown

let markdownString = """
# Hello, Swift Markdown!

This is a basic Markdown text with **bold text**, *italic text*, [a link](https://example.com), and:

- A
- Simple
- List
"""

let html = try? SwiftMarkdown.markdownToHTML(markdownString)
print(html ?? "Markdown conversion failed")

When you run the above code, it should output:

<h1>Hello, Swift Markdown!</h1>
<p>This is a basic Markdown text with <strong>bold text</strong>, <em>italic text</em>, <a href="https://example.com">a link</a>, and:</p>
<ul>
<li>A</li>
<li>Simple</li>
<li>List</li>
</ul>

Customizing Markdown Rendering

SwiftMarkdown is flexible enough to accommodate customization of rendering. Let’s say you want to add a custom class to every link your Markdown generates. Here’s an example of how you can achieve this:

import SwiftMarkdown

let markdownString = "[a link](https://example.com)"

let document = try? SwiftMarkdown(markdown: markdownString)
document?.linkAttributes = ["class": "custom-class"]

let html = try? document?.getHTML()
print(html ?? "Markdown conversion failed")

The output will be:

<a href="https://example.com" class="custom-class">a link</a>

Visual Markdown Preview

To preview the rendered Markdown in a Swift-based text editor like NSTextView, a WKWebView component can be used where the HTML string is loaded. Further information about previewing Markdown can be found in our blog post about previewing Markdown in Vim.

Conclusion

Processing Markdown is a frequent necessity in many Swift and iOS projects. Thanks to the SwiftMarkdown framework, parsing and customizing Markdown has never been easier. Now you know how to parse Markdown in Swift, so go ahead, test it in your project, and enhance your text processing capabilities.

Back to main blog.