饺子 (Dumpling)

Build Status Version License Platform Carthage compatible

饺子 (Dumpling) 是一个纯 Swift 编写的,可定制和可扩展的 Markdown 解析器,适用于 iOS、macOS 和 Linux。

特性

内部架构

饺子 (Dumpling) 使用一种高度可扩展和模块化的函数式方法,称为解析器组合子 (parser combinator)。

Markdown 解析器生成一个名为抽象语法树 (AST) 的中间数据模型。

渲染器实现使用 AST 来生成最终输出。 饺子 (Dumpling) 提供了 3 种主要的内置输出格式 - 纯文本 (Plain Text)、HTML 和 NSAttributedString。

支持的元素

*italic* or _italics_ 
**bold** or __bold__ 
~~strikethrough~~ 

# Header 1
## Header 2
### Header 3
#### Header 4
##### Header 5
###### Header 6

Horizontal lines (Thematic breaks)

***
---
___
* * *   *
-----------------


* List
  - List
+ List

1. Ordered 
1. Lists with **style**

Inline `code` or 
``` 
block code
```
[Links](http://github.com/chojnac/)

> Blockquotes
> > and  nested blockquote.
>
> ## With nested content 
> 1.   This is the **first** list ~~item~~.
> 2.   This is the *second* list item.

请注意,该项目的目标是为灵活的 Markdown 解析器创建一个基础。 饺子 (Dumpling) 并未完全支持所有 Markdown 规范。 这是一个非常年轻的实现,因此仅涵盖最常见的 Markdown 功能集。

安装

CocoaPods

pod 'Dumpling'

Carthage

github "chojnac/Dumpling.git" ~> 0.3.0

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/chojnac/Dumpling.git", .upToNextMajor(from: "0.3.0"))
]

用法

基本用法

解析文本并生成 HTML

import Dumpling 

let text = "**Lorem ipsum**"
let html = Markdown().parse(text).renderHTML()

或 NSAttributedText

import Dumpling 

let text = "**Lorem ipsum**"
let string = Markdown().parse(text).renderAttributedString()

更改 NSAttributedText 样式

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)

您将在项目示例 playground中找到更高级的示例

许可证

饺子 (Dumpling) 在 MIT 许可证下可用。 有关更多信息,请参见LICENSE文件。

项目中使用的 Markdown 测试文档基于 Max Stoiber 的 Markdown 测试文件