MarkdownGenerator

Release Swift 4.0+ Build Status CI codecov Swift Package Manager Compatible Linux Compatible

一个用于生成 Markdown 文档的小型 Swift 库。

特性

MarkdownConvertible

遵循 MarkdownConvertible 协议的类型可以通过实现 .markdown 计算属性来渲染为 markdown 字符串。

开箱即用,MarkdownGenerator 提供了以下 Markdown 元素

请查看以下示例,或阅读参考文档

列表

列表可以嵌套到任何级别,并且包含单行或多行项目。列表可以是有序的、无序的或混合的。

let list = MarkdownList(items: ["🍏", "🍌", "🍊", "🍇"])
print(list.markdown)

生成以下输出

-   🍏
-   🍌
-   🍊
-   🍇

渲染为

表格

虽然 Markdown 最初不支持表格,但大多数现代 Markdown 阅读器(包括 GitHub)现在可以正确渲染表格。

let data: [[String]] = [
    ["🍏", "Apple", "Fruits"],
    ["🍊", "Orange", "Fruits"],
    ["🥖", "Bread", "Bakery"],
]
let table = MarkdownTable(headers: ["", "Name", "Department"], data: data)
print(table.markdown)

生成以下输出

|    | Name   | Department |
| -- | ------ | ---------- |
| 🍏 | Apple  | Fruits     |
| 🍊 | Orange | Fruits     |
| 🥖 | Bread  | Bakery     |

渲染为

名称 部门
🍏 苹果 水果
🍊 橙子 水果
🥖 面包 面包店

漂亮的表格 🎉

块引用

任何 MarkdownConvertible 内容(包括 String)都可以轻松地进行 .blockquoted

let input = """
## This is a header.

1.   This is the first list item.
2.   This is the second list item.

Here's some example code:

    return shell_exec("echo $input | $markdown_script");

> This is a quote.
"""

print(input.blockquoted.markdown)

生成以下输出

> ## This is a header.
>
> 1.   This is the first list item.
> 2.   This is the second list item.
>
> Here's some example code:
>
>     return shell_exec("echo $input | $markdown_script");
>
> > This is a quote.

渲染为

这是一个标题。

  1. 这是第一个列表项。
  2. 这是第二个列表项。

这是一个示例代码

return shell_exec("echo $input | $markdown_script");

这是一个引用。

可折叠块

可折叠块在 GitHub 和其他 Markdown 查看器上看起来很棒。 提供详细内容而不会使输出混乱的好方法。

let details: [MarkdownConvertible] = [
    MarkdownHeader(title: "Title"),
    MarkdownList(items: ["🐶", "🐱", "🦊"]),
    MarkdownTable(headers: ["Name", "Count"], data: [["Dog", "1"], ["Cat", "2"]]),
    MarkdownCodeBlock(code: "let foo = Bar()", style: .backticks(language: "swift"))
]

print(MarkdownCollapsibleSection(summary: "This is cool stuff", details: details).markdown)

生成以下输出

<details><summary>This is cool stuff</summary>

# Title

-   🐶
-   🐱
-   🦊

| Name | Count |
| ---- | ----- |
| Dog  | 1     |
| Cat  | 2     |

```swift
let foo = Bar()
```
</details>

渲染为(点击展开)

这很酷

标题

名称 数量
1
2
let foo = Bar()

联系方式

在 Twitter 上关注我和/或联系我:@eneko

贡献

如果您发现问题,只需打开一个 issue。也非常欢迎 Pull Request。

许可

MarkdownGenerator 在 Apache 2.0 许可下发布。 有关更多信息,请参见LICENSE