Parma

使用纯 SwiftUI 组件显示 Markdown。利用 ViewBuilderTextView 创建自定义外观。

示例

import Parma

struct ContentView: View {
    var markdown = "I'm **Strong**."
    
    var body: some View {
        Parma(markdown)
    }
}

更多示例请参考 demo 应用。

Markdown 支持

已支持

未来版本可能支持

不支持

安装

要求

Swift Package Manager

Swift Package Manager 是一种用于管理 Swift 代码分发的工具。它与 Swift 构建系统集成,可以自动执行在所有平台上下载、编译和链接依赖项的过程。

通过 使用 Xcode 的 GUIParma 添加为依赖项,package url 为 https://github.com/dasautoooo/Parma

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要使用 CocoaPods 将 Parma 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod 'Parma'

外观定制

要自定义 Text 样式和 View,请创建一个符合协议 ParmaRenderable 的新渲染器,并且仅重新实现那些适合您目的的渲染器。最后,创建 Parma 视图时,将自定义渲染器指定为新的渲染器。

import Parma

struct ContentView: View {
    var markdown = "I'm **Strong**."
    
    var body: some View {
        Parma(markdown, render: MyRender())
    }
}

struct MyRender: ParmaRenderable {
    ...
}

有一个 DemoApp 修改了以下一些委托方法,供大家参考。

/// Define the heading text style.
/// - Parameters:
///   - level: The level of heading.
///   - textView: The textView generated from captured heading string.
func heading(level: HeadingLevel?, textView: Text) -> Text

/// Define the paragraph text style.
/// - Parameter text: The text string captured from paragraph.
func paragraph(text: String) -> Text

/// Define the text style for plain text. Do NOT recommend to alter this if there's no special purpose.
/// - Parameter text: The text string captured from markdown.
func plainText(_ text: String) -> Text

/// Define the strong text style.
/// - Parameter textView: The textView generated from captured strong string.
func strong(textView: Text) -> Text

/// Define the emphasis text style.
/// - Parameter textView: The textView generated from captured emphasis string.
func emphasis(textView: Text) -> Text

/// Define the link text style.
/// - Parameters:
///   - textView: The textView generated from captured link string.
///   - destination: The destination of the link.
func link(textView: Text, destination: String?) -> Text

/// Define the code text style.
/// - Parameter text: The text string captured from code.
func code(_ text: String) -> Text

/// Define the style of heading view.
/// - Parameters:
///   - level: The level of heading.
///   - view: The view contains heading text.
func headingBlock(level: HeadingLevel?, view: AnyView) -> AnyView

/// Define the style of paragraph view.
/// - Parameter view: The view contains view(s) which belong(s) to this paragraph.
func paragraphBlock(view: AnyView) -> AnyView

/// Define the style of list item.
/// - Parameter attributes: Attributes of the list containing the item. Those must be considered for proper item rendering.
/// - Parameter index: Normalized index of the list item. For exemple, the index of the third item of a one level list would be `[2]` and the second item of a sublist appearing fourth in it's parent list would be `[3, 1]`.
/// - Parameter view: The view contains view(s) which belong(s) to this item.
func listItem(attributes: ListAttributes, index: [Int], view: AnyView) -> AnyView

/// Define the style of image view.
/// - Parameter urlString: The url string for this image view.
/// - Parameter altTextView: The view contains alt text.
func imageView(with urlString: String, altTextView: AnyView?) -> AnyView

名称来源

Parma 是意大利北部的一个城市,以其建筑、音乐和艺术而闻名。选择这个城市名称作为项目名称的原因是 Giambattista Bodoni,一位著名的排版设计师,他一生的大部分时间都在这座城市生活和工作。

Bodoni 是帕尔马的一位意大利排版设计师,字体设计师。在他的一生中,他设计了许多字体,现在被称为 Bodoni。每台 Mac 都安装了 Bodoni 字体,并且可以免费使用。

鸣谢

该软件包构建于 Down 之上,Down 是一个 Swift 中的 markdown 解析器。