fix: prevent HTML-escaping of inline style and script content in htmlToJsx

Add dangerouslySetInnerHTML overrides for <style> and <script> elements
so that CSS/JS injected by tree transforms is not HTML-escaped during
preact-render-to-string serialization.
This commit is contained in:
saberzero1 2026-03-08 20:03:22 +01:00
parent 6c78b5f6c1
commit cb242ab386
No known key found for this signature in database

View File

@ -1,15 +1,26 @@
import { Components, Jsx, toJsxRuntime } from "hast-util-to-jsx-runtime"
import { Node, Root } from "hast"
import { Fragment, jsx, jsxs } from "preact/jsx-runtime"
import { h } from "preact"
import { trace } from "./trace"
import { type FilePath } from "./path"
function childrenToString(children: unknown): string {
if (typeof children === "string") return children
if (Array.isArray(children)) return children.map(childrenToString).join("")
return String(children ?? "")
}
const customComponents: Components = {
table: (props) => (
<div class="table-container">
<table {...props} />
</div>
),
style: ({ children, ...rest }) =>
h("style", { ...rest, dangerouslySetInnerHTML: { __html: childrenToString(children) } }),
script: ({ children, ...rest }) =>
h("script", { ...rest, dangerouslySetInnerHTML: { __html: childrenToString(children) } }),
}
export function htmlToJsx(fp: FilePath, tree: Node) {