HTML Converter Compose

repository·main·Indexed 41 days ago

https://github.com/cbeyls/htmlconvertercompose

A Compose Multiplatform library for converting HTML strings into AnnotatedString, supporting styling, paragraphs, and hyperlinks across JVM, Native, and Web platforms. Features include CSS colored text support, custom link interaction listeners, and support for a wide range of inline, block, and list HTML tags.

Tokens
1.5K
Snippets
7
Records
9
Agent score
0%

What's inside HTML Converter Compose

  1. Enable CSS colored text

    main

    By default, CSS color properties are ignored to respect the Compose theme. To enable support for foreground (color) and background (background, background-color) CSS properties, set isTextColorEnabled = true in your HtmlStyle.

    Supported Formats:

    • All CSS level 4 named colors (case insensitive).
    • Hexadecimal RGB (#RRGGBB, #RRGGBBAA, #RGB, #RGBA).

    Note: rgb(...), rgba(...), hsl(...), and hsla(...) are not supported. If a color is defined on a link, it overrides the textLinkStyles color.

    Supported Inline Tags: span, strong, b, em, cite, dfn, i, big, small, tt, code, a, u, del, s, strike, sup, sub.

    val convertedText = remember(html) {
        htmlToAnnotatedString(
            html,
            style = HtmlStyle(isTextColorEnabled = true)
        )
    }
    Text(text = convertedText)
  2. Install the HTML Converter dependency

    main

    Add the following dependency to your module's build.gradle or build.gradle.kts file to use the library in a standard project:

    dependencies {
        implementation("be.digitalia.compose.htmlconverter:htmlconverter:1.1.1")
    }

    For Kotlin Multiplatform projects, add it to your commonMain source set:

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("be.digitalia.compose.htmlconverter:htmlconverter:1.1.1")
            }
        }
    }
  3. Workaround for Compose 1.7 maxLines bug

    main

    Compose UI versions 1.7.0 through 1.7.6 have a bug that causes crashes when using maxLines in a Text composable that displays an AnnotatedString containing a LinkAnnotation inside a paragraph.

    Solutions:

    1. Upgrade to Compose 1.8.0 or newer.
    2. If you must stay on Compose 1.7.x, disable indentation support in your HtmlStyle to avoid the crash.
    val convertedText = remember(html) {
        htmlToAnnotatedString(
            html,
            style = HtmlStyle(indentUnit = TextUnit.Unspecified)
        )
    }
    Text(
        text = convertedText,
        maxLines = 3
    )
  4. Convert HTML to AnnotatedString for styled text

    main

    Use htmlToAnnotatedString(html) to convert an HTML string into a Compose AnnotatedString. This allows you to display styled text (including paragraphs and links) using a Text composable.

    Best Practice: When calling this inside a @Composable function, wrap the call in remember() to cache the result and avoid re-parsing the HTML on every recomposition.

    Text(text = remember(html) { htmlToAnnotatedString(html) })
  5. Configure HTML conversion with HtmlStyle

    main

    The htmlToAnnotatedString() function accepts an optional style parameter of type HtmlStyle to customize the output.

    Available HtmlStyle options:

    • textLinkStyles: An optional collection of styles for <a> tags. If set to null, hyperlinks will not be styled. Default is a simple underline.
    • indentUnit: The indentation unit for block quotations and nested lists. Default is 24.sp. Set to 0.sp or TextUnit.Unspecified to disable indentation. Note: em units are not supported on Compose Desktop.
    • isTextColorEnabled: A boolean to enable or disable CSS colored text support. Default is false to ensure theme compatibility.
    val convertedText = remember(html, linkColor) {
        htmlToAnnotatedString(
            html,
            style = HtmlStyle(
                textLinkStyles = TextLinkStyles(
                    style = SpanStyle(color = linkColor)
                )
            )
        )
    }
  6. Handle hyperlink clicks

    main

    By default, hyperlinks (<a> tags) are annotated with LinkAnnotation.Url and use the platform's default UriHandler to open the URL in a browser.

    To implement custom navigation or disable clicks, provide a linkInteractionListener to htmlToAnnotatedString().

    val convertedText = remember(html) {
        htmlToAnnotatedString(
            html,
            linkInteractionListener = { link ->
                if (link is LinkAnnotation.Url) {
                    navigateTo(link.url)
                }
            }
        )
    }
    Text(text = convertedText)
  7. Implement custom HTML parsing

    main
    Both htmlToAnnotatedString() and htmlToString() provide overloads that accept an HTMLParser instead of a String. Implement the HTMLParser interface to provide HTML from sources other than a standard string, such as a character stream or a binary format.
  8. Supported HTML tags reference

    main

    The library supports a wide range of HTML tags:

    • Inline (with styling): strong, b, em, cite, dfn, i, big, small, tt, code, a, u, del, s, strike, sup, sub, span (via CSS).
    • Block (paragraphs): p, blockquote, pre, div, header, footer, main, nav, aside, section, article, address, figure, figcaption, video, audio.
    • Lists: ul, ol, li, dl, dt, dd.
    • Headings: h1, h2, h3, h4, h5, h6.
    • Other: hr (marks a new paragraph), br (line break).

    Skipped Tags (content is also removed): script, head, table, form, fieldset. Ignored Tags: Other tags are ignored but their text content is preserved.