Skip to content

Commit

Permalink
Add .collapse
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Sep 5, 2024
1 parent cca062c commit c9d92d4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package eu.iamgio.quarkdown.ast.quarkdown.block

import eu.iamgio.quarkdown.ast.InlineContent
import eu.iamgio.quarkdown.ast.NestableNode
import eu.iamgio.quarkdown.ast.Node
import eu.iamgio.quarkdown.visitor.node.NodeVisitor

/**
* A collapsible block, whose content can be hidden or shown by interacting with it.
* @param title title of the block
* @param isOpen whether the block is open at the beginning
*/
data class Collapse(
val title: InlineContent,
val isOpen: Boolean,
override val children: List<Node>,
) : NestableNode {
override fun <T> accept(visitor: NodeVisitor<T>): T = visitor.visit(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import eu.iamgio.quarkdown.ast.quarkdown.FunctionCallNode
import eu.iamgio.quarkdown.ast.quarkdown.block.Aligned
import eu.iamgio.quarkdown.ast.quarkdown.block.Box
import eu.iamgio.quarkdown.ast.quarkdown.block.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.block.Collapse
import eu.iamgio.quarkdown.ast.quarkdown.block.Math
import eu.iamgio.quarkdown.ast.quarkdown.block.PageBreak
import eu.iamgio.quarkdown.ast.quarkdown.block.SlidesFragment
Expand Down Expand Up @@ -255,6 +256,8 @@ open class BaseHtmlNodeRenderer(context: Context) : TagNodeRenderer<HtmlTagBuild

override fun visit(node: Box): CharSequence = throw UnsupportedRenderException(node)

override fun visit(node: Collapse): CharSequence = throw UnsupportedRenderException(node)

override fun visit(node: Whitespace): CharSequence = throw UnsupportedRenderException(node)

override fun visit(node: TableOfContentsView): CharSequence = throw UnsupportedRenderException(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import eu.iamgio.quarkdown.ast.quarkdown.FunctionCallNode
import eu.iamgio.quarkdown.ast.quarkdown.block.Aligned
import eu.iamgio.quarkdown.ast.quarkdown.block.Box
import eu.iamgio.quarkdown.ast.quarkdown.block.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.block.Collapse
import eu.iamgio.quarkdown.ast.quarkdown.block.Math
import eu.iamgio.quarkdown.ast.quarkdown.block.PageBreak
import eu.iamgio.quarkdown.ast.quarkdown.block.SlidesFragment
Expand Down Expand Up @@ -125,6 +126,16 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context
}
}

override fun visit(node: Collapse) =
buildTag("details") {
if (node.isOpen) {
attribute("open", "")
}

tag("summary") { +node.title }
+node.children
}

override fun visit(node: Whitespace) =
buildTag("span") {
style {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import eu.iamgio.quarkdown.ast.quarkdown.FunctionCallNode
import eu.iamgio.quarkdown.ast.quarkdown.block.Aligned
import eu.iamgio.quarkdown.ast.quarkdown.block.Box
import eu.iamgio.quarkdown.ast.quarkdown.block.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.block.Collapse
import eu.iamgio.quarkdown.ast.quarkdown.block.Math
import eu.iamgio.quarkdown.ast.quarkdown.block.PageBreak
import eu.iamgio.quarkdown.ast.quarkdown.block.SlidesFragment
Expand Down Expand Up @@ -130,6 +131,8 @@ interface NodeVisitor<T> {

fun visit(node: Box): T

fun visit(node: Collapse): T

fun visit(node: Whitespace): T

fun visit(node: TableOfContentsView): T
Expand Down
24 changes: 24 additions & 0 deletions core/src/test/kotlin/eu/iamgio/quarkdown/HtmlNodeRendererTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import eu.iamgio.quarkdown.ast.base.inline.Text
import eu.iamgio.quarkdown.ast.quarkdown.block.Aligned
import eu.iamgio.quarkdown.ast.quarkdown.block.Box
import eu.iamgio.quarkdown.ast.quarkdown.block.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.block.Collapse
import eu.iamgio.quarkdown.ast.quarkdown.block.Math
import eu.iamgio.quarkdown.ast.quarkdown.block.PageBreak
import eu.iamgio.quarkdown.ast.quarkdown.inline.MathSpan
Expand Down Expand Up @@ -729,6 +730,29 @@ class HtmlNodeRendererTest {
)
}

@Test
fun collapse() {
val out = readParts("quarkdown/collapse.html")

assertEquals(
out.next(),
Collapse(
title = listOf(Emphasis(listOf(Text("Hello")))),
isOpen = false,
children = listOf(Strong(listOf(Text("world")))),
).render(),
)

assertEquals(
out.next(),
Collapse(
title = listOf(Text("Hello")),
isOpen = true,
children = listOf(BlockQuote(children = listOf(Paragraph(listOf(Text("world")))))),
).render(),
)
}

@Test
fun `text transform`() {
val out = readParts("quarkdown/texttransform.html")
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/resources/rendering/quarkdown/collapse.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<details>
<summary>
<em>
Hello
</em>
</summary>
<strong>
world
</strong>
</details>

---

<details open="">
<summary>
Hello
</summary>
<blockquote>
<p>
world
</p>
</blockquote>
</details>
14 changes: 14 additions & 0 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Layout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.iamgio.quarkdown.ast.base.block.Table
import eu.iamgio.quarkdown.ast.quarkdown.block.Aligned
import eu.iamgio.quarkdown.ast.quarkdown.block.Box
import eu.iamgio.quarkdown.ast.quarkdown.block.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.block.Collapse
import eu.iamgio.quarkdown.ast.quarkdown.block.Stacked
import eu.iamgio.quarkdown.ast.quarkdown.inline.Whitespace
import eu.iamgio.quarkdown.context.Context
Expand All @@ -32,6 +33,7 @@ val Layout: Module =
::whitespace,
::clip,
::box,
::collapse,
::table,
)

Expand Down Expand Up @@ -164,6 +166,18 @@ fun box(
body: MarkdownContent,
) = Box(title?.children, type, padding, backgroundColor, foregroundColor, body.children).wrappedAsValue()

/**
* Inserts content in a collapsible block, whose content can be hidden or shown by interacting with it.
* @param title title of the block
* @param open whether the block is open at the beginning
* @return the new [Collapse] node
*/
fun collapse(
title: InlineMarkdownContent,
open: Boolean = false,
body: MarkdownContent,
) = Collapse(title.children, open, body.children).wrappedAsValue()

/**
* Creates a table out of a collection of columns.
*
Expand Down

0 comments on commit c9d92d4

Please sign in to comment.