From 5b6eb504587df592e42690743bce4b41d4066d00 Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Sat, 13 Nov 2021 01:32:10 +0100 Subject: Feat: Markdown file previewing (#152) * feat(md): initial support for md-roam * feat(md): link support and more for md * fix(md): add missing remark math package --- components/Sidebar/Backlinks.tsx | 1 + components/Sidebar/Link.tsx | 55 +++++++++++++------------------------ components/Sidebar/Note.tsx | 7 +++-- components/Sidebar/index.tsx | 58 +++++++++++++++++++++------------------- 4 files changed, 54 insertions(+), 67 deletions(-) (limited to 'components') diff --git a/components/Sidebar/Backlinks.tsx b/components/Sidebar/Backlinks.tsx index e115225..892af8c 100644 --- a/components/Sidebar/Backlinks.tsx +++ b/components/Sidebar/Backlinks.tsx @@ -59,6 +59,7 @@ export const Backlinks = (props: BacklinksProps) => { return ( { const { @@ -74,6 +78,7 @@ export const NodeLink = (props: NodeLinkProps) => { openContextMenu, href, children, + isWiki, } = props const { highlightColor } = useContext(ThemeContext) @@ -82,8 +87,10 @@ export const NodeLink = (props: NodeLinkProps) => { const type = href.replaceAll(/(.*?)\:?.*/g, '$1') const uri = href.replaceAll(/.*?\:(.*)/g, '$1') const ID = id ?? uri + const linkText = isWiki ? `[[${children}]]` : children return ( setSidebarHighlightedNode(nodeById[ID])} onMouseLeave={() => setSidebarHighlightedNode({})} tabIndex={0} @@ -101,7 +108,7 @@ export const NodeLink = (props: NodeLinkProps) => { _hover={{ textDecoration: 'none', cursor: 'pointer', bgColor: coolHighlightColor + '22' }} _focus={{ outlineColor: highlightColor }} > - {children} + {linkText} ) } @@ -129,6 +136,8 @@ export const PreviewLink = (props: LinkProps) => { openContextMenu, outline, noUnderline, + linksByNodeId, + isWiki, } = props // TODO figure out how to properly type this // see https://github.com/rehypejs/rehype-react/issues/25 @@ -137,6 +146,7 @@ export const PreviewLink = (props: LinkProps) => { const type = href.replaceAll(/(.*?)\:.*/g, '$1') const extraNoteStyle = outline ? outlineNoteStyle : viewerNoteStyle + console.log(previewNode) const getText = () => { fetch(`http://localhost:35901/file/${file}`) .then((res) => { @@ -153,6 +163,7 @@ export const PreviewLink = (props: LinkProps) => { 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.' }) } + useEffect(() => { if (type.replaceAll(/(http)?.*/g, '$1')) { return @@ -165,6 +176,7 @@ export const PreviewLink = (props: LinkProps) => { } getText() }, [hover, orgText]) + if (!type) { return {children} } @@ -195,39 +207,6 @@ export const PreviewLink = (props: LinkProps) => { const id = getId(type, uri) const file = encodeURIComponent(encodeURIComponent(nodeById[id]?.file as string)) - /* const processor = unified() - * .use(uniorgParse) - * .use(uniorg2rehype) - * .use(katex) - * .use(rehype2react, { - * createElement: React.createElement, - * components: { - * // eslint-disable-next-line react/display-name - * a: ({ children, href }) => ( - * - * {children} - * - * ), - * img: ({ src }) => { - * return - * }, - * section: ({ children, className }) => ( - *
{children}
- * ), - * p: ({ children }) => { - * return

{children}

- * }, - * }, - * }) - */ if (id) { return ( <> @@ -250,6 +229,7 @@ export const PreviewLink = (props: LinkProps) => { nodeByCite, openContextMenu, noUnderline, + isWiki, }} />
@@ -307,10 +287,11 @@ export const PreviewLink = (props: LinkProps) => { setSidebarHighlightedNode, setPreviewNode, nodeByCite, - previewNode, openContextMenu, outline, + linksByNodeId, }} + previewNode={nodeById[id]!} collapse={false} /> @@ -323,7 +304,7 @@ export const PreviewLink = (props: LinkProps) => { ) } return ( - + {children} ) diff --git a/components/Sidebar/Note.tsx b/components/Sidebar/Note.tsx index 197fa76..62cc8ad 100644 --- a/components/Sidebar/Note.tsx +++ b/components/Sidebar/Note.tsx @@ -1,11 +1,12 @@ import React from 'react' import { NodeObject } from 'force-graph' -import { NodeById, NodeByCite, LinksByNodeId } from '../../pages' +import { NodeById, NodeByCite, LinksByNodeId, normalizeLinkEnds } from '../../pages' import { Box, Flex } from '@chakra-ui/react' import { UniOrg } from '../../util/uniorg' import { Backlinks } from '../../components/Sidebar/Backlinks' import { defaultNoteStyle, viewerNoteStyle, outlineNoteStyle } from './noteStyle' +import { OrgRoamLink } from '../../api' export interface NoteProps { setPreviewNode: any @@ -35,6 +36,7 @@ export const Note = (props: NoteProps) => { outline, collapse, } = props + const extraStyle = outline ? outlineNoteStyle : viewerNoteStyle return ( { {...{ setPreviewNode, previewNode, - nodeById, nodeByCite, setSidebarHighlightedNode, openContextMenu, outline, collapse, + nodeById, + linksByNodeId, }} /> { /> )} > - - - <TagBar - {...{ filter, setFilter, tagColors, setTagColors, openContextMenu, previewNode }} - /> - <Note - {...{ - setPreviewNode, - previewNode, - nodeById, - nodeByCite, - setSidebarHighlightedNode, - justification, - justificationList, - linksByNodeId, - openContextMenu, - outline, - setOutline, - collapse, - }} - /> - </VStack> + {previewRoamNode && ( + <VStack + flexGrow={1} + // overflowY="scroll" + alignItems="left" + bg="alt.100" + paddingLeft={4} + > + <Title previewNode={previewRoamNode} /> + <TagBar + {...{ filter, setFilter, tagColors, setTagColors, openContextMenu, previewNode }} + /> + <Note + {...{ + setPreviewNode, + previewNode, + nodeById, + nodeByCite, + setSidebarHighlightedNode, + justification, + justificationList, + linksByNodeId, + openContextMenu, + outline, + setOutline, + collapse, + }} + /> + </VStack> + )} </Scrollbars> </Flex> </Resizable> -- cgit v1.2.3