Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: merge adjacent macro em tags in front end search processing (#313)

gbirmanb61a91cb4cd6
diff --git a/js/app/bun.lock b/js/app/bun.lock
index b7035b583c..ee906ba21e 100644
--- a/js/app/bun.lock
+++ b/js/app/bun.lock
@@ -1,6 +1,5 @@
{
"lockfileVersion": 1,
- "configVersion": 0,
"workspaces": {
"": {
"name": "@coparse/monorepo",
diff --git a/js/app/packages/core/util/searchHighlight.ts b/js/app/packages/core/util/searchHighlight.ts
index 00f76d5d6c..7310d7b2e5 100644
--- a/js/app/packages/core/util/searchHighlight.ts
+++ b/js/app/packages/core/util/searchHighlight.ts
@@ -42,3 +42,23 @@ export function extractSearchSnippet(highlightedContent: string): string {
const rawContent = highlightedContent.replace(/<\/?macro_em>/g, '');
return rawContent.replace(/\s+/g, ' ').trim();
}
+
+/**
+ * Merges adjacent macro_em tags in highlighted content.
+ * When multiple macro_em tags appear consecutively (with only whitespace between them),
+ * they are merged into a single macro_em tag.
+ *
+ * @param highlightedContent - Content with <macro_em> tags marking matches
+ * @returns Content with adjacent macro_em tags merged
+ *
+ * @example
+ * mergeAdjacentMacroEmTags("The <macro_em>quick</macro_em> <macro_em>brown</macro_em> fox")
+ * // Returns: "The <macro_em>quick brown</macro_em> fox"
+ *
+ * @example
+ * mergeAdjacentMacroEmTags("<macro_em>Hello</macro_em> <macro_em>world</macro_em>, <macro_em>goodbye</macro_em>")
+ * // Returns: "<macro_em>Hello world</macro_em>, <macro_em>goodbye</macro_em>"
+ */
+export function mergeAdjacentMacroEmTags(highlightedContent: string): string {
+ return highlightedContent.replace(/<\/macro_em>(\s+)<macro_em>/g, '$1');
+}
diff --git a/js/app/packages/macro-entity/src/queries/search.ts b/js/app/packages/macro-entity/src/queries/search.ts
index 86c2716bc9..085f25e6c2 100644
--- a/js/app/packages/macro-entity/src/queries/search.ts
+++ b/js/app/packages/macro-entity/src/queries/search.ts
@@ -4,6 +4,7 @@ import { isErr } from '@core/util/maybeResult';
import {
extractSearchSnippet,
extractSearchTerms,
+ mergeAdjacentMacroEmTags,
} from '@core/util/searchHighlight';
import type { ChannelType } from '@service-comms/generated/models';
import { type PaginatedSearchArgs, searchClient } from '@service-search/client';
@@ -45,6 +46,7 @@ const getLocationHighlights = (
const contents = r.highlight.content ?? [];
return contents.map((content) => {
+ const mergedContent = mergeAdjacentMacroEmTags(content);
let location: SearchLocation | undefined;
switch (fileType) {
case 'md':
@@ -56,9 +58,9 @@ const getLocationHighlights = (
location = {
type: 'pdf' as const,
searchPage,
- searchSnippet: extractSearchSnippet(content),
+ searchSnippet: extractSearchSnippet(mergedContent),
searchRawQuery: searchQuery,
- highlightTerms: extractSearchTerms(content),
+ highlightTerms: extractSearchTerms(mergedContent),
};
} catch (_e) {
console.error('Cannot parse pdf serach info', r);
@@ -68,14 +70,18 @@ const getLocationHighlights = (
}
return {
- content,
+ content: mergedContent,
location,
};
});
});
+ const nameHighlight = innerResults.at(0)?.highlight.name ?? null;
+
return {
- nameHighlight: innerResults.at(0)?.highlight.name ?? null,
+ nameHighlight: nameHighlight
+ ? mergeAdjacentMacroEmTags(nameHighlight)
+ : null,
contentHighlights: contentHighlights.length > 0 ? contentHighlights : null,
source: 'service' as const,
};
@@ -86,13 +92,17 @@ const getHighlights = (innerResults: InnerSearchResult[]) => {
const contents = r.highlight.content ?? [];
return contents.map((content) => ({
- content,
+ content: mergeAdjacentMacroEmTags(content),
location: undefined,
}));
});
+ const nameHighlight = innerResults.at(0)?.highlight.name ?? null;
+
return {
- nameHighlight: innerResults.at(0)?.highlight.name ?? null,
+ nameHighlight: nameHighlight
+ ? mergeAdjacentMacroEmTags(nameHighlight)
+ : null,
contentHighlights: contentHighlights.length > 0 ? contentHighlights : null,
source: 'service' as const,
};