Skip to content

Commit

Permalink
Have markdown print out unknown callout type as a warning
Browse files Browse the repository at this point in the history
... instead of showing users an UNKNOWN callout type.
  • Loading branch information
bitspittle committed Sep 5, 2024
1 parent c236fbb commit 90a9009
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,6 @@ class CalloutType(
provideColor = { it.toPalette().callout.tip },
)

/**
* A special fallback type provided so that markdown handling can show something if a specified type is not
* recognized.
*/
val UNKNOWN = CalloutType(
{ CloseIcon() },
"???",
Colors.Magenta
)

/**
* Information that a user should be aware of to prevent errors.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ fun initSilkWidgets(ctx: InitSilkContext) {
mutableTheme.registerStyle("silk-callout-type_question", CalloutType.QUESTION)
mutableTheme.registerStyle("silk-callout-type_quote", CalloutType.QUOTE)
mutableTheme.registerStyle("silk-callout-type_tip", CalloutType.TIP)
mutableTheme.registerStyle("silk-callout-type_unknown", CalloutType.UNKNOWN)
mutableTheme.registerStyle("silk-callout-type_warning", CalloutType.WARNING)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class KotlinRenderer(
private val reporter: Reporter,
) : Renderer {
private var indentCount = 0
private val indent get() = NodeScope(TypedMap()).indent(indentCount)
private val indent get() = NodeScope(reporter, TypedMap()).indent(indentCount)


// Flexible data which can be used by Node handlers however they need
Expand Down Expand Up @@ -254,7 +254,7 @@ class KotlinRenderer(
}

private fun <N : Node> doVisit(node: N, composableCall: Provider<NodeScope.(N) -> String>) {
val scope = NodeScope(data, indentCount)
val scope = NodeScope(reporter, data, indentCount)
composableCall.get().invoke(scope, node).takeIf { it.isNotBlank() }?.let { code ->
// Remove leading indentation (if any) because we add it ourselves
doVisit(node, code.trimStart(), scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ fun SilkCalloutBlockquoteHandler(
if (calloutTypeFqn != null) {
"""$SILK.display.Callout(type = $calloutTypeFqn, label = ${label?.let { "\"$it\"" }}, variant = $variant)"""
} else {
"""$SILK.display.Callout(type = $SILK.display.CalloutType.UNKNOWN, label = "Invalid callout type [!$typeId]", variant = $variant)"""
reporter.warn("Unknown Markdown callout type: $typeId")
null
}
}
silkCallout ?: "$KOBWEB_DOM.GenericTag(\"blockquote\")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.varabyte.kobwebx.gradle.markdown.handlers

import com.varabyte.kobweb.common.collect.Key
import com.varabyte.kobweb.common.collect.TypedMap
import com.varabyte.kobweb.gradle.core.util.Reporter
import com.varabyte.kobweb.gradle.core.util.getJsDependencyResults
import com.varabyte.kobweb.gradle.core.util.hasDependencyNamed
import com.varabyte.kobwebx.gradle.markdown.children
Expand Down Expand Up @@ -50,9 +51,10 @@ internal const val SILK = "com.varabyte.kobweb.silk.components"
/**
* Data available to [MarkdownHandlers] callbacks
*
* @param reporter A logger useful for reporting errors or warnings.
* @param data A simple map that is created once per file and can be used by components however they want to.
*/
class NodeScope(val data: TypedMap, private val indentCountBase: Int = 0) {
class NodeScope(val reporter: Reporter, val data: TypedMap, private val indentCountBase: Int = 0) {
/** If set, will cause the Markdown visit to visit these nodes instead of the node's original children. */
var childrenOverride: List<Node>? = null

Expand Down

0 comments on commit 90a9009

Please sign in to comment.