Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructures MarkdownParseOptions to expose extension selectors. #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ import com.halilibo.richtext.ui.resolveDefaults
)
}
LaunchedEffect(isAutolinkEnabled) {
markdownParseOptions = markdownParseOptions.copy(
autolink = isAutolinkEnabled
)
markdownParseOptions = if (isAutolinkEnabled)
MarkdownParseOptions.MarkdownWithLinks
else
MarkdownParseOptions.MarkdownOnly
}

val colors = if (isDarkModeEnabled) darkColorScheme() else lightColorScheme()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ import com.halilibo.richtext.ui.resolveDefaults
)
}
LaunchedEffect(isAutolinkEnabled) {
markdownParseOptions = markdownParseOptions.copy(
autolink = isAutolinkEnabled
)
markdownParseOptions = if (isAutolinkEnabled)
MarkdownParseOptions.MarkdownWithLinks
else
MarkdownParseOptions.MarkdownOnly
}

val colors = if (isDarkModeEnabled) darkColorScheme() else lightColorScheme()
Expand Down
6 changes: 5 additions & 1 deletion docs/richtext-commonmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ Passing `MarkdownParseOptions` into either `Markdown` composable or `CommonmarkA

```kotlin
val markdownParseOptions = MarkdownParseOptions(
autolink = false
listOfNotNull(
TablesExtension.create(),
StrikethroughExtension.create(),
AutolinkExtension.create()
)
)

Markdown(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,7 @@ public actual class CommonmarkAstNodeParser actual constructor(
) {

private val parser = Parser.builder()
.extensions(
listOfNotNull(
TablesExtension.create(),
StrikethroughExtension.create(),
if (options.autolink) AutolinkExtension.create() else null
)
)
.extensions(options.extensions)
.build()

public actual fun parse(text: String): AstNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package com.halilibo.richtext.commonmark

import org.commonmark.Extension
import org.commonmark.ext.autolink.AutolinkExtension
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension
import org.commonmark.ext.gfm.tables.TablesExtension

/**
* Allows configuration of the Markdown parser
*
* @param autolink Detect plain text links and turn them into Markdown links.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to update kdoc.

*/
public data class MarkdownParseOptions(
val autolink: Boolean
) {
public data class MarkdownParseOptions(val extensions: List<Extension>) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since richtext-commonmark and richtext-markdown are now separate modules I'm not as opposed to exposing commonmark details as I had been before, but I still would prefer if extensions were exposed as a value class.

value class MarkdownExtension private constructor(internal val value: Int) {
  companion object {
    val Tables = ...
    val AutoLink = ...
    ...
  }
}

public companion object {
public val Default: MarkdownParseOptions = MarkdownParseOptions(
autolink = true
public val MarkdownWithLinks: MarkdownParseOptions = MarkdownParseOptions(
listOfNotNull(
TablesExtension.create(),
StrikethroughExtension.create(),
AutolinkExtension.create()
)
)

public val MarkdownOnly: MarkdownParseOptions = MarkdownParseOptions(
listOfNotNull(
TablesExtension.create(),
StrikethroughExtension.create()
)
)

public val Default: MarkdownParseOptions = MarkdownWithLinks
}
}
}
Loading