Skip to content

Commit 811dd55

Browse files
authored
Merge pull request #404 from mikepenz/feature/cleanup
Project cleanup / refinement
2 parents 3494d9b + 5f0d68f commit 811dd55

File tree

3 files changed

+181
-63
lines changed

3 files changed

+181
-63
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@
2929

3030
### What's included 🚀
3131

32-
- Super simple setup
33-
- Cross-platform ready
34-
- Lightweight
32+
- **Cross-platform Markdown Rendering** - Works on Android, iOS, Desktop, and Web
33+
- **Material Design Integration** - Seamless integration with Material 2 and Material 3 themes
34+
- **Rich Markdown Support** - Renders headings, lists, code blocks, tables, images, and more
35+
- **Syntax Highlighting** - Optional code syntax highlighting for various programming languages
36+
- **Image Loading** - Flexible image loading with Coil2 and Coil3 integration
37+
- **Customization Options** - Extensive customization for colors, typography, components, and more
38+
- **Performance Optimized** - Efficient rendering with lazy loading support for large documents
39+
- **Extended Text Spans** - Support for advanced text styling with extended spans
40+
- **Lightweight** - Minimal dependencies and optimized for performance
3541

3642
-------
3743

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/Markdown.kt

Lines changed: 160 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,48 @@ import org.intellij.markdown.parser.MarkdownParser
3737

3838

3939
/**
40-
* Renders the markdown content.
41-
*
42-
* @param content The markdown content to be rendered.
43-
* @param colors The colors to be used for rendering.
44-
* @param typography The typography to be used for rendering.
45-
* @param modifier The modifier to be applied to the container.
46-
* @param padding The padding to be applied to the container.
47-
* @param dimens The dimensions to be used for rendering.
48-
* @param flavour The flavour descriptor for parsing the markdown. By default uses GFM flavour.
49-
* @param parser The parser to be used for parsing the markdown. By default uses the flavour supplied.
50-
* @param imageTransformer The image transformer to be used for rendering images.
51-
* @param annotator The annotator to be used for rendering annotations.
52-
* @param extendedSpans The extended spans to be used for rendering.
53-
* @param inlineContent The inline content to be rendered.
54-
* @param components The components to be used for rendering.
55-
* @param referenceLinkHandler The reference link handler to be used for handling links.
56-
* @param animations The animations to be used for rendering.
57-
* @param loading A composable function to be displayed while loading the content.
58-
* @param success A composable function to be displayed with the markdown content. It receives the modifier, state and components as parameters. By default this is a [Column].
59-
* @param error A composable function to be displayed in case of an error. Only really possible if assertions are enabled on the parser)
40+
* Renders the provided markdown content string using Jetpack Compose.
41+
*
42+
* This is the primary entry point for rendering markdown content in your application.
43+
* It handles parsing the markdown string, applying styling, and rendering the content
44+
* with appropriate components.
45+
*
46+
* The rendering process follows these steps:
47+
* 1. Parse the markdown content using the provided parser
48+
* 2. Create a state object to manage the rendering process
49+
* 3. Apply styling and customization options
50+
* 4. Render the content using the appropriate components
51+
*
52+
* Example usage:
53+
* ```
54+
* Markdown(
55+
* content = "# Hello World\n\nThis is a **bold** statement.",
56+
* colors = markdownColor(),
57+
* typography = markdownTypography()
58+
* )
59+
* ```
60+
*
61+
* @param content The markdown content string to be rendered.
62+
* @param colors The color scheme to be used for rendering different markdown elements.
63+
* @param typography The typography settings to be used for text rendering.
64+
* @param modifier The modifier to be applied to the container. Defaults to [Modifier.fillMaxSize].
65+
* @param padding The padding configuration to be applied to different markdown elements.
66+
* @param dimens The dimension settings for spacing and sizing of markdown elements.
67+
* @param flavour The markdown flavor descriptor for parsing. By default uses GitHub Flavored Markdown (GFM).
68+
* @param parser The parser instance to be used for parsing the markdown. By default creates a new parser with the provided flavor.
69+
* @param imageTransformer The transformer for handling and loading images within markdown content.
70+
* @param annotator The annotator for adding custom annotations to the rendered content.
71+
* @param extendedSpans The configuration for extended text spans beyond standard markdown.
72+
* @param inlineContent The configuration for inline custom content within markdown.
73+
* @param components The custom components to be used for rendering different markdown elements.
74+
* @param referenceLinkHandler The handler for resolving reference-style links in markdown.
75+
* @param animations The animation configurations for interactive elements.
76+
* @param loading A composable function to be displayed while the content is being parsed and prepared.
77+
* @param success A composable function to be displayed with the successfully parsed markdown content.
78+
* It receives the state, components, and modifier as parameters.
79+
* By default, this renders the content in a [Column].
80+
* @param error A composable function to be displayed in case of parsing errors.
81+
* This is only triggered if assertions are enabled on the parser.
6082
*/
6183
@Composable
6284
fun Markdown(
@@ -108,23 +130,48 @@ fun Markdown(
108130
}
109131

110132
/**
111-
* Renders the markdown content.
112-
*
113-
* @param markdownState The markdown state to be rendered.
114-
* @param colors The colors to be used for rendering.
115-
* @param typography The typography to be used for rendering.
116-
* @param modifier The modifier to be applied to the container.
117-
* @param padding The padding to be applied to the container.
118-
* @param dimens The dimensions to be used for rendering.
119-
* @param imageTransformer The image transformer to be used for rendering images.
120-
* @param annotator The annotator to be used for rendering annotations.
121-
* @param extendedSpans The extended spans to be used for rendering.
122-
* @param inlineContent The inline content to be rendered.
123-
* @param components The components to be used for rendering.
124-
* @param animations The animations to be used for rendering.
125-
* @param loading A composable function to be displayed while loading the content.
126-
* @param success A composable function to be displayed with the markdown content. It receives the modifier, state and components as parameters. By default this is a [Column].
127-
* @param error A composable function to be displayed in case of an error. Only really possible if assertions are enabled on the parser)
133+
* Renders markdown content using a pre-created [MarkdownState] object.
134+
*
135+
* This overload is useful when you need more control over the state management
136+
* of your markdown content, such as when you want to:
137+
* - Pre-parse markdown content
138+
* - Share the same state across multiple composables
139+
* - Implement custom state handling logic
140+
*
141+
* The state object manages the parsing and preparation of the markdown content,
142+
* while this function handles the rendering with appropriate styling and components.
143+
*
144+
* Example usage:
145+
* ```
146+
* val markdownState = rememberMarkdownState(content = "# Hello World")
147+
*
148+
* Markdown(
149+
* markdownState = markdownState,
150+
* colors = markdownColor(),
151+
* typography = markdownTypography()
152+
* )
153+
* ```
154+
*
155+
* @param markdownState A [MarkdownState] object that manages the parsing and state of the markdown content.
156+
* @param colors The color scheme to be used for rendering different markdown elements.
157+
* @param typography The typography settings to be used for text rendering.
158+
* @param modifier The modifier to be applied to the container. Defaults to [Modifier.fillMaxSize].
159+
* @param padding The padding configuration to be applied to different markdown elements.
160+
* @param dimens The dimension settings for spacing and sizing of markdown elements.
161+
* @param imageTransformer The transformer for handling and loading images within markdown content.
162+
* @param annotator The annotator for adding custom annotations to the rendered content.
163+
* @param extendedSpans The configuration for extended text spans beyond standard markdown.
164+
* @param inlineContent The configuration for inline custom content within markdown.
165+
* @param components The custom components to be used for rendering different markdown elements.
166+
* @param animations The animation configurations for interactive elements.
167+
* @param loading A composable function to be displayed while the content is being parsed and prepared.
168+
* @param success A composable function to be displayed with the successfully parsed markdown content.
169+
* It receives the state, components, and modifier as parameters.
170+
* By default, this renders the content in a [Column].
171+
* @param error A composable function to be displayed in case of parsing errors.
172+
* This is only triggered if assertions are enabled on the parser.
173+
*
174+
* @see rememberMarkdownState
128175
*/
129176
@Composable
130177
fun Markdown(
@@ -168,23 +215,53 @@ fun Markdown(
168215

169216

170217
/**
171-
* Renders the markdown content.
172-
*
173-
* @param state The markdown state to be rendered.
174-
* @param colors The colors to be used for rendering.
175-
* @param typography The typography to be used for rendering.
176-
* @param modifier The modifier to be applied to the container.
177-
* @param padding The padding to be applied to the container.
178-
* @param dimens The dimensions to be used for rendering.
179-
* @param imageTransformer The image transformer to be used for rendering images.
180-
* @param annotator The annotator to be used for rendering annotations.
181-
* @param extendedSpans The extended spans to be used for rendering.
182-
* @param inlineContent The inline content to be rendered.
183-
* @param components The components to be used for rendering.
184-
* @param animations The animations to be used for rendering.
185-
* @param loading A composable function to be displayed while loading the content.
186-
* @param success A composable function to be displayed with the markdown content. It receives the modifier, state and components as parameters. By default this is a [Column].
187-
* @param error A composable function to be displayed in case of an error. Only really possible if assertions are enabled on the parser)
218+
* Renders markdown content using a [State] object directly.
219+
*
220+
* This is the lowest-level overload of the Markdown composable, providing direct control
221+
* over the rendering state. It's primarily used internally by the other overloads but
222+
* can be useful for advanced use cases where you need to:
223+
* - Implement custom state transitions
224+
* - Handle specific loading/error states manually
225+
* - Integrate with custom state management systems
226+
*
227+
* The function handles different states (Loading, Success, Error) and provides
228+
* appropriate UI for each state.
229+
*
230+
* Example usage:
231+
* ```
232+
* val state: State = ... // Your custom state implementation
233+
*
234+
* Markdown(
235+
* state = state,
236+
* colors = markdownColor(),
237+
* typography = markdownTypography(),
238+
* loading = { LoadingIndicator() },
239+
* error = { ErrorMessage() }
240+
* )
241+
* ```
242+
*
243+
* @param state A [State] object representing the current state of the markdown content (loading, success, or error).
244+
* @param colors The color scheme to be used for rendering different markdown elements.
245+
* @param typography The typography settings to be used for text rendering.
246+
* @param modifier The modifier to be applied to the container. Defaults to [Modifier.fillMaxSize].
247+
* @param padding The padding configuration to be applied to different markdown elements.
248+
* @param dimens The dimension settings for spacing and sizing of markdown elements.
249+
* @param imageTransformer The transformer for handling and loading images within markdown content.
250+
* @param annotator The annotator for adding custom annotations to the rendered content.
251+
* @param extendedSpans The configuration for extended text spans beyond standard markdown.
252+
* @param inlineContent The configuration for inline custom content within markdown.
253+
* @param components The custom components to be used for rendering different markdown elements.
254+
* @param animations The animation configurations for interactive elements.
255+
* @param loading A composable function to be displayed when the state is [State.Loading].
256+
* @param success A composable function to be displayed when the state is [State.Success].
257+
* It receives the state, components, and modifier as parameters.
258+
* By default, this renders the content in a [Column].
259+
* @param error A composable function to be displayed when the state is [State.Error].
260+
*
261+
* @see State
262+
* @see State.Loading
263+
* @see State.Success
264+
* @see State.Error
188265
*/
189266
@Composable
190267
fun Markdown(
@@ -228,11 +305,35 @@ fun Markdown(
228305
}
229306

230307
/**
231-
* Renders the parsed markdown content.
308+
* Renders the successfully parsed markdown content in a Column layout.
309+
*
310+
* This function is used internally by the [Markdown] composable when the state is [State.Success].
311+
* It iterates through the parsed markdown nodes and renders each element using the appropriate
312+
* component from the provided [MarkdownComponents].
232313
*
233-
* @param state The success markdown state.
234-
* @param components The MarkdownComponents instance containing the components to use.
235-
* @param modifier The modifier to be applied to the container.
314+
* You can use this function directly if you have a custom state management system and
315+
* want to render only the success state of markdown content.
316+
*
317+
* Example usage:
318+
* ```
319+
* val successState: State.Success = ... // Your parsed markdown state
320+
* val components = markdownComponents()
321+
*
322+
* MarkdownSuccess(
323+
* state = successState,
324+
* components = components,
325+
* modifier = Modifier.padding(16.dp)
326+
* )
327+
* ```
328+
*
329+
* @param state The [State.Success] object containing the parsed markdown content and node tree.
330+
* @param components The [MarkdownComponents] instance containing the components to use for rendering
331+
* different markdown elements.
332+
* @param modifier The modifier to be applied to the container Column.
333+
*
334+
* @see State.Success
335+
* @see MarkdownComponents
336+
* @see MarkdownElement
236337
*/
237338
@Composable
238339
fun MarkdownSuccess(
@@ -245,4 +346,4 @@ fun MarkdownSuccess(
245346
MarkdownElement(node, components, state.content, skipLinkDefinition = state.linksLookedUp)
246347
}
247348
}
248-
}
349+
}

multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/model/MarkdownImageState.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,19 @@ internal class MarkdownImageStateImpl(override val density: Density) : MarkdownI
5151
}
5252
}
5353

54+
/**
55+
* Creates and remembers a [MarkdownImageState] instance.
56+
*
57+
* This composable function creates a new instance of [MarkdownImageState] using the current
58+
* density from [LocalDensity] and remembers it across recompositions as long as the
59+
* density doesn't change.
60+
*
61+
* It's used internally by the markdown renderer to manage image state for inline images.
62+
*
63+
* @return A remembered instance of [MarkdownImageState].
64+
*/
5465
@Composable
5566
internal fun rememberMarkdownImageState(): MarkdownImageState {
5667
val density = LocalDensity.current
5768
return remember(density) { MarkdownImageStateImpl(density) }
58-
}
69+
}

0 commit comments

Comments
 (0)