Kotlin Multiplatform Markdown Renderer

repository·develop·Indexed 21 days ago

https://github.com/mikepenz/multiplatform-markdown-renderer

A Markdown rendering library for Compose Multiplatform supporting Android, iOS, Desktop, and Web. It features deep integration with Material 2 and Material 3 themes, support for rich Markdown elements, syntax highlighting via the Highlights project, and image loading integration with Coil 2 and Coil 3. The library provides optimized rendering for large documents through lazy loading and flexible parsing options via rememberMarkdownState, parseMarkdownFlow, and parseMarkdown.

Tokens
8.6K
Snippets
34
Records
41
Agent score
77%

What's inside multiplatform-markdown-renderer

  1. Overview of Kotlin Multiplatform Markdown Renderer features

    develop

    The library provides a powerful Markdown rendering solution for Compose Multiplatform projects. Key features include:

    • Cross-platform Support: Works on Android, iOS, Desktop, and Web.
    • Material Design Integration: Supports both Material 2 and Material 3.
    • Rich Markdown Support: Renders headings, lists, code blocks, tables, images, and more.
    • Syntax Highlighting: Optional support for various programming languages.
    • Image Loading: Integrates with Coil2 and Coil3.
    • Performance: Optimized with lazy loading support for large documents.
    • Customization: Extensive options for colors, typography, and components.
  2. Use custom placeholders in tables via MarkdownAnnotator

    develop

    You can use a custom MarkdownAnnotator combined with MarkdownInlineContent to substitute specific tokens with custom UI elements (like glyphs or icons) both inside and outside of table cells.

    For example, a token like ⦃check⦄ can be intercepted and rendered as a ✅ glyph within a table cell to indicate status.

  3. Use inline images within table cells

    develop

    Images placed inside table cells are rendered as inline content, adhering to the GFM specification which prohibits block-level content within cells. This allows for:

    • Single Images: Rendering an avatar or profile image via ![alt](url).
    • Mixed Content: Combining an image with surrounding text within the same cell (e.g., ![logo](url) text).
  4. Support inline content within GFM tables

    develop

    The renderer supports GitHub Flavored Markdown (GFM) tables containing a full set of inline content within cells. This includes:

    • Text Styles: Bold (**text**), Italic (*text* or _text_), and Strikethrough (~~text~~).
    • Code: Inline code blocks using backticks (`code`).
    • Links: Standard Markdown links ([text](url)), autolinks (<url>), and GFM autolinks (https://url).
    • Escaping: You can include escaped pipes (\|) within table cells to prevent them from being interpreted as column delimiters.
    • Mixed Content: Cells can combine multiple inline elements, such as bold text, code, and links simultaneously.
  5. Install Kotlin Multiplatform Markdown Renderer via Gradle

    develop

    To use the library in a Kotlin Multiplatform project, you must include the core library and exactly one Material theme module (-m2 or -m3). Since version 0.13.0, the core library does not include default styling, so the theme module is required for default appearance.

    For JVM (Desktop) or Android-only projects, use the platform-specific dependencies instead of the multiplatform core.

    // For Multiplatform projects
    dependencies {
        // Core library
        implementation("com.mikepenz:multiplatform-markdown-renderer:${version}")
    
        // Choose ONE based on your Material theme:
        // For Material 2
        implementation("com.mikepenz:multiplatform-markdown-renderer-m2:${version}")
    
        // OR for Material 3
        // implementation("com.mikepenz:multiplatform-markdown-renderer-m3:${version}")
    }
    
    // For JVM (Desktop) only
    dependencies {
        implementation("com.mikepenz:multiplatform-markdown-renderer-jvm:${version}")
    }
    
    // For Android only
    dependencies {
        implementation("com.mikepenz:multiplatform-markdown-renderer-android:${version}")
    }
  6. Basic usage of the Markdown composable

    develop

    The simplest way to render markdown is to pass a markdown string directly to the Markdown composable.

    Important: Depending on your application's theme, you must import the correct implementation:

    • For Material 3: com.mikepenz.markdown.m3.Markdown
    • For Material 2: com.mikepenz.markdown.m2.Markdown

    By default, changing the content will trigger a loading state while the new content is parsed. To prevent this and keep the previous content visible during updates, set retainState = true.

    // In your composable (use the appropriate Markdown implementation for your theme)
    Markdown(
        """
        # Hello Markdown
    
        This is a simple markdown example with:
    
        - Bullet points
        - **Bold text**
        - *Italic text*
    
        [Check out this link](https://github.com/mikepenz/multiplatform-markdown-renderer)
        """.trimIndent()
    )
  7. Configure Image Loading (Coil)

    develop

    The library supports various image loading implementations. To use Coil, you must add the specific dependency for your version and pass the corresponding imageTransformer to the Markdown composable.

    For Coil 3:

    • Dependency: com.mikepenz:multiplatform-markdown-renderer-coil3:${version}
    • Transformer: Coil3ImageTransformerImpl

    For Coil 2:

    • Dependency: com.mikepenz:multiplatform-markdown-renderer-coil2:${version}
    • Transformer: Coil2ImageTransformerImpl
    // Coil 3 Example
    Markdown(
        MARKDOWN,
        imageTransformer = Coil3ImageTransformerImpl,
    )