Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/SwiftDocOrg/CommonMark

Create, parse, and render Markdown text according to the CommonMark specification
https://github.com/SwiftDocOrg/CommonMark

cmark commonmark markdown swift

Last synced: about 2 months ago
JSON representation

Create, parse, and render Markdown text according to the CommonMark specification

Awesome Lists containing this project

README

        

# CommonMark

![CI][ci badge]
[![Documentation][documentation badge]][documentation]

A Swift package for working with [CommonMark][commonmark] text.
It's built on top of [libcmark][cmark]
and fully compliant with the [CommonMark Spec][commonmark].

## Usage

```swift
import CommonMark

let markdown = #"""
# [Universal Declaration of Human Rights][udhr]

## Article 1.

All human beings are born free and equal in dignity and rights.
They are endowed with reason and conscience
and should act towards one another in a spirit of brotherhood.

[udhr]: https://www.un.org/en/universal-declaration-human-rights/ "View full version"
"""#

let document = try Document(markdown)
```

### Inspecting Document Nodes

```swift
document.children.count // 3

let heading = document.children[0] as! Heading
heading.headerLevel // 1
heading.children.count // 1

let link = heading.children[0] as! Link
link.urlString // "https://www.un.org/en/universal-declaration-human-rights/")
link.title // "View full version"

let subheading = document.children[1] as! Heading
subheading.headerLevel // 2
subheading.children.count // 1

let subheadingText = subheading.children[0] as! Text
subheadingText.literal // "Article 1."

let paragraph = document.children[2] as! Paragraph
paragraph.description // "All human beings [ ... ]"
paragraph.range.lowerBound // (line: 5, column: 1)
paragraph.range.upperBound // (line: 7, column: 62)
```

### Rendering to HTML, XML, LaTeX, and Manpage

```swift
let html = document.render(format: .html) //

[ ... ]
let xml = document.render(format: .xml) //