Skip to content

Commit

Permalink
Clean up Commonmark API conventions
Browse files Browse the repository at this point in the history
- use one node type for Heading
- rename 'content' to 'children' if it points to
other blocks

Also clean up tests to not use parenthesis,
becasue it breaks navigation in IDEA
  • Loading branch information
obask committed Mar 31, 2024
1 parent 82d3581 commit 2dc4fae
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 822 deletions.
88 changes: 44 additions & 44 deletions markdown/core/api/core.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,44 @@ import org.commonmark.node.Text as CMText
*/
public sealed interface InlineMarkdown {

public val value: Node
public val nativeNode: Node

@JvmInline
public value class Emphasis(override val value: CMEmphasis) : InlineMarkdown
public value class Code(override val nativeNode: CMCode) : InlineMarkdown

@JvmInline
public value class Image(override val value: CMImage) : InlineMarkdown
public value class CustomNode(override val nativeNode: CMCustomNode) : InlineMarkdown

@JvmInline
public value class Code(override val value: CMCode) : InlineMarkdown
public value class Emphasis(override val nativeNode: CMEmphasis) : InlineMarkdown

@JvmInline
public value class CustomNode(override val value: CMCustomNode) : InlineMarkdown
public value class HardLineBreak(override val nativeNode: CMHardLineBreak) : InlineMarkdown

@JvmInline
public value class HardLineBreak(override val value: CMHardLineBreak) : InlineMarkdown
public value class HtmlInline(override val nativeNode: CMHtmlInline) : InlineMarkdown

@JvmInline
public value class SoftLineBreak(override val value: CMSoftLineBreak) : InlineMarkdown
public value class Image(override val nativeNode: CMImage) : InlineMarkdown

@JvmInline
public value class HtmlInline(override val value: CMHtmlInline) : InlineMarkdown
public value class Link(override val nativeNode: CMLink) : InlineMarkdown

@JvmInline
public value class Link(override val value: CMLink) : InlineMarkdown
public value class Paragraph(override val nativeNode: CMParagraph) : InlineMarkdown

@JvmInline
public value class StrongEmphasis(override val value: CMStrongEmphasis) : InlineMarkdown
public value class SoftLineBreak(override val nativeNode: CMSoftLineBreak) : InlineMarkdown

@JvmInline
public value class Paragraph(override val value: CMParagraph) : InlineMarkdown
public value class StrongEmphasis(override val nativeNode: CMStrongEmphasis) : InlineMarkdown

@JvmInline
public value class Text(override val value: CMText) : InlineMarkdown
public value class Text(override val nativeNode: CMText) : InlineMarkdown

public val children: Iterator<InlineMarkdown>
get() = object : Iterator<InlineMarkdown> {
var current = this@InlineMarkdown.value.firstChild
var current = this@InlineMarkdown.nativeNode.firstChild

override fun hasNext(): Boolean = current != null

Expand All @@ -83,14 +83,14 @@ public sealed interface InlineMarkdown {

public fun Node.toInlineNode(): InlineMarkdown = when (this) {
is CMText -> Text(this)
is CMLink -> Link(this)
is CMEmphasis -> Emphasis(this)
is CMImage -> Image(this)
is CMStrongEmphasis -> StrongEmphasis(this)
is CMCode -> Code(this)
is CMCustomNode -> CustomNode(this)
is CMHtmlInline -> HtmlInline(this)
is CMImage -> Image(this)
is CMHardLineBreak -> HardLineBreak(this)
is CMSoftLineBreak -> SoftLineBreak(this)
is CMHtmlInline -> HtmlInline(this)
is CMLink -> Link(this)
is CMStrongEmphasis -> StrongEmphasis(this)
is CMCustomNode -> CustomNode(this)
else -> error("Unexpected block $this")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package org.jetbrains.jewel.markdown

public sealed interface MarkdownBlock {

public data class BlockQuote(val content: List<MarkdownBlock>) : MarkdownBlock

public interface CustomBlock : MarkdownBlock
public data class BlockQuote(val children: List<MarkdownBlock>) : MarkdownBlock

public sealed interface CodeBlock : MarkdownBlock {

Expand All @@ -20,6 +18,8 @@ public sealed interface MarkdownBlock {
) : CodeBlock
}

public interface CustomBlock : MarkdownBlock

public data class Heading(
override val inlineContent: List<InlineMarkdown>,
val level: Int,
Expand All @@ -29,25 +29,25 @@ public sealed interface MarkdownBlock {

public sealed interface ListBlock : MarkdownBlock {

public val items: List<ListItem>
public val children: List<ListItem>
public val isTight: Boolean

public data class BulletList(
override val items: List<ListItem>,
override val isTight: Boolean,
val bulletMarker: String,
) : ListBlock

public data class OrderedList(
override val items: List<ListItem>,
override val children: List<ListItem>,
override val isTight: Boolean,
val startFrom: Int,
val delimiter: String,
) : ListBlock

public data class UnorderedList(
override val children: List<ListItem>,
override val isTight: Boolean,
val marker: String,
) : ListBlock
}

public data class ListItem(
val content: List<MarkdownBlock>,
val children: List<MarkdownBlock>,
) : MarkdownBlock

public object ThematicBreak : MarkdownBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.commonmark.node.FencedCodeBlock
import org.commonmark.node.Heading
import org.commonmark.node.HtmlBlock
import org.commonmark.node.IndentedCodeBlock
import org.commonmark.node.ListBlock
import org.commonmark.node.ListItem
import org.commonmark.node.Node
import org.commonmark.node.OrderedList
Expand All @@ -24,10 +23,12 @@ import org.jetbrains.jewel.foundation.ExperimentalJewelApi
import org.jetbrains.jewel.markdown.InlineMarkdown
import org.jetbrains.jewel.markdown.MarkdownBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.CodeBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock
import org.jetbrains.jewel.markdown.MimeType
import org.jetbrains.jewel.markdown.extensions.MarkdownProcessorExtension
import org.jetbrains.jewel.markdown.rendering.DefaultInlineMarkdownRenderer
import org.jetbrains.jewel.markdown.toInlineNode
import org.commonmark.node.ListBlock as CMListBlock

/**
* @param optimizeEdits Optional. Indicates whether the processing should only update the changed blocks
Expand Down Expand Up @@ -179,31 +180,30 @@ public class MarkdownProcessor(
private fun Node.tryProcessMarkdownBlock(): MarkdownBlock? =
// Non-Block children are ignored
when (this) {
is BlockQuote -> toMarkdownBlockQuote()
is Heading -> toMarkdownHeadingOrNull()
is Paragraph -> toMarkdownParagraphOrNull()
is FencedCodeBlock -> toMarkdownCodeBlockOrNull()
is IndentedCodeBlock -> toMarkdownCodeBlockOrNull()
is Heading -> toMarkdownHeadingOrNull()
is BulletList -> toMarkdownListOrNull()
is OrderedList -> toMarkdownListOrNull()
is FencedCodeBlock -> toMarkdownCodeBlockOrNull()
is IndentedCodeBlock -> toMarkdownCodeBlockOrNull()
is BlockQuote -> toMarkdownBlockQuote()
is ThematicBreak -> MarkdownBlock.ThematicBreak
is HtmlBlock -> toMarkdownHtmlBlockOrNull()
is CustomBlock -> {
extensions.find { it.processorExtension.canProcess(this) }
?.processorExtension?.processMarkdownBlock(this, this@MarkdownProcessor)
}

else -> null
}

private fun BlockQuote.toMarkdownBlockQuote(): MarkdownBlock.BlockQuote =
MarkdownBlock.BlockQuote(processChildren(this))

private fun Heading.toMarkdownHeadingOrNull(): MarkdownBlock.Heading? =
if (level > 6) {
null
} else {
MarkdownBlock.Heading(contentsAsInlineMarkdown(), level)
}
private fun Heading.toMarkdownHeadingOrNull(): MarkdownBlock.Heading? {
if (level < 1 || level > 6) return null
return MarkdownBlock.Heading(contentsAsInlineMarkdown(), level)
}

private fun Paragraph.toMarkdownParagraphOrNull(): MarkdownBlock.Paragraph? {
val inlineMarkdown = contentsAsInlineMarkdown()
Expand All @@ -221,21 +221,21 @@ public class MarkdownProcessor(
private fun IndentedCodeBlock.toMarkdownCodeBlockOrNull(): CodeBlock.IndentedCodeBlock =
CodeBlock.IndentedCodeBlock(literal.trimEnd('\n'))

private fun BulletList.toMarkdownListOrNull(): MarkdownBlock.ListBlock.BulletList? {
private fun BulletList.toMarkdownListOrNull(): ListBlock.UnorderedList? {
val children = processListItems()
if (children.isEmpty()) return null

return MarkdownBlock.ListBlock.BulletList(children, isTight, marker)
return ListBlock.UnorderedList(children, isTight, marker)
}

private fun OrderedList.toMarkdownListOrNull(): MarkdownBlock.ListBlock.OrderedList? {
private fun OrderedList.toMarkdownListOrNull(): ListBlock.OrderedList? {
val children = processListItems()
if (children.isEmpty()) return null

return MarkdownBlock.ListBlock.OrderedList(children, isTight, markerStartNumber, markerDelimiter)
return ListBlock.OrderedList(children, isTight, markerStartNumber, markerDelimiter)
}

private fun ListBlock.processListItems() = buildList {
private fun CMListBlock.processListItems() = buildList {
forEachChild { child ->
if (child !is ListItem) return@forEachChild
add(MarkdownBlock.ListItem(processChildren(child)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public open class DefaultInlineMarkdownRenderer(rendererExtensions: List<Markdow
appendInlineMarkdownFrom(child.children, styling)
}

is InlineMarkdown.Text -> append(child.value.literal)
is InlineMarkdown.Text -> append(child.nativeNode.literal)

is InlineMarkdown.Emphasis -> {
withStyles(styling.emphasis, child) { appendInlineMarkdownFrom(it.children, styling) }
Expand All @@ -50,13 +50,13 @@ public open class DefaultInlineMarkdownRenderer(rendererExtensions: List<Markdow

is InlineMarkdown.Link -> {
withStyles(styling.link, child) {
pushUrlAnnotation(UrlAnnotation(it.value.destination))
pushUrlAnnotation(UrlAnnotation(it.nativeNode.destination))
appendInlineMarkdownFrom(it.children, styling)
}
}

is InlineMarkdown.Code -> {
withStyles(styling.inlineCode, child) { append(it.value.literal) }
withStyles(styling.inlineCode, child) { append(it.nativeNode.literal) }
}

is InlineMarkdown.HardLineBreak,
Expand All @@ -65,14 +65,14 @@ public open class DefaultInlineMarkdownRenderer(rendererExtensions: List<Markdow

is InlineMarkdown.HtmlInline -> {
if (styling.renderInlineHtml) {
withStyles(styling.inlineHtml, child) { append(it.value.literal.trim()) }
withStyles(styling.inlineHtml, child) { append(it.nativeNode.literal.trim()) }
}
}

is InlineMarkdown.Image -> {
appendInlineContent(
INLINE_IMAGE,
child.value.destination + "\n" + plainTextRenderer.render(child.value),
child.nativeNode.destination + "\n" + plainTextRenderer.render(child.nativeNode),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ import org.jetbrains.jewel.markdown.MarkdownBlock.CustomBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.Heading
import org.jetbrains.jewel.markdown.MarkdownBlock.HtmlBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock.BulletList
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock.OrderedList
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock.UnorderedList
import org.jetbrains.jewel.markdown.MarkdownBlock.ListItem
import org.jetbrains.jewel.markdown.MarkdownBlock.Paragraph
import org.jetbrains.jewel.markdown.MarkdownBlock.ThematicBreak
Expand Down Expand Up @@ -96,7 +96,7 @@ public open class DefaultMarkdownBlockRenderer(
is Heading -> render(block, rootStyling.heading)
is HtmlBlock -> render(block, rootStyling.htmlBlock)
is OrderedList -> render(block, rootStyling.list.ordered)
is BulletList -> render(block, rootStyling.list.unordered)
is UnorderedList -> render(block, rootStyling.list.unordered)
is ListItem -> render(block)
is Paragraph -> render(block, rootStyling.paragraph)
ThematicBreak -> renderThematicBreak(rootStyling.thematicBreak)
Expand Down Expand Up @@ -182,7 +182,7 @@ public open class DefaultMarkdownBlockRenderer(
verticalArrangement = Arrangement.spacedBy(rootStyling.blockVerticalSpacing),
) {
CompositionLocalProvider(LocalContentColor provides styling.textColor) {
render(block.content)
render(block.children)
}
}
}
Expand All @@ -191,7 +191,7 @@ public open class DefaultMarkdownBlockRenderer(
override fun render(block: ListBlock, styling: MarkdownStyling.List) {
when (block) {
is OrderedList -> render(block, styling.ordered)
is BulletList -> render(block, styling.unordered)
is UnorderedList -> render(block, styling.unordered)
}
}

Expand All @@ -208,7 +208,7 @@ public open class DefaultMarkdownBlockRenderer(
modifier = Modifier.padding(styling.padding),
verticalArrangement = Arrangement.spacedBy(itemSpacing),
) {
for ((index, item) in block.items.withIndex()) {
for ((index, item) in block.children.withIndex()) {
Row {
val number = block.startFrom + index
Text(
Expand All @@ -228,7 +228,7 @@ public open class DefaultMarkdownBlockRenderer(
}

@Composable
override fun render(block: BulletList, styling: MarkdownStyling.List.Unordered) {
override fun render(block: UnorderedList, styling: MarkdownStyling.List.Unordered) {
val itemSpacing =
if (block.isTight) {
styling.itemVerticalSpacingTight
Expand All @@ -240,7 +240,7 @@ public open class DefaultMarkdownBlockRenderer(
modifier = Modifier.padding(styling.padding),
verticalArrangement = Arrangement.spacedBy(itemSpacing),
) {
for (item in block.items) {
for (item in block.children) {
Row {
Text(
text = styling.bullet.toString(),
Expand All @@ -259,7 +259,7 @@ public open class DefaultMarkdownBlockRenderer(
@Composable
override fun render(block: ListItem) {
Column(verticalArrangement = Arrangement.spacedBy(rootStyling.blockVerticalSpacing)) {
render(block.content)
render(block.children)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import org.jetbrains.jewel.markdown.MarkdownBlock.CodeBlock.FencedCodeBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.CodeBlock.IndentedCodeBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.HtmlBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock.BulletList
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock.OrderedList
import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock.UnorderedList
import org.jetbrains.jewel.markdown.MarkdownBlock.ListItem

@ExperimentalJewelApi
Expand Down Expand Up @@ -41,7 +41,7 @@ public interface MarkdownBlockRenderer {
public fun render(block: OrderedList, styling: MarkdownStyling.List.Ordered)

@Composable
public fun render(block: BulletList, styling: MarkdownStyling.List.Unordered)
public fun render(block: UnorderedList, styling: MarkdownStyling.List.Unordered)

@Composable
public fun render(block: ListItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class MarkdownStyling(
public val h6: H6,
) {

public interface HN : WithInlinesStyling, WithUnderline {
public sealed interface HN : WithInlinesStyling, WithUnderline {
public val padding: PaddingValues
}

Expand Down
Loading

0 comments on commit 2dc4fae

Please sign in to comment.