"use client"; import { Accordion, AccordionContent } from "@/lib/accordion"; import { Poppable } from "@/lib/poppables/components/poppable"; import { createElements } from "@/lib/tcmd"; import Link from "next/link"; import React, { FC, Fragment, ReactNode, use, useMemo } from "react"; import { sanitize } from "isomorphic-dompurify"; export const TCMD: FC<{ body: Promise }> = ({ body }) => { const text = use(body); const elements = useMemo(() => createElements(text), [text]); return ( //
//
{JSON.stringify(elements,null,2)}
//
{elements.map((e, i) => {renderBlock(e)} )}
); }; const renderBlock = (block: BlockChildren): ReactNode => { switch (block.type) { case "block": return block.children.map((e, i) => ( {renderBlock(e)} )); case "grid": return (
{block.children.map((c, i) => (
{renderBlock(c)}
))}
); case "card": return (
{block.children.map((e, i) => ( {renderBlock(e)} ))}
); case "accordion": return ( {block.children.map((e, i) => ( {renderBlock(e)} ))} ); default: return ( renderParagraph(block as ParagraphToken) ); } }; const renderParagraph = (p: ParagraphToken) => { switch (p.type) { case "p": return (
{p.content.map((e, i) => ( {renderToken(e)} ))}
); case "code": return (
          {p.content.map((c) => c.line.toString()).join("\n\n")}
        
); default: return (
Block or paragraph missing implementation: {p.type}
); } }; const renderToken = (t: Token) => { switch (t.type) { case "h1": return (
{renderInlineToken(t.line)}
); case "h2": return (
{renderInlineToken(t.line)}
); case "h3": return (
{renderInlineToken(t.line)}
); case "p": return (
{t.lines.map((e, i) => ( {renderInlineToken(e.line)} ))}
); case "text": return ( <> {renderInlineToken(t.line)}   ); case "list1": return
  • {renderInlineToken(t.line)}
  • ; case "list2": return
  • {renderInlineToken(t.line)}
  • ; default: return (
    Missing implementation for tcMD element `{(t as { type: string }) .type}`
    ); } }; const renderInlineToken = (l: Line) => { if (typeof l === "string") return l; return l.map((token) => ( {(() => { switch (token.type) { case "text": return {token.content}; case "bold": return {token.content}; case "anchor": return ( {token.content} ); case "image": { token.data.src = token.data.src as string; if (token.data.src.startsWith(" ); } // eslint-disable-next-line @next/next/no-img-element return {token.content}; } case "popover": return ( {token.content} ); default: return ( Inline element not implemented: {token.type} ); } })()} )); };