From 7429a6c13a6d60104d642cce9f8a6ac7222dbc00 Mon Sep 17 00:00:00 2001 From: clean99 <53188310+clean99@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:57:20 +0800 Subject: [PATCH] Preserve top-level Markdown block slots --- .changeset/tidy-islands-align.md | 5 ++ packages/effect/src/ui/html/Markdown.ts | 7 +- packages/effect/test/ui/html/Markdown.test.ts | 70 ++++++++++++++++++- 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 .changeset/tidy-islands-align.md diff --git a/.changeset/tidy-islands-align.md b/.changeset/tidy-islands-align.md new file mode 100644 index 00000000000..33e63cd9b8e --- /dev/null +++ b/.changeset/tidy-islands-align.md @@ -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`. diff --git a/packages/effect/src/ui/html/Markdown.ts b/packages/effect/src/ui/html/Markdown.ts index f4f0d6cc34a..49627daead1 100644 --- a/packages/effect/src/ui/html/Markdown.ts +++ b/packages/effect/src/ui/html/Markdown.ts @@ -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" @@ -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 @@ -1020,7 +1021,7 @@ export const viewBlocks = ( return island(node, config.islands, "islands", () => blocks(node.blocks)) } } - return blocks(document.blocks) + return document.blocks.map((node) => block(node) ?? View.text("")) } /** diff --git a/packages/effect/test/ui/html/Markdown.test.ts b/packages/effect/test/ui/html/Markdown.test.ts index 4aced0b53b5..5b4d141b7f0 100644 --- a/packages/effect/test/ui/html/Markdown.test.ts +++ b/packages/effect/test/ui/html/Markdown.test.ts @@ -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" @@ -340,6 +340,74 @@ it.layer(TestHost.layer())("view", (it) => { })) }) +describe("viewBlocks", () => { + const h = View.makeHelper({}, Html.vocabulary) + + const configs: Array<[string, Markdown.ViewConfig]> = [ + ["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 = [] + const islandIndices: Array = [] + 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({