Swiftpack.co - gonzalezreal/SwiftCommonMark as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Parse and create CommonMark documents in Swift.
.package(url: "https://github.com/gonzalezreal/SwiftCommonMark.git", from: "0.1.2")


CI contact: @gonzalezreal

SwiftCommonMark is a library for parsing and creating Markdown documents in Swift, fully compliant with the CommonMark Spec.


You can create a Document by providing a CommonMark string.

let document = Document(
    It's very easy to make some words **bold** and other words *italic* with Markdown.

    **Want to experiment with Markdown?** Play with the [reference CommonMark

And access its abstract syntax tree via the blocks property.

for block in document.blocks {
    switch block {
    case let .blockQuote(blocks):
        // Inspect the nested blocks
    case let .list(list):
        print(list.style) // bullet or ordered
        print(list.spacing) // loose or tight
        for item in list.items {
            // Inspect item.blocks
    case let .code(text, language):
        // A code block
    case let .html(content):
        // An HTML block
    case let .paragraph(inlines):
        // Inspect nested inlines
        for inline in inlines {
            switch inline {
            case let .text(text):
                // Plain text
            case .softBreak:
                // A soft break is usually replaced by a space
            case .lineBreak:
                // A line break
            case let .code(text):
                // Inline code
            case let .html(text):
                // Inline HTML
            case let .emphasis(inlines):
                // Emphasized inlines
            case let .strong(inlines):
                // Strong inlines
            case let .link(inlines, url, title):
                // A link
            case let .image(inlines, url, title):
                // An image
    case let .heading(inlines, level):
        // A heading with the specified level
    case .thematicBreak:
        // A thematic break

The MarkdownUI library uses this technique to render a CommonMark Document into an NSAttributedString.

From Swift 5.4 onwards, you can create a Document in a type-safe manner using an embedded DSL.

let document = Document {
    Heading(level: 2) {
        "Markdown lists"
    "Sometimes you want numbered lists:"
    List(start: 1) {
    "Sometimes you want bullet points:"
    List {
        "Start a line with a star"
        Item {
            "And you can have sub points:"
            List {
                "Like this"
                "And this"

You can always get the CommonMark syntax of a document by accessing its description. For instance, calling description in the previous document:


Produces the following output:

## Markdown lists

Sometimes you want numbered lists:

1.  One
2.  Two
3.  Three

Sometimes you want bullet points:

  - Start a line with a star
  - Profit\!
  - And you can have sub points:
      - Like this
      - And this


You can add SwiftCommonMark to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter https://github.com/gonzalezreal/SwiftCommonMark into the package repository URL text field
  3. Link CommonMark to your application target

Other Libraries


Stars: 13
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

SwiftCommonMark 0.1.2
18 weeks ago
  • Fixed: Parse image URLs within links (thanks to @GyroJoe)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics