Skip to content

Commit 79e830b

Browse files
committed
fix(search): empty mindmap note breaking search (closes #1107)
1 parent 438f28b commit 79e830b

File tree

2 files changed

+73
-46
lines changed

2 files changed

+73
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, it, expect } from "vitest";
2+
import { processMindmapContent } from "./note_content_fulltext.js";
3+
4+
describe("processMindmapContent", () => {
5+
it("supports empty JSON", () => {
6+
expect(processMindmapContent("{}")).toEqual("");
7+
});
8+
9+
it("supports blank text / invalid JSON", () => {
10+
expect(processMindmapContent("")).toEqual("");
11+
expect(processMindmapContent(`{ "node": " }`)).toEqual("");
12+
});
13+
});

src/services/search/expressions/note_content_fulltext.ts

+60-46
Original file line numberDiff line numberDiff line change
@@ -131,52 +131,7 @@ class NoteContentFulltextExp extends Expression {
131131

132132
content = content.replace(/ /g, " ");
133133
} else if (type === "mindMap" && mime === "application/json") {
134-
let mindMapcontent = JSON.parse(content);
135-
136-
// Define interfaces for the JSON structure
137-
interface MindmapNode {
138-
id: string;
139-
topic: string;
140-
children: MindmapNode[]; // Recursive structure
141-
direction?: number;
142-
expanded?: boolean;
143-
}
144-
145-
interface MindmapData {
146-
nodedata: MindmapNode;
147-
arrows: any[]; // If you know the structure, replace `any` with the correct type
148-
summaries: any[];
149-
direction: number;
150-
theme: {
151-
name: string;
152-
type: string;
153-
palette: string[];
154-
cssvar: Record<string, string>; // Object with string keys and string values
155-
};
156-
}
157-
158-
// Recursive function to collect all topics
159-
function collectTopics(node: MindmapNode): string[] {
160-
// Collect the current node's topic
161-
let topics = [node.topic];
162-
163-
// If the node has children, collect topics recursively
164-
if (node.children && node.children.length > 0) {
165-
for (const child of node.children) {
166-
topics = topics.concat(collectTopics(child));
167-
}
168-
}
169-
170-
return topics;
171-
}
172-
173-
// Start extracting from the root node
174-
const topicsArray = collectTopics(mindMapcontent.nodedata);
175-
176-
// Combine topics into a single string
177-
const topicsString = topicsArray.join(", ");
178-
179-
content = normalize(topicsString.toString());
134+
content = processMindmapContent(content);
180135
} else if (type === "canvas" && mime === "application/json") {
181136
interface Element {
182137
type: string;
@@ -215,4 +170,63 @@ class NoteContentFulltextExp extends Expression {
215170
}
216171
}
217172

173+
export function processMindmapContent(content: string) {
174+
let mindMapcontent;
175+
176+
try {
177+
mindMapcontent = JSON.parse(content);
178+
} catch (e) {
179+
return "";
180+
}
181+
182+
// Define interfaces for the JSON structure
183+
interface MindmapNode {
184+
id: string;
185+
topic: string;
186+
children: MindmapNode[]; // Recursive structure
187+
direction?: number;
188+
expanded?: boolean;
189+
}
190+
191+
interface MindmapData {
192+
nodedata: MindmapNode;
193+
arrows: any[]; // If you know the structure, replace `any` with the correct type
194+
summaries: any[];
195+
direction: number;
196+
theme: {
197+
name: string;
198+
type: string;
199+
palette: string[];
200+
cssvar: Record<string, string>; // Object with string keys and string values
201+
};
202+
}
203+
204+
// Recursive function to collect all topics
205+
function collectTopics(node?: MindmapNode): string[] {
206+
if (!node) {
207+
return [];
208+
}
209+
210+
// Collect the current node's topic
211+
let topics = [node.topic];
212+
213+
// If the node has children, collect topics recursively
214+
if (node.children && node.children.length > 0) {
215+
for (const child of node.children) {
216+
topics = topics.concat(collectTopics(child));
217+
}
218+
}
219+
220+
return topics;
221+
}
222+
223+
// Start extracting from the root node
224+
const topicsArray = collectTopics(mindMapcontent.nodedata);
225+
226+
// Combine topics into a single string
227+
const topicsString = topicsArray.join(", ");
228+
229+
return normalize(topicsString.toString());
230+
}
231+
218232
export default NoteContentFulltextExp;

0 commit comments

Comments
 (0)