Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: add raw html content with highlighting in results

Abdullah Atta0faf26773514
diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts
index 00bebfbf9..e43bd96d7 100644
--- a/packages/core/src/api/lookup.ts
+++ b/packages/core/src/api/lookup.ts
@@ -53,6 +53,14 @@ type FuzzySearchField<T> = {
column: AnyColumnWithTable<DatabaseSchema, keyof DatabaseSchema>;
ignore?: boolean;
};
+
+const MATCH_TAG_NAME = "nn-search-result";
+const MATCH_TAG_OPEN = `<${MATCH_TAG_NAME}>`;
+const MATCH_TAG_CLOSE = `</${MATCH_TAG_NAME}>`;
+const MATCH_TAG_REGEX = new RegExp(
+ `<${MATCH_TAG_NAME}>(.*?)<\\/${MATCH_TAG_NAME}>`,
+ "gm"
+);
export default class Lookup {
constructor(private readonly db: Database) {}
@@ -83,7 +91,7 @@ export default class Lookup {
.select([
"id",
sql<string>`'title'`.as("type"),
- sql<string>`highlight(notes_fts, 1, '<nnmark>', '</nnmark>')`.as(
+ sql<string>`highlight(notes_fts, 1, '<nn-search-result>', '</nn-search-result>')`.as(
"match"
),
sql<number>`rank * 10`.as("rank")
@@ -101,7 +109,7 @@ export default class Lookup {
.select([
"noteId as id",
sql<string>`'content'`.as("type"),
- sql<string>`highlight(content_fts, 2, '<nnmark>', '</nnmark>')`.as(
+ sql<string>`highlight(content_fts, 2, '<nn-search-result>', '</nn-search-result>')`.as(
"match"
),
"rank"
@@ -130,15 +138,19 @@ export default class Lookup {
id: result.id,
content: [],
title: [],
- rank: 0
+ rank: 0,
+ rawContent: ""
});
- if (result.type === "content")
- old.content = extractMatchingBlocks(result.match, "nnmark").flatMap(
- (block) => {
- return splitHighlightedMatch(block);
- }
- );
+ if (result.type === "content") {
+ old.content = extractMatchingBlocks(
+ result.match,
+ MATCH_TAG_NAME
+ ).flatMap((block) => {
+ return splitHighlightedMatch(block);
+ });
+ old.rawContent = result.match;
+ }
if (result.type === "title")
old.title = splitHighlightedMatch(result.match).flatMap((m) => m);
old.rank += result.rank;
@@ -491,7 +503,7 @@ function highlightQueries(text: string, queries: string[]): string {
if (start > lastEnd) {
parts.push(text.slice(lastEnd, start));
}
- parts.push("<nnmark>", text.slice(start, end), "</nnmark>");
+ parts.push(MATCH_TAG_OPEN, text.slice(start, end), MATCH_TAG_CLOSE);
lastEnd = end;
}
@@ -522,7 +534,7 @@ function arrayToVirtualizedGrouping<T extends { id: string }>(
}
function splitHighlightedMatch(text: string): Match[][] {
- const parts = text.split(/<nnmark>(.*?)<\/nnmark>/g);
+ const parts = text.split(MATCH_TAG_REGEX);
const allMatches: Match[][] = [];
let matches: Match[] = [];
let totalLength = 0;
diff --git a/packages/core/src/content-types/tiptap.ts b/packages/core/src/content-types/tiptap.ts
index a276973a8..76d1f8bff 100644
--- a/packages/core/src/content-types/tiptap.ts
+++ b/packages/core/src/content-types/tiptap.ts
@@ -286,7 +286,8 @@ export class Tiptap {
!this.data.includes(ATTRIBUTES.src) &&
!this.data.includes(ATTRIBUTES.hash) &&
// check for internal links
- !this.data.includes("nn://")
+ !this.data.includes("nn://") &&
+ !this.data.includes("nn-search-result")
)
return {
data: this.data,
@@ -342,6 +343,8 @@ export class Tiptap {
const html = new HTMLRewriter({
ontag: (name, attr, pos) => {
switch (name) {
+ case "nn-search-result":
+ return null;
case "img": {
const hash = attr[ATTRIBUTES.hash];
diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts
index 3d50dbc12..f924ea390 100644
--- a/packages/core/src/types.ts
+++ b/packages/core/src/types.ts
@@ -496,6 +496,7 @@ export type Match = {
export interface HighlightedResult {
type: "searchResult";
id: string;
+ rawContent?: string;
title: Match[];
content: Match[][];
rank: number;
diff --git a/packages/core/src/utils/html-rewriter.ts b/packages/core/src/utils/html-rewriter.ts
index ab09ff53d..d65c42077 100644
--- a/packages/core/src/utils/html-rewriter.ts
+++ b/packages/core/src/utils/html-rewriter.ts
@@ -23,7 +23,12 @@ type OnTagHandler = (
name: string,
attr: Record<string, string>,
pos: { start: number; end: number }
-) => false | { name: string; attr: Record<string, string> } | undefined | void;
+) =>
+ | false
+ | { name: string; attr: Record<string, string> }
+ | null
+ | undefined
+ | void;
export class HTMLRewriter {
private transformed = "";
@@ -65,7 +70,8 @@ export class HTMLRewriter {
if (result === false) {
this.ignoreIndex = 0;
return;
- } else if (result) {
+ } else if (result === null) return;
+ else if (result) {
name = result.name;
attr = result.attr;
}