Skip to content
Merged
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
3 changes: 3 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"marked": "catalog:",
"marked-katex-extension": "5.1.6",
"marked-shiki": "catalog:",
"morphdom": "2.7.8",
"remeda": "catalog:",
"shiki": "catalog:",
"solid-js": "catalog:",
Expand Down
55 changes: 49 additions & 6 deletions packages/ui/src/components/markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMarked } from "../context/marked"
import { useI18n } from "../context/i18n"
import DOMPurify from "dompurify"
import morphdom from "morphdom"
import { checksum } from "@opencode-ai/util/encode"
import { ComponentProps, createEffect, createResource, createSignal, onCleanup, splitProps } from "solid-js"
import { isServer } from "solid-js/web"
Expand Down Expand Up @@ -194,26 +195,68 @@ export function Markdown(
{ initialValue: "" },
)

let copySetupTimer: ReturnType<typeof setTimeout> | undefined
let copyCleanup: (() => void) | undefined

createEffect(() => {
const container = root()
const content = html()
if (!container) return
if (!content) return
if (isServer) return
const cleanup = setupCodeCopy(container, {
copy: i18n.t("ui.message.copy"),
copied: i18n.t("ui.message.copied"),

if (!content) {
container.innerHTML = ""
return
}

const temp = document.createElement("div")
temp.innerHTML = content

morphdom(container, temp, {
childrenOnly: true,
onBeforeElUpdated: (fromEl, toEl) => {
if (fromEl.isEqualNode(toEl)) return false
if (fromEl.getAttribute("data-component") === "markdown-code") {
const fromPre = fromEl.querySelector("pre")
const toPre = toEl.querySelector("pre")
if (fromPre && toPre && !fromPre.isEqualNode(toPre)) {
morphdom(fromPre, toPre)
}
return false
}
return true
},
onBeforeNodeDiscarded: (node) => {
if (node instanceof Element) {
if (node.getAttribute("data-slot") === "markdown-copy-button") return false
if (node.getAttribute("data-component") === "markdown-code") return false
}
return true
},
})
onCleanup(cleanup)

if (copySetupTimer) clearTimeout(copySetupTimer)
copySetupTimer = setTimeout(() => {
if (copyCleanup) copyCleanup()
copyCleanup = setupCodeCopy(container, {
copy: i18n.t("ui.message.copy"),
copied: i18n.t("ui.message.copied"),
})
}, 150)
})

onCleanup(() => {
if (copySetupTimer) clearTimeout(copySetupTimer)
if (copyCleanup) copyCleanup()
})

return (
<div
data-component="markdown"
classList={{
...(local.classList ?? {}),
[local.class ?? ""]: !!local.class,
}}
innerHTML={html.latest}
ref={setRoot}
{...others}
/>
Expand Down