diff options
Diffstat (limited to 'util/processOrg.tsx')
-rw-r--r-- | util/processOrg.tsx | 121 |
1 files changed, 79 insertions, 42 deletions
diff --git a/util/processOrg.tsx b/util/processOrg.tsx index 26d9377..fa135a1 100644 --- a/util/processOrg.tsx +++ b/util/processOrg.tsx @@ -33,6 +33,10 @@ import { Section } from '../components/Sidebar/Section' import { NoteContext } from './NoteContext' import { OrgRoamLink, OrgRoamNode } from '../api' +// @ts-expect-error non-ESM unified means no types +import { toString } from 'hast-util-to-string' +import { Box, chakra } from '@chakra-ui/react' + export interface ProcessedOrgProps { nodeById: NodeById previewNode: OrgRoamNode @@ -44,6 +48,8 @@ export interface ProcessedOrgProps { outline: boolean collapse: boolean linksByNodeId: LinksByNodeId + macros: { [key: string]: string } | {} + attachDir: string } export const ProcessedOrg = (props: ProcessedOrgProps) => { @@ -58,9 +64,10 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { outline, collapse, linksByNodeId, + macros, + attachDir, } = props - console.log(linksByNodeId) - console.log(previewNode) + if (!previewNode || !linksByNodeId) { return null } @@ -68,7 +75,9 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { const orgProcessor = unified() .use(uniorgParse) .use(extractKeywords) - .use(attachments) + .use(attachments, { + idDir: attachDir || undefined, + }) .use(uniorgSlug) .use(uniorg2rehype, { useSections: true }) @@ -85,8 +94,6 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { const linkEntries = Object.entries(nodesInNote) const wikiLinkResolver = (wikiLink: string): string[] => { const entry = linkEntries.find((idNodeArray) => { - console.log(idNodeArray) - console.log(wikiLink) return idNodeArray?.[1]?.title === wikiLink }) const id = entry?.[0] ?? '' @@ -94,7 +101,6 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { } const wikiLinkProcessor = (wikiLink: string): string => { - console.log(wikiLink) return `id:${wikiLink}` } @@ -117,43 +123,77 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { const isMarkdown = previewNode?.file?.slice(-3) === '.md' const baseProcessor = isMarkdown ? mdProcessor : orgProcessor + console.log(macros) const processor = useMemo( () => - baseProcessor.use(katex).use(rehype2react, { - createElement: React.createElement, - // eslint-disable-next-line react/display-name - components: { - a: ({ children, href }) => { - return ( - <PreviewLink - nodeByCite={nodeByCite} - setSidebarHighlightedNode={setSidebarHighlightedNode} - href={`${href as string}`} - nodeById={nodeById} - linksByNodeId={linksByNodeId} - setPreviewNode={setPreviewNode} - openContextMenu={openContextMenu} - outline={outline} - previewNode={previewNode} - isWiki={isMarkdown} - > - {children} - </PreviewLink> - ) + baseProcessor + .use(katex, { + trust: (context) => ['\\htmlId', '\\href'].includes(context.command), + macros: { + '\\eqref': '\\href{###1}{(\\text{#1})}', + '\\ref': '\\href{###1}{\\text{#1}}', + '\\label': '\\htmlId{#1}{}', + // '\\weird': '\\textbf{#1}', + ...macros, }, - img: ({ src }) => { - return <OrgImage src={src as string} file={previewNode?.file} /> - }, - section: ({ children, className }) => ( - <Section {...{ outline, collapse }} className={className as string}> - {children} - </Section> - ), - p: ({ children }) => { - return <p lang="en">{children as ReactNode}</p> + }) + .use(rehype2react, { + createElement: React.createElement, + // eslint-disable-next-line react/display-name + components: { + a: ({ children, href }) => { + return ( + <PreviewLink + nodeByCite={nodeByCite} + setSidebarHighlightedNode={setSidebarHighlightedNode} + href={`${href as string}`} + nodeById={nodeById} + linksByNodeId={linksByNodeId} + setPreviewNode={setPreviewNode} + openContextMenu={openContextMenu} + outline={outline} + previewNode={previewNode} + isWiki={isMarkdown} + macros={macros} + attachDir={attachDir} + > + {children} + </PreviewLink> + ) + }, + img: ({ src }) => { + return <OrgImage src={src as string} file={previewNode?.file} /> + }, + section: ({ children, className }) => { + if (className && (className as string).slice(-1) === `${previewNode.level}`) { + return <Box>{(children as React.ReactElement[]).slice(1)}</Box> + } + return ( + <Section {...{ outline, collapse }} className={className as string}> + {children} + </Section> + ) + }, + blockquote: ({ children }) => ( + <chakra.blockquote + color="gray.800" + bgColor="gray.300" + pt={4} + pb={2} + mb={4} + mt={3} + pl={4} + borderLeftWidth={4} + borderLeftColor="gray.700" + > + {children as React.ReactElement[]} + </chakra.blockquote> + ), + p: ({ children }) => { + return <p lang="en">{children as ReactNode}</p> + }, }, - }, - }), + }), [previewNode?.id], ) @@ -162,6 +202,3 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { <NoteContext.Provider value={{ collapse, outline }}>{text as ReactNode}</NoteContext.Provider> ) } -function useCallBack(arg0: () => unified.Processor<unified.Settings>) { - throw new Error('Function not implemented.') -} |