Swiftpack.co - Package - gonzalezreal/MarkdownUI
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

MarkdownUI

CI Twitter: @gonzalezreal

MarkdownUI is a library for rendering Markdown in SwiftUI, fully compliant with the CommonMark Spec.

Supported Platforms

You can use the Markdown SwiftUI view in the following platforms:

  • macOS 11.0+
  • iOS 14.0+
  • tvOS 14.0+

The NSAttributedString extension is available in:

  • macOS 10.12+
  • iOS 11.0+
  • tvOS 11.0+
  • watchOS 3.0+

Usage

You can create a Markdown view in SwiftUI by providing a CommonMark document.

Markdown(
    #"""
    It's very easy to make some words **bold** and other words *italic* with Markdown.

    **Want to experiment with Markdown?** Play with the [reference CommonMark
    implementation](https://spec.commonmark.org/dingus/).
    """#
)

Markdown text

A Markdown view renders text using a body font appropriate for the current platform. You can choose a different font or customize other properties like the foreground color, paragraph spacing, or heading styles using the markdownStyle(_:) view modifier.

Markdown(
    #"""
    ## Inline code
    If you have inline code blocks, wrap them in backticks: `var example = true`.
    """#
)
.markdownStyle(
    MarkdownStyle(
        font: .system(.body, design: .serif),
        codeFontName: "Menlo",
        codeFontSize: .em(0.88)
    )
)

Markdown style

A Markdown view always uses all the available width and adjusts its height to fit its rendered text.

Use the accentColor(_:) view modifier to configure the link color.

Markdown("Play with the [reference CommonMark implementation](https://spec.commonmark.org/dingus/).")
    .accentColor(.purple)

Use modifiers like lineLimit(_:) and truncationMode(_:) to configure how the view handles space constraints.

Markdown("> Knowledge is power, Francis Bacon.")
    .lineLimit(1)

You can set the alignment of the text by using the multilineTextAlignment(_:) view modifier.

Markdown(
    #"""
    There are many different ways to style code with CommonMark. If you
    have inline code blocks, wrap them in backticks: `var example = true`.
    """#
)
.multilineTextAlignment(.trailing)

Text alignment

Using the NSAttributedString Extension

If you are not yet using SwiftUI, you can use the NSAttributedString extension to render Markdown in your app.

let attributedString = NSAttributedString(
    document: #"""
    It's very easy to make some words **bold** and other words *italic* with Markdown.
    """#,
    style: MarkdownStyle(font: .system(.body))
)

Supported Markdown Elements

MarkdownUI uses the CommonMark reference parser and it is fully compliant with the CommonMark Spec.

You can explore all the capabilities of this package in the companion demo project.

Demo

Installation

You can add MarkdownUI to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter https://github.com/gonzalezreal/MarkdownUI into the package repository URL text field
  3. Link MarkdownUI to your application target

Other Libraries

Github

link
Stars: 116

Releases

MarkdownUI 0.3.3 - 2021-01-16T20:21:17

  • Add support for macCatalyst
  • Update view when document changes

MarkdownUI 0.3.2 - 2021-01-14T17:42:32

  • Update NetworkImage dependency to 2.1.0

MarkdownUI 0.3.1 - 2021-01-13T17:06:48

  • Update NetworkImage dependency to 2.0.0

MarkdownUI 0.3.0 - 2021-01-08T17:33:55

  • Scale images to fit the Markdown view
  • Fix build for Mac Catalyst environment
  • Update AttributedText dependency to 0.2.1. This fixes the layout issues in macOS.
  • Add support for relative URLs in markdown images, using the markdownBaseURL view modifier.
  • Add initializers to create a Document with the content of the file at a given path.

MarkdownUI 0.2.0 - 2021-01-03T18:29:04

  • Use AttributedText 0.2.0, which improves how the view height adapt to its contents
  • Add Markdown snapshot tests, replacing the renderer tests (and thus fixing #20)

MarkdownUI 0.1.1 - 2020-12-31T09:27:35

  • Use a version-based requirement for cmark dependency to fix package resolution
  • Use AttributedText 0.1.6

MarkdownUI 0.1.0 - 2020-12-29T11:37:44