2023-11-12 02:41:44 +07:00
|
|
|
import { Components, Jsx, toJsxRuntime } from "hast-util-to-jsx-runtime"
|
2023-10-22 11:05:46 +07:00
|
|
|
import { Node, Root } from "hast"
|
|
|
|
import { Fragment, jsx, jsxs } from "preact/jsx-runtime"
|
|
|
|
import { trace } from "./trace"
|
|
|
|
import { type FilePath } from "./path"
|
|
|
|
|
2023-11-12 02:39:56 +07:00
|
|
|
const customComponents: Components = {
|
|
|
|
table: (props) => (
|
|
|
|
<div class="table-container">
|
|
|
|
<table {...props} />
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2023-12-19 00:48:40 +07:00
|
|
|
export function htmlToJsx(fp: FilePath, tree: Node) {
|
2023-10-22 11:05:46 +07:00
|
|
|
try {
|
2023-11-12 02:39:56 +07:00
|
|
|
return toJsxRuntime(tree as Root, {
|
|
|
|
Fragment,
|
2023-11-12 02:41:44 +07:00
|
|
|
jsx: jsx as Jsx,
|
|
|
|
jsxs: jsxs as Jsx,
|
2023-11-12 02:39:56 +07:00
|
|
|
elementAttributeNameCase: "html",
|
|
|
|
components: customComponents,
|
|
|
|
})
|
2023-10-22 11:05:46 +07:00
|
|
|
} catch (e) {
|
|
|
|
trace(`Failed to parse Markdown in \`${fp}\` into JSX`, e as Error)
|
|
|
|
}
|
|
|
|
}
|