Ink

Welcome to Ink, a fast and flexible Markdown parser written in Swift. It can be used to convert Markdown-formatted strings into HTML, and also supports metadata parsing, as well as powerful customization options for fine-grained post-processing. It was built with a focus on Swift-based web development and other HTML-centered workflows.

Converting Markdown into HTML

To get started with Ink, all you have to do is to import it, and use its MarkdownParser type to convert any Markdown string into efficiently rendered HTML:

import Ink

let markdown: String = ...
let parser = MarkdownParser()
let html = parser.html(from: markdown)

That’s it! The resulting HTML can then be displayed as-is, or embedded into some other context — and if that’s all you need Ink for, then no more code is required.

Automatic metadata parsing

Ink also comes with metadata support built-in, meaning that you can define key/value pairs at the top of any Markdown document, which will then be automatically parsed into a Swift dictionary.

To take advantage of that feature, call the parse method on MarkdownParser, which gives you a Markdown value that both contains any metadata found within the parsed Markdown string, as well as its HTML representation:

let markdown: String = ...
let parser = MarkdownParser()
let result = parser.parse(markdown)

let dateString = result.metadata["date"]
let html = result.html

To define metadata values within a Markdown document, use the following syntax:

---
keyA: valueA
keyB: valueB
---

Markdown text...

The above format is also supported by many different Markdown editors and other tools, even though it’s not part of the original Markdown spec.

Powerful customization

Besides its built-in parsing rules, which aims to cover the most common features found in the various flavors of Markdown, you can also customize how Ink performs its parsing through the use of modifiers.

A modifier is defined using the Modifier type, and is associated with a given Target, which determines the kind of Markdown fragments that it will be used for. For example, here’s how an H3 tag could be added before each code block:

var parser = MarkdownParser()

let modifier = Modifier(target: .codeBlocks) { html, markdown in
    return "<h3>This is a code block:</h3>" + html
}

parser.addModifier(modifier)

let markdown: String = ...
let html = parser.html(from: markdown)

Modifiers are passed both the HTML that Ink generated for the given fragment, and its raw Markdown representation as well — both of which can be used to determine how each fragment should be customized.

Performance built-in

Ink was designed to be as fast and efficient as possible, to enable hundreds of full-length Markdown articles to be parsed in a matter of seconds, while still offering a fully customizable API as well. Two key characteristics make this possible:

  1. Ink aims to get as close to O(N) complexity as possible, by minimizing the amount of times it needs to read the Markdown strings that are passed to it, and by optimizing its HTML rendering to be completely linear. While true O(N) complexity is impossible to achieve when it comes to Markdown parsing, because of its very flexible syntax, the goal is to come as close to that target as possible.
  2. A high degree of memory efficiency is achieved thanks to Swift’s powerful String API, which Ink makes full use of — by using string indexes, ranges and substrings, rather than performing unnecessary string copying between its various operations.

Installation

Ink is distributed using the Swift Package Manager. To install it into a project, simply add it as a dependency within your Package.swift manifest:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/johnsundell/ink.git", from: "0.1.0")
    ],
    ...
)

Then import Ink wherever you’d like to use it:

import Ink

For more information on how to use the Swift Package Manager, check out this article, or its official documentation.

Command line tool

Ink also ships with a simple but useful command line tool that lets you convert Markdown to HTML directly from the command line.

To install it, clone the project and run make:

$ git clone https://github.com/johnsundell/Ink.git
$ cd Ink
$ make

The command line tool will be installed as ink, and can be passed Markdown text for conversion into HTML in several ways.

Calling it without arguments will start reading from stdin until terminated with Ctrl+D:

$ ink

Markdown text can be piped in when ink is called without arguments:

$ echo "*Hello World*" | ink

A single argument is treated as a filename, and the corresponding file will be parsed:

$ ink file.md

A Markdown string can be passed directly using the -m or --markdown flag:

$ ink -m "*Hello World*"

You can of course also build your own command line tools that utilizes Ink in more advanced ways by importing it as a package.

Markdown syntax supported

Ink supports the following Markdown features:

  • Headings (H1 - H6), using leading pound signs, for example ## H2.
  • Italic text, by surrounding a piece of text with either an asterisk (*), or an underscore (_). For example *Italic text*.
  • Bold text, by surrounding a piece of text with either two asterisks (**), or two underscores (__). For example **Bold text**.
  • Text strikethrough, by surrounding a piece of text with two tildes (~~), for example ~~Strikethrough text~~.
  • Inline code, marked with a backtick on either site of the code.
  • Code blocks, marked with three backticks both above and below the block.
  • Links, using the following syntax: [Title](url).
  • Images, using the following syntax: ![Alt text](image-url).
  • Both images and links can also use reference URLs, which can be defined anywhere in a Markdown document using this syntax: [referenceName]: url.
  • Both ordered lists (using numbers followed by a period (.) or right parenthesis ()) as bullets) and unordered lists (using either a dash (-), plus (+), or asterisk (*) as bullets) are supported.
  • Ordered lists start from the index of the first entry
  • Nested lists are supported as well, by indenting any part of a list that should be nested within its parent.
  • Horizontal lines can be placed using either three asterisks (***) or three dashes (---) on a new line.
  • HTML can be inlined both at the root level, and within text paragraphs.
  • Blockquotes can be created by placing a greater-than arrow at the start of a line, like this: > This is a blockquote.

Please note that, being a very young implementation, Ink does not fully support all Markdown specs, such as CommonMark. Ink definitely aims to cover as much ground as possible, and to include support for the most commonly used Markdown features, but if complete CommonMark compatibility is what you’re looking for — then you might want to check out tools like CMark.

Internal architecture

Ink uses a highly modular rule-based internal architecture, to enable new rules and formatting options to be added without impacting the system as a whole.

Each Markdown fragment is individually parsed and rendered by a type conforming to the internal Readable and HTMLConvertible protocols — such as FormattedText, List, and Image.

To parse a part of a Markdown document, each fragment type uses a Reader instance to read the Markdown string, and to make assertions about its structure. Errors are used as control flow to signal whether a parsing operation was successful or not, which in turn enables the parent context to decide whether to advance the current Reader instance, or whether to rewind it.

A good place to start exploring Ink’s implementation is to look at the main MarkdownParser type’s parse method, and to then dive deeper into the various Fragment implementations, and the Reader type.

GitHub