From 40b7568837ac5f4bcd24f286e347b50c4de91f98 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 5 Aug 2024 04:43:06 +0530 Subject: [PATCH] Clean duplicate title shown in reference snippets of hierarchical docs Hierarchical documents like org-mode, markdown have their ancestry shown in first line. Remove it to show cleaner, deduplicated reference text from org-mode, markdown files --- .../app/components/chatMessage/chatMessage.tsx | 4 ---- .../components/referencePanel/referencePanel.tsx | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/interface/web/app/components/chatMessage/chatMessage.tsx b/src/interface/web/app/components/chatMessage/chatMessage.tsx index f54181413..9b2a2dcb0 100644 --- a/src/interface/web/app/components/chatMessage/chatMessage.tsx +++ b/src/interface/web/app/components/chatMessage/chatMessage.tsx @@ -252,15 +252,11 @@ export default function ChatMessage(props: ChatMessageProps) { useEffect(() => { const observer = new MutationObserver((mutationsList, observer) => { - console.log("called mutation observer"); // If the addedNodes property has one or more nodes if (messageRef.current) { for (let mutation of mutationsList) { if (mutation.type === "childList" && mutation.addedNodes.length > 0) { // Call your function here - - console.log("render katex in body"); - renderMathInElement(messageRef.current, { delimiters: [ { left: "$$", right: "$$", display: true }, diff --git a/src/interface/web/app/components/referencePanel/referencePanel.tsx b/src/interface/web/app/components/referencePanel/referencePanel.tsx index 6f9407395..8389e2bf1 100644 --- a/src/interface/web/app/components/referencePanel/referencePanel.tsx +++ b/src/interface/web/app/components/referencePanel/referencePanel.tsx @@ -35,11 +35,20 @@ interface NotesContextReferenceCardProps extends NotesContextReferenceData { showFullContent: boolean; } +function extractSnippet(props: NotesContextReferenceCardProps): string { + const hierarchicalFileExtensions = ["org", "md", "markdown"]; + const extension = props.title.split(".").pop() || ""; + const cleanContent = hierarchicalFileExtensions.includes(extension) + ? props.content.split("\n").slice(1).join("\n") + : props.content; + return props.showFullContent + ? DOMPurify.sanitize(md.render(cleanContent)) + : DOMPurify.sanitize(cleanContent); +} + function NotesContextReferenceCard(props: NotesContextReferenceCardProps) { - const snippet = props.showFullContent - ? DOMPurify.sanitize(md.render(props.content)) - : DOMPurify.sanitize(props.content); const fileIcon = getIconFromFilename(props.title || ".txt", "w-6 h-6 text-muted-foreground inline-flex mr-2"); + const snippet = extractSnippet(props); const [isHovering, setIsHovering] = useState(false); return (