Skip to content

Commit

Permalink
add preview html code
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Jul 23, 2024
1 parent 00be2be commit 3a10f58
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,49 @@ export function Mermaid(props: { code: string }) {
);
}

export function HTMLPreview(props: { code: string }) {
const ref = useRef<HTMLDivElement>(null);

return (
<div
className="no-dark html"
style={{
cursor: "pointer",
overflow: "auto",
}}
ref={ref}
onClick={() => console.log("click")}
>
<iframe
frameBorder={0}
sandbox="allow-scripts"
style={{ width: "100%", height: 400 }}
srcdoc={props.code}
></iframe>
</div>
);
}

export function PreCode(props: { children: any }) {
const ref = useRef<HTMLPreElement>(null);
const refText = ref.current?.innerText;
const [mermaidCode, setMermaidCode] = useState("");
const [htmlCode, setHtmlCode] = useState("");

const renderMermaid = useDebouncedCallback(() => {
const renderArtifacts = useDebouncedCallback(() => {
if (!ref.current) return;
const mermaidDom = ref.current.querySelector("code.language-mermaid");
if (mermaidDom) {
setMermaidCode((mermaidDom as HTMLElement).innerText);
}
const htmlDom = ref.current.querySelector("code.language-html");
if (htmlDom) {
setHtmlCode((htmlDom as HTMLElement).innerText);
}
}, 600);

useEffect(() => {
setTimeout(renderMermaid, 1);
setTimeout(renderArtifacts, 1);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [refText]);

Expand All @@ -83,6 +111,7 @@ export function PreCode(props: { children: any }) {
{mermaidCode.length > 0 && (
<Mermaid code={mermaidCode} key={mermaidCode} />
)}
{htmlCode.length > 0 && <HTMLPreview code={htmlCode} key={htmlCode} />}
<pre ref={ref}>
<span
className="copy-code-button"
Expand Down

0 comments on commit 3a10f58

Please sign in to comment.