Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-islands-align.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Keep an empty slot in `Markdown.viewBlocks` for top-level islands that render nothing, so returned nodes stay aligned with `document.blocks`.
7 changes: 4 additions & 3 deletions packages/effect/src/ui/html/Markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import * as Option from "../../Option.ts"
import * as Predicate from "../../Predicate.ts"
import * as Schema from "../../Schema.ts"
import type * as SchemaAST from "../../SchemaAST.ts"
import type * as View from "../View.ts"
import * as View from "../View.ts"
import type * as Html from "./Html.ts"
import { scheme } from "./internal/url.ts"

Expand Down Expand Up @@ -913,7 +913,8 @@ export const islandsFor = <

/**
* Renders a document's blocks, one node per top-level block, to place in
* your own container.
* your own container. An island that renders nothing keeps its position as
* an empty text node.
*
* @stability unstable
* @category views
Expand Down Expand Up @@ -1020,7 +1021,7 @@ export const viewBlocks = <Msg, R = never, InlineR = R>(
return island(node, config.islands, "islands", () => blocks(node.blocks))
}
}
return blocks(document.blocks)
return document.blocks.map((node) => block(node) ?? View.text(""))
}

/**
Expand Down
70 changes: 69 additions & 1 deletion packages/effect/test/ui/html/Markdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, describe, expect, it } from "@effect/vitest"
import { Context, Effect, Layer, Schema } from "effect"
import { Command, UiSchema } from "effect/ui"
import { Command, UiSchema, View } from "effect/ui"
import { Html, HtmlServer, Markdown, MarkdownParser } from "effect/ui/html"
import { HtmlLocator, HtmlPlay } from "effect/ui/html/testing"
import { Locator, TestHost } from "effect/ui/testing"
Expand Down Expand Up @@ -340,6 +340,74 @@ it.layer(TestHost.layer())("view", (it) => {
}))
})

describe("viewBlocks", () => {
const h = View.makeHelper<never, {}, Html.Elements>({}, Html.vocabulary)

const configs: Array<[string, Markdown.ViewConfig<never>]> = [
["without a view", {}],
["with invalid attributes", {
islands: Markdown.islandsFor(
{ BlockSlot: Schema.Struct({ level: Schema.String }) },
{ BlockSlot: (h, { level }) => h.span.children(level) }
)
}],
["returning undefined", { islands: { BlockSlot: () => undefined } }]
]

it.each(configs)("preserves the slot of an island %s", (_name, config) => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined)
try {
const document = parseMarkdown(
lines("# Title", "", "::BlockSlot", "", "First paragraph.", "", "Second paragraph.")
)
const nodes = Markdown.viewBlocks(h, document, config)

expect(nodes).toHaveLength(document.blocks.length)
assert.deepStrictEqual(nodes[1], View.text(""))
assert.deepStrictEqual(nodes.map((node) => View.textOf(View.toNode(node))), [
"Title",
"",
"First paragraph.",
"Second paragraph."
])
assert.deepStrictEqual(View.children(nodes).map(View.textOf), [
"Title",
"First paragraph.",
"Second paragraph."
])
} finally {
warn.mockRestore()
}
})

it("keeps whole-document occurrence indices when an island renders nothing", () => {
const codeIndices: Array<number> = []
const islandIndices: Array<number> = []
const document = parseMarkdown(
lines("```ts", "first", "```", "", "::Hidden", "", "::Hidden", "", "```ts", "second", "```")
)
const nodes = Markdown.viewBlocks(h, document, {
views: {
CodeBlock: (h, code, index) => {
codeIndices.push(index)
return Markdown.defaultViews.CodeBlock(h, code, index)
}
},
islands: {
Hidden: (h, _attributes, _content, index) => {
islandIndices.push(index)
return index === 0 ? undefined : h.aside.children("Shown")
}
}
})

expect(nodes).toHaveLength(document.blocks.length)
assert.deepStrictEqual(codeIndices, [0, 1])
assert.deepStrictEqual(islandIndices, [0, 1])
assert.deepStrictEqual(nodes.map((node) => View.textOf(View.toNode(node))), ["first", "", "Shown", "second"])
})
})

const CounterMessage = UiSchema.TaggedEnum({ Incremented: {} })

const Counter = Html.component({
Expand Down
Loading