Diego Lavalle – Swift and Apple Platforms Development

May 10, 2021 • Server-Side Swift, Content Management

Supporting Markdown on SwiftySites

DL

One of the first challenges one encounters when building a static site generator is Markdown support.

I knew Apple had just announced during WWDC21 support for Markdown in attributed strings and other APIs but offered no interface for directly converting Markdown to HTML.

A bit of digging in the developer forums led me to this answer by an Apple engineer. Now I knew for a fact that internally they were using the fantastic cmark-gfm library.

cmark-gfm itself is a fork of the cmark C library. GFM stands for GitHub Flavor Markdown and consists of a set of extensions to the CommonMark specification.

Having some experience in integrating C code into Swift applications I started adapting cmark-gfm for my purposes. The endeavor involved cross-compiling for the relevant architectures and linking to a custom Objective-C framework.

Since cmark-gfm lacked a simplified interface for straightforwardly converting GFM to HTML – which the original cmark had – I decided to write my own Swift protocol around it. I decided to expose all the options and extensions available from the underlying libraries.

While the whole effort was open-source I decided to publish the final result as a binary XCFramework. This makes it really easy to include – via Swift Package Manager (SPM) – in SwiftySites or any other project that needs it without the hassle of having to build everything from C sources.