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