diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/processOrg.tsx | 75 | ||||
-rw-r--r-- | util/uniorg.tsx | 61 | ||||
-rw-r--r-- | util/webSocketFunctions.ts | 25 |
3 files changed, 161 insertions, 0 deletions
diff --git a/util/processOrg.tsx b/util/processOrg.tsx new file mode 100644 index 0000000..3fc13dc --- /dev/null +++ b/util/processOrg.tsx @@ -0,0 +1,75 @@ +import unified from 'unified' +//import createStream from 'unified-stream' +import uniorgParse from 'uniorg-parse' +import uniorg2rehype from 'uniorg-rehype' +import uniorgSlug from 'uniorg-slug' +import extractKeywords from 'uniorg-extract-keywords' +import attachments from 'uniorg-attach' +// rehypeHighlight does not have any types +// add error thing here +// import highlight from 'rehype-highlight' +import katex from 'rehype-katex' +import 'katex/dist/katex.css' +import rehype2react from 'rehype-react' + +import { PreviewLink } from '../components/Sidebar/Link' +import { NodeByCite, NodeById } from '../pages' +import React, { useMemo } from 'react' +import { OrgImage } from '../components/Sidebar/OrgImage' + +export interface ProcessedOrgProps { + nodeById: NodeById + previewNode: any + setPreviewNode: any + previewText: any + nodeByCite: NodeByCite + setSidebarHighlightedNode: any + openContextMenu: any +} + +export const ProcessedOrg = (props: ProcessedOrgProps) => { + const { + nodeById, + setSidebarHighlightedNode, + setPreviewNode, + previewText, + nodeByCite, + previewNode, + openContextMenu, + } = props + + const processor = unified() + .use(uniorgParse) + .use(extractKeywords) + .use(attachments) + .use(uniorgSlug) + .use(uniorg2rehype) + // .use(highlight) + .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} + setPreviewNode={setPreviewNode} + openContextMenu={openContextMenu} + > + {children} + </PreviewLink> + ) + }, + img: ({ src }) => { + return <OrgImage src={src as string} file={previewNode.file} /> + }, + }, + }) + + const text = useMemo(() => processor.processSync(previewText).result, [previewText]) + return <>{text}</> +} diff --git a/util/uniorg.tsx b/util/uniorg.tsx new file mode 100644 index 0000000..8802dd1 --- /dev/null +++ b/util/uniorg.tsx @@ -0,0 +1,61 @@ +import React, { useEffect, useMemo, useState } from 'react' +import { OrgRoamNode } from '../api' +import { NodeByCite, NodeById } from '../pages/index' +import { ProcessedOrg } from './processOrg' + +export interface UniOrgProps { + nodeById: NodeById + previewNode: any + setPreviewNode: any + nodeByCite: NodeByCite + setSidebarHighlightedNode: any + openContextMenu: any +} + +export const UniOrg = (props: UniOrgProps) => { + const { + openContextMenu, + setSidebarHighlightedNode, + nodeById, + nodeByCite, + previewNode, + setPreviewNode, + } = props + + const [previewText, setPreviewText] = useState('') + + const file = encodeURIComponent(encodeURIComponent(previewNode.file)) + useEffect(() => { + fetch(`http://localhost:35901/file/${file}`) + .then((res) => { + return res.text() + }) + .then((res) => { + if (res !== 'error') { + setPreviewText(res) + } + }) + .catch((e) => { + console.log(e) + return 'Could not fetch the text for some reason, sorry!\n\n This can happen because you have an id with forward slashes (/) in it.' + }) + }, [previewNode.id]) + + return ( + <> + {previewNode?.id && ( + <ProcessedOrg + {...{ + nodeById, + previewNode, + setPreviewNode, + previewText, + nodeByCite, + setSidebarHighlightedNode, + openContextMenu, + }} + /> + )} + </> + ) +} diff --git a/util/webSocketFunctions.ts b/util/webSocketFunctions.ts new file mode 100644 index 0000000..8e2bd7e --- /dev/null +++ b/util/webSocketFunctions.ts @@ -0,0 +1,25 @@ +import { OrgRoamNode } from '../api' +import ReconnectingWebSocket from 'reconnecting-websocket' + +export function sendMessageToEmacs(command: string, data: {}, webSocket: ReconnectingWebSocket) { + webSocket.send(JSON.stringify({ command: command, data: data })) +} + +export function getOrgText(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + sendMessageToEmacs('getText', { id: node.id }, webSocket) +} + +export function openNodeInEmacs(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + sendMessageToEmacs('open', { id: node.id }, webSocket) +} + +export function deleteNodeInEmacs(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + if (node.level !== 0) { + return + } + sendMessageToEmacs('delete', { id: node.id, file: node.file }, webSocket) +} + +export function createNodeInEmacs(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + sendMessageToEmacs('create', { id: node.id, title: node.title, ref: node.properties.ROAM_REFS }, webSocket) +} |