-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow customizing the CommonMark Parser (#444)
This is required by one of our customers, and it allows them to customize the handling of standard tags (e.g., remove images). It is an opt-in behaviour and it does not impact existing Markdown handling code or behaviour.
- Loading branch information
Showing
3 changed files
with
72 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...own/core/src/main/kotlin/org/jetbrains/jewel/markdown/processing/MarkdownParserFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.jetbrains.jewel.markdown.processing | ||
|
||
import org.commonmark.parser.IncludeSourceSpans | ||
import org.commonmark.parser.Parser | ||
import org.jetbrains.jewel.markdown.extensions.MarkdownProcessorExtension | ||
|
||
/** | ||
* Simplifies creating a [CommonMark `Parser`][Parser] while also | ||
* supporting Jewel's [MarkdownProcessorExtension]s and the `optimizeEdits` | ||
* flag. | ||
*/ | ||
public object MarkdownParserFactory { | ||
/** | ||
* Create a [CommonMark `Parser`][Parser] with the provided [extensions]. | ||
* The parser's [Builder][Parser.Builder] can be customized by providing a | ||
* non-null [customizeBuilder] lambda. | ||
* | ||
* Make sure to provide the right value for [optimizeEdits], matching the | ||
* one provided to the [MarkdownProcessor], or this parser will not be set | ||
* up correctly. | ||
* | ||
* @param optimizeEdits If true, sets up the [Parser] to allow for edits | ||
* optimization in the [MarkdownProcessor]. | ||
* @param extensions A list of [MarkdownProcessorExtension] to attach. | ||
* @param customizeBuilder Allows customizing the [Parser.Builder] before | ||
* its [build()][Parser.Builder.build] method is called. | ||
*/ | ||
public fun create( | ||
optimizeEdits: Boolean, | ||
extensions: List<MarkdownProcessorExtension> = emptyList(), | ||
customizeBuilder: (Parser.Builder.() -> Parser.Builder)? = null, | ||
): Parser = | ||
Parser.builder() | ||
.extensions(extensions.map(MarkdownProcessorExtension::parserExtension)) | ||
.run { | ||
val builder = | ||
if (optimizeEdits) { | ||
includeSourceSpans(IncludeSourceSpans.BLOCKS) | ||
} else { | ||
this | ||
} | ||
|
||
if (customizeBuilder != null) { | ||
builder.customizeBuilder() | ||
} else { | ||
builder | ||
} | ||
} | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters