forked from JetBrains/jewel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oleg Baskakov
committed
Mar 30, 2024
1 parent
7c9345f
commit deb8370
Showing
6 changed files
with
215 additions
and
2 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
48 changes: 48 additions & 0 deletions
48
...core/src/main/kotlin/org/jetbrains/jewel/markdown/processing/TaskListItemPostProcessor.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,48 @@ | ||
package org.jetbrains.jewel.markdown.processing | ||
|
||
import org.commonmark.node.AbstractVisitor | ||
import org.commonmark.node.CustomNode | ||
import org.commonmark.node.ListItem | ||
import org.commonmark.node.Node | ||
import org.commonmark.node.Paragraph | ||
import org.commonmark.node.Text | ||
import org.commonmark.parser.PostProcessor | ||
import java.util.regex.Pattern | ||
|
||
public class TaskListItemPostProcessor : PostProcessor { | ||
|
||
public override fun process(node: Node): Node { | ||
val visitor = TaskListItemVisitor() | ||
node.accept(visitor) | ||
return node | ||
} | ||
|
||
private class TaskListItemVisitor : AbstractVisitor() { | ||
|
||
override fun visit(listItem: ListItem) { | ||
val child = listItem.firstChild | ||
if (child is Paragraph) { | ||
val node = child.getFirstChild() | ||
if (node is Text) { | ||
val textNode = node | ||
val matcher = REGEX_TASK_LIST_ITEM.matcher(textNode.literal) | ||
if (matcher.matches()) { | ||
// Add the task list item marker node as the first child of the list item. | ||
listItem.prependChild(object : CustomNode() {}) | ||
|
||
// Parse the node using the input after the task marker (in other words, group 2 from the matcher). | ||
// (Note that the String has been trimmed, so we should add a space between the | ||
// TaskListItemMarker and the text that follows it when we come to render it). | ||
textNode.literal = matcher.group(2) | ||
} | ||
} | ||
} | ||
visitChildren(listItem) | ||
} | ||
} | ||
|
||
public companion object { | ||
|
||
private val REGEX_TASK_LIST_ITEM: Pattern = Pattern.compile("^\\[([xX\\s])]\\s+(.*)") | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...wn/core/src/main/kotlin/org/jetbrains/jewel/markdown/processing/TaskListItemsExtension.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,38 @@ | ||
package org.jetbrains.jewel.markdown.processing | ||
|
||
import org.commonmark.Extension | ||
import org.commonmark.parser.Parser | ||
import org.commonmark.parser.Parser.ParserExtension | ||
import org.commonmark.renderer.html.HtmlNodeRendererContext | ||
import org.commonmark.renderer.html.HtmlNodeRendererFactory | ||
import org.commonmark.renderer.html.HtmlRenderer | ||
import org.commonmark.renderer.html.HtmlRenderer.HtmlRendererExtension | ||
|
||
/** | ||
* Extension for adding task list items. | ||
* | ||
* | ||
* Create it with [.create] and then configure it on the builders | ||
* ([org.commonmark.parser.Parser.Builder.extensions], | ||
* [HtmlRenderer.Builder.extensions]). | ||
* | ||
* | ||
* @since 0.15.0 | ||
*/ | ||
public class TaskListItemsExtension private constructor() : ParserExtension, HtmlRendererExtension { | ||
|
||
public override fun extend(parserBuilder: Parser.Builder) { | ||
parserBuilder.postProcessor(TaskListItemPostProcessor()) | ||
} | ||
|
||
public override fun extend(rendererBuilder: HtmlRenderer.Builder) { | ||
TODO() | ||
} | ||
|
||
public companion object { | ||
|
||
public fun create(): Extension { | ||
return TaskListItemsExtension() | ||
} | ||
} | ||
} |
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
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
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