Skip to content

Commit

Permalink
fix(search): empty mindmap note breaking search (closes #1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Feb 3, 2025
1 parent 438f28b commit 79e830b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 46 deletions.
13 changes: 13 additions & 0 deletions src/services/search/expressions/note_content_fulltext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, it, expect } from "vitest";
import { processMindmapContent } from "./note_content_fulltext.js";

describe("processMindmapContent", () => {
it("supports empty JSON", () => {
expect(processMindmapContent("{}")).toEqual("");
});

it("supports blank text / invalid JSON", () => {
expect(processMindmapContent("")).toEqual("");
expect(processMindmapContent(`{ "node": " }`)).toEqual("");
});
});
106 changes: 60 additions & 46 deletions src/services/search/expressions/note_content_fulltext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,52 +131,7 @@ class NoteContentFulltextExp extends Expression {

content = content.replace(/ /g, " ");
} else if (type === "mindMap" && mime === "application/json") {
let mindMapcontent = JSON.parse(content);

// Define interfaces for the JSON structure
interface MindmapNode {
id: string;
topic: string;
children: MindmapNode[]; // Recursive structure
direction?: number;
expanded?: boolean;
}

interface MindmapData {
nodedata: MindmapNode;
arrows: any[]; // If you know the structure, replace `any` with the correct type
summaries: any[];
direction: number;
theme: {
name: string;
type: string;
palette: string[];
cssvar: Record<string, string>; // Object with string keys and string values
};
}

// Recursive function to collect all topics
function collectTopics(node: MindmapNode): string[] {
// Collect the current node's topic
let topics = [node.topic];

// If the node has children, collect topics recursively
if (node.children && node.children.length > 0) {
for (const child of node.children) {
topics = topics.concat(collectTopics(child));
}
}

return topics;
}

// Start extracting from the root node
const topicsArray = collectTopics(mindMapcontent.nodedata);

// Combine topics into a single string
const topicsString = topicsArray.join(", ");

content = normalize(topicsString.toString());
content = processMindmapContent(content);
} else if (type === "canvas" && mime === "application/json") {
interface Element {
type: string;
Expand Down Expand Up @@ -215,4 +170,63 @@ class NoteContentFulltextExp extends Expression {
}
}

export function processMindmapContent(content: string) {
let mindMapcontent;

try {
mindMapcontent = JSON.parse(content);
} catch (e) {
return "";
}

// Define interfaces for the JSON structure
interface MindmapNode {
id: string;
topic: string;
children: MindmapNode[]; // Recursive structure
direction?: number;
expanded?: boolean;
}

interface MindmapData {
nodedata: MindmapNode;
arrows: any[]; // If you know the structure, replace `any` with the correct type
summaries: any[];
direction: number;
theme: {
name: string;
type: string;
palette: string[];
cssvar: Record<string, string>; // Object with string keys and string values
};
}

// Recursive function to collect all topics
function collectTopics(node?: MindmapNode): string[] {
if (!node) {
return [];
}

// Collect the current node's topic
let topics = [node.topic];

// If the node has children, collect topics recursively
if (node.children && node.children.length > 0) {
for (const child of node.children) {
topics = topics.concat(collectTopics(child));
}
}

return topics;
}

// Start extracting from the root node
const topicsArray = collectTopics(mindMapcontent.nodedata);

// Combine topics into a single string
const topicsString = topicsArray.join(", ");

return normalize(topicsString.toString());
}

export default NoteContentFulltextExp;

0 comments on commit 79e830b

Please sign in to comment.