import 'twin.macro'; import type * as m from 'mdast'; import tw from 'twin.macro'; import {createContext, memo, useContext, useState} from 'react'; import {CodeBlock, CodeTokenType, EnvironmentEditor} from './CodeBlock'; const HeadingContext = createContext(false); function useIsInHeading() { return useContext(HeadingContext); } const BlockquoteContext = createContext(false); function useIsInBlockquote() { return useContext(BlockquoteContext); } const nodeRenderers: { [TType in m.Content['type']]?: (props: { node: Extract; index: number; }) => React.ReactElement | null; } = { blockquote: DocumentBlockquote, heading: DocumentHeading, image: DocumentImage, inlineCode: DocumentInlineCode, link: DocumentLink, list: DocumentList, listItem: DocumentListItem, paragraph: DocumentParagraph, strong: DocumentStrong, emphasis: DocumentStrong, table: DocumentTable, text: DocumentText, }; const Document = memo(({document}: {document: m.Root}) => { return {document}; }); export default Document; function DocumentChildren({children}: {children: m.Parent}) { let index = 0; return ( <> {children.children.map((node) => { const currentIndex = index++; const preparedNode = (node as any).type === 'collapse' ? { ...node, heading: (node as any).heading.map((node: any) => ( )), body: (node as any).body.map((node: any) => ( )), } : node; return ( ); })} ); } function DocumentElement({ index, node, }: { index: number; node: | m.Content | { type: 'codeBlocks'; blocks: { lang: string; code: { type: CodeTokenType; value: string; }[]; }[]; } | {type: 'collapse'; heading: React.ReactNode; body: React.ReactNode}; }) { const isInBlockquote = useIsInBlockquote(); switch (node.type) { case 'codeBlocks': return ; case 'collapse': return ; default: const NodeRenderer: any = nodeRenderers[node.type]; if (NodeRenderer !== undefined) { return ; } throw new Error(`Unknown node type: ${(node as any).type}`); // return
{JSON.stringify(node, null, `  `)}
; } } function DocumentBlockquote({ index, node, }: { index: number; node: m.Blockquote; }) { return (
{node}
); } function DocumentHeading({index, node}: {index: number; node: m.Heading}) { const isInBlockquote = useIsInBlockquote(); const isInHeading = useIsInHeading(); if (!isInHeading) { return ( ); } const id: string = `${(node as any).id}`; const anchor = isInBlockquote ? null : ( <> ); switch (node.depth) { case 1: return (

{anchor} {node}

); case 2: return (

{anchor} {node}

); case 3: return (

{anchor} {node}

); case 4: return (

{anchor} {node}

); case 5: return (
{anchor} {node}
); default: return (
{anchor} {node}
); } } function DocumentImage({ node, }: { node: m.Image & {width?: number; height?: number}; }) { return ( {node.alt ); } function DocumentInlineCode({node}: {node: m.InlineCode}) { const isInHeading = useIsInHeading(); const isInBlockquote = useIsInBlockquote(); if (isInHeading) { return {node.value}; } if (node.value.startsWith(`@[[[`) && node.value.endsWith(`]]]@`)) { return ( {node.value.substring(`@[[[`.length, node.value.length - `]]]@`.length)} ); } if (/^\$[A-Z_]+$/.test(node.value)) { return ( ); } return ( {node.value} ); } function DocumentLink({node}: {node: m.Link}) { return ( {node} ); } function DocumentList({index, node}: {index: number; node: m.List}) { return node.ordered ? (
    {node}
) : (
    {node}
); } function DocumentListItem({index, node}: {index: number; node: m.ListItem}) { return (
  • {node}
  • ); } function DocumentParagraph({index, node}: {index: number; node: m.Paragraph}) { return (

    {node}

    ); } function DocumentStrong({node}: {node: m.Strong | m.Emphasis}) { return ( {node} ); } function DocumentTable({node}: {node: m.Table}) { return ( {node.children[0].children.map((cell, i) => ( ))} {node.children.slice(1).map((row, i) => ( {row.children.map((cell, i) => ( ))} ))}
    {cell}
    {cell}
    ); } function DocumentText({node}: {node: m.Text}) { return <>{node.value}; } function Collapse({ heading, body, }: { heading: React.ReactNode; body: React.ReactNode; }) { const [collapsed, setCollapsed] = useState(true); return ( <>
    {heading}
    {collapsed ? null :
    {body}
    } ); }