MarkdownText

repository·main·Indexed 41 days ago

https://github.com/shaps80/markdowntext

A native SwiftUI library for rendering Markdown text in iOS and macOS applications. It features customizable styling APIs, support for standard Markdown elements, and a LazyMarkdownText variant for performance-intensive tasks.

Tokens
795
Snippets
4
Records
5
Agent score
50%

What's inside MarkdownText

  1. Render Markdown text with MarkdownText and LazyMarkdownText

    main

    To render Markdown in your SwiftUI views, use MarkdownText for standard text or LazyMarkdownText for large amounts of content where improved scrolling and loading performance is required.

    // Standard usage
    MarkdownText("Some **markdown** text")
    
    // For massive markdown content
    LazyMarkdownText(someMassiveMarkdownText)
    MarkdownText("Some **markdown** text")
    LazyMarkdownText(someMassiveMarkdownText)
  2. Install MarkdownText via Swift Package Manager

    main

    The preferred method for installation is using Swift Package Manager. Add the following dependency to your Package.swift file:

    .package(url: "https://github.com/shaps80/MarkdownText.git", .upToNextMinor(from: "1.0.0"))

    Alternatively, you can install the library manually by copying the files located in the Sources directory into your project.

    .package(url: "https://github.com/shaps80/MarkdownText.git", .upToNextMinor(from: "1.0.0"))
  3. Apply Markdown modifiers for styling and visibility

    main

    You can apply styling and visibility modifiers directly to the MarkdownText view within your SwiftUI hierarchy.

    Styling Modifiers

    • .markdownQuoteStyle(.inset)
    • .markdownOrderedListBulletStyle(.tinted)
    • .markdownImageStyle(.animated)

    Visibility Modifiers

    • .markdownCode(.visible)
    • .markdownThematicBreak(.hidden)
    NavigationView {
        MarkdownText(markdown)
    }
    .markdownQuoteStyle(.inset)
    .markdownOrderedListBulletStyle(.tinted)
    .markdownImageStyle(.animated)
    .markdownCode(.visible)
    .markdownThematicBreak(.hidden)
  4. Customize Markdown element styles

    main

    MarkdownText uses SwiftUI-style APIs to allow customization of markdown elements. You can implement custom styles by conforming to specific style protocols (e.g., UnorderedListBulletMarkdownStyle, ImageMarkdownStyle) and providing a makeBody(configuration:) method.

    Customizing Bullets

    struct CustomUnorderedBullets: UnorderedListBulletMarkdownStyle {
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .foregroundColor(.blue)
        }
    }

    Customizing Image Animations

    struct ScaledImageStyle: ImageMarkdownStyle {
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .transition(.scale)
        }
    }
    struct CustomUnorderedBullets: UnorderedListBulletMarkdownStyle {
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .foregroundColor(.blue)
        }
    }
    
    struct ScaledImageStyle: ImageMarkdownStyle {
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .transition(.scale)
        }
    }
  5. Supported Markdown elements

    main

    MarkdownText supports the following Markdown syntax:

    • Headings
    • Paragraphs
    • Quotes
    • Inline formatting:
      • Strong/Bold
      • Emphasis/Italic
      • Strikethrough
      • Code
      • Links (non-interactive only)
    • Lists:
      • Ordered
      • Unordered
      • Checklist (GitHub style)
    • Thematic Breaks
    • Code Blocks
    • Images (includes a full backport of AsyncImage)