chojnac/Dumpling
Customisable and extensible Markdown parser written in Swift
Features
- 100% Swift
- Easy to extend with new markdown tags, output formats or customise existing implementations.
- Intermediate layer describing document structure with Abstract Syntax Tree
- Build-in support for rendering outputs as the Plain Text, HTML, NSAttributedString.
- Multiplatform - iOS, macOS, Linux.
Internal architecture
Dumpling uses a highly extensible and modular functional approach called parser combinator.
Markdown parser produces an intermediate data model called Abstract Syntax Tree (AST).
Renderer implementations use AST to produce the final output. Dumpling provides 3 main build-in output formats - Plain Text, HTML and NSAttributedString.
Supported Elements
*italic* or _italics_
**bold** or __bold__
~~strikethrough~~
# Header 1Header 2
Header 3
Header 4
Header 5
Header 6
Horizontal lines (Thematic breaks)
- *
- List
- List
- List
- Ordered
- Lists with style
Inline code or
block codeBlockquotes
and nested blockquote.
With nested content
- This is the first list ~~item~~.
- This is the second list item.
Please note that the goal of this project was to create a foundation for a flexible Markdown parser. Dumpling doesn't fully support all Markdown specs. This is a very young implementation so it covers only the most common set of Markdown features.Installation
CocoaPods
pod 'Dumpling'Carthage
github "chojnac/Dumpling.git" ~> 0.3.0Swift Package Manager
dependencies: [
.package(url: "https://github.com/chojnac/Dumpling.git", .upToNextMajor(from: "0.3.0"))
]Usage
Basic usage
Parse text and produce html:
import Dumpling
let text = "**Lorem ipsum**"
let html = Markdown().parse(text).renderHTML()or NSAttributedText:
import Dumpling
let text = "**Lorem ipsum**"
let string = Markdown().parse(text).renderAttributedString()Change NSAttributedText style
import Dumpling
let theme = AttributedStringTheme(
baseFont: .systemFont(ofSize: 13),
color: .blue
)
theme.addStyle(
forKey: .strong,
style: StringStyle.foregroundColor(Color.red)
)
theme.addStyle(
forKey: .em,
style: compose(
StringStyle.foregroundColor(Color.green),
StringStyle.traits([.italic])
)
)
let string = Markdown()
.parse(text)
.renderAttributedString(theme: theme)You will find more advanced examples in the project example playground
License
Dumpling is available under the MIT license. See the LICENSE file for more info.
Markdown test document used in the project based on Max Stoiber's Markdown Test File
Package Metadata
Repository: chojnac/Dumpling
Stars: 47
Forks: 3
Open issues: 1
Default branch: main
Primary language: swift
License: MIT
Topics: customizable, markdown, parser, swift
README: README.md