openai/compose-richtext
Publicmirrored fromhttps://github.com/openai/compose-richtextAvailable
docs/richtext-markdown.md
52lines · modecode
| 1 | # Markdown |
| 2 | |
| 3 | [](https://developer.android.com/studio/build/dependencies) |
| 4 | [](https://kotlinlang.org/docs/mpp-intro.html) |
| 5 | |
| 6 | Library for rendering Markdown tree that is defined as an `AstNode`. This module would be useless |
| 7 | for someone who is looking to just render a Markdown string. Please check out |
| 8 | `richtext-commonmark` for such features. `richtext-markdown` behaves as sort of a building block. |
| 9 | You can create your own parser or use 3rd party ones that converts any Markdown string to an |
| 10 | `AstNode` tree. |
| 11 | |
| 12 | ## Gradle |
| 13 | |
| 14 | ```kotlin |
| 15 | dependencies { |
| 16 | implementation("com.halilibo.compose-richtext:richtext-markdown:${richtext_version}") |
| 17 | } |
| 18 | ``` |
| 19 | |
| 20 | ## Rendering |
| 21 | |
| 22 | The simplest way to render markdown is just pass an `AstNode` to the [`BasicMarkdown`](../api/richtext-markdown/com.halilibo.richtext.markdown/-basic-markdown.html) |
| 23 | composable under RichText scope: |
| 24 | |
| 25 | ~~~kotlin |
| 26 | RichText( |
| 27 | modifier = Modifier.padding(16.dp) |
| 28 | ) { |
| 29 | // requires richtext-commonmark module. |
| 30 | val parser = remember(options) { CommonmarkAstNodeParser(options) } |
| 31 | val astNode = remember(parser) { |
| 32 | parser.parse( |
| 33 | """ |
| 34 | # Demo |
| 35 | |
| 36 | Emphasis, aka italics, with *asterisks* or _underscores_. Strong emphasis, aka bold, with **asterisks** or __underscores__. Combined emphasis with **asterisks and _underscores_**. [Links with two blocks, text in square-brackets, destination is in parentheses.](https://www.example.com). Inline `code` has `back-ticks around` it. |
| 37 | |
| 38 | 1. First ordered list item |
| 39 | 2. Another item |
| 40 | * Unordered sub-list. |
| 41 | 3. And another item. |
| 42 | You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown). |
| 43 | |
| 44 | * Unordered list can use asterisks |
| 45 | - Or minuses |
| 46 | + Or pluses |
| 47 | """.trimIndent() |
| 48 | ) |
| 49 | } |
| 50 | BasicMarkdown(astNode) |
| 51 | } |
| 52 | ~~~ |
| 53 | |