Skip to content

Commit

Permalink
Merge branch 'JetBrains:main' into autolink
Browse files Browse the repository at this point in the history
  • Loading branch information
jrlogsdon authored Apr 1, 2024
2 parents 49e2309 + 82d3581 commit b5c3765
Show file tree
Hide file tree
Showing 22 changed files with 1,336 additions and 702 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commonmark = "0.22.0"
composeDesktop = "1.6.10-dev1549"
detekt = "1.23.4"
dokka = "1.8.20"
idea = "241.14494.17-EAP-SNAPSHOT"
idea = "241.14494.158-EAP-SNAPSHOT"
ideaGradlePlugin = "1.17.1"
jna = "5.14.0"
kotlin = "1.8.21"
Expand Down
391 changes: 247 additions & 144 deletions markdown/core/api/core.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.jetbrains.jewel.markdown

import org.commonmark.node.Node
import org.jetbrains.jewel.markdown.InlineMarkdown.Code
import org.jetbrains.jewel.markdown.InlineMarkdown.CustomNode
import org.jetbrains.jewel.markdown.InlineMarkdown.Emphasis
import org.jetbrains.jewel.markdown.InlineMarkdown.HardLineBreak
import org.jetbrains.jewel.markdown.InlineMarkdown.HtmlInline
import org.jetbrains.jewel.markdown.InlineMarkdown.Image
import org.jetbrains.jewel.markdown.InlineMarkdown.Link
import org.jetbrains.jewel.markdown.InlineMarkdown.SoftLineBreak
import org.jetbrains.jewel.markdown.InlineMarkdown.StrongEmphasis
import org.jetbrains.jewel.markdown.InlineMarkdown.Text
import org.commonmark.node.Code as CMCode
import org.commonmark.node.CustomNode as CMCustomNode
import org.commonmark.node.Emphasis as CMEmphasis
import org.commonmark.node.HardLineBreak as CMHardLineBreak
import org.commonmark.node.HtmlInline as CMHtmlInline
import org.commonmark.node.Image as CMImage
import org.commonmark.node.Link as CMLink
import org.commonmark.node.Paragraph as CMParagraph
import org.commonmark.node.SoftLineBreak as CMSoftLineBreak
import org.commonmark.node.StrongEmphasis as CMStrongEmphasis
import org.commonmark.node.Text as CMText

/**
* A run of inline Markdown used as content for
* [block-level elements][MarkdownBlock].
*/
public sealed interface InlineMarkdown {

public val value: Node

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

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

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

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

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

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

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

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

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

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

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

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

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

override fun next(): InlineMarkdown =
if (hasNext()) {
current.toInlineNode().also {
current = current.next
}
} else {
throw NoSuchElementException()
}
}
}

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

import org.intellij.lang.annotations.Language

public sealed interface MarkdownBlock {

public data class Paragraph(override val inlineContent: InlineMarkdown) :
MarkdownBlock, BlockWithInlineMarkdown

public sealed interface Heading : MarkdownBlock, BlockWithInlineMarkdown {

public data class H1(override val inlineContent: InlineMarkdown) : Heading
public data class BlockQuote(val content: List<MarkdownBlock>) : MarkdownBlock

public data class H2(override val inlineContent: InlineMarkdown) : Heading
public interface CustomBlock : MarkdownBlock

public data class H3(override val inlineContent: InlineMarkdown) : Heading
public sealed interface CodeBlock : MarkdownBlock {

public data class H4(override val inlineContent: InlineMarkdown) : Heading
public val content: String

public data class H5(override val inlineContent: InlineMarkdown) : Heading
public data class IndentedCodeBlock(
override val content: String,
) : CodeBlock

public data class H6(override val inlineContent: InlineMarkdown) : Heading
public data class FencedCodeBlock(
override val content: String,
val mimeType: MimeType?,
) : CodeBlock
}

public data class BlockQuote(val content: List<MarkdownBlock>) : MarkdownBlock
public data class Heading(
override val inlineContent: List<InlineMarkdown>,
val level: Int,
) : MarkdownBlock, BlockWithInlineMarkdown

public data class HtmlBlock(val content: String) : MarkdownBlock

public sealed interface ListBlock : MarkdownBlock {

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

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

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

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

public sealed interface CodeBlock : MarkdownBlock {

public val content: String

public data class IndentedCodeBlock(
override val content: String,
) : CodeBlock

public data class FencedCodeBlock(
override val content: String,
val mimeType: MimeType?,
) : CodeBlock
}

public data class Image(val url: String, val altString: String?) : MarkdownBlock

public object ThematicBreak : MarkdownBlock

public data class HtmlBlock(val content: String) : MarkdownBlock

public interface Extension : MarkdownBlock
public data class Paragraph(override val inlineContent: List<InlineMarkdown>) :
MarkdownBlock, BlockWithInlineMarkdown
}

public interface BlockWithInlineMarkdown {

public val inlineContent: InlineMarkdown
public val inlineContent: List<InlineMarkdown>
}

/**
* A run of inline Markdown used as content for
* [block-level elements][MarkdownBlock].
*/
@JvmInline
public value class InlineMarkdown(@Language("Markdown") public val content: String)
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public interface MarkdownBlockProcessorExtension {
public fun canProcess(block: CustomBlock): Boolean

/**
* Processes the [block] as a [MarkdownBlock.Extension], if possible. Note
* Processes the [block] as a [MarkdownBlock.CustomBlock], if possible. Note
* that you should always check that [canProcess] returns true for the same
* [block], as implementations might throw an exception for unsupported
* block types.
*/
public fun processMarkdownBlock(block: CustomBlock, processor: MarkdownProcessor): MarkdownBlock.Extension?
public fun processMarkdownBlock(block: CustomBlock, processor: MarkdownProcessor): MarkdownBlock.CustomBlock?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package org.jetbrains.jewel.markdown.extensions

import androidx.compose.runtime.Composable
import org.jetbrains.jewel.markdown.MarkdownBlock
import org.jetbrains.jewel.markdown.MarkdownBlock.Extension
import org.jetbrains.jewel.markdown.MarkdownBlock.CustomBlock
import org.jetbrains.jewel.markdown.rendering.InlineMarkdownRenderer
import org.jetbrains.jewel.markdown.rendering.MarkdownBlockRenderer

/**
* An extension for [MarkdownBlockRenderer] that can render one or more
* [MarkdownBlock.Extension]s.
* [MarkdownBlock.CustomBlock]s.
*/
public interface MarkdownBlockRendererExtension {

/** Check whether the provided [block] can be rendered by this extension. */
public fun canRender(block: Extension): Boolean
public fun canRender(block: CustomBlock): Boolean

/**
* Render a [MarkdownBlock.Extension] as a native Composable. Note that if
* Render a [MarkdownBlock.CustomBlock] as a native Composable. Note that if
* [canRender] returns `false` for [block], the implementation might throw.
*/
@Composable
public fun render(
block: Extension,
block: CustomBlock,
blockRenderer: MarkdownBlockRenderer,
inlineRenderer: InlineMarkdownRenderer,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface MarkdownProcessorExtension {
* An extension for
* [`MarkdownParser`][org.jetbrains.jewel.markdown.parsing.MarkdownParser]
* that will transform a supported [CustomBlock] into the corresponding
* [MarkdownBlock.Extension].
* [MarkdownBlock.CustomBlock].
*/
public val processorExtension: MarkdownBlockProcessorExtension?
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface MarkdownRendererExtension {
* An extension for
* [`MarkdownBlockRenderer`][org.jetbrains.jewel.markdown.rendering.MarkdownBlockRenderer]
* that will render a supported
* [`Extension`][org.jetbrains.jewel.markdown.MarkdownBlock.Extension] into
* [`CustomBlock`][org.jetbrains.jewel.markdown.MarkdownBlock.CustomBlock] into
* a native Jewel UI.
*/
public val blockRenderer: MarkdownBlockRendererExtension
Expand Down
Loading

0 comments on commit b5c3765

Please sign in to comment.