From a900c869d3a9fb73d3d1e0263b677c3306b65e74 Mon Sep 17 00:00:00 2001 From: Anwesh Gangula Date: Fri, 20 Feb 2026 16:20:08 +0530 Subject: [PATCH 1/4] use details-summary for results --- dist/obsidian-omnisearch-google.user.js | 32 +++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/dist/obsidian-omnisearch-google.user.js b/dist/obsidian-omnisearch-google.user.js index d888638..00dc2dd 100644 --- a/dist/obsidian-omnisearch-google.user.js +++ b/dist/obsidian-omnisearch-google.user.js @@ -58,7 +58,7 @@ isInit(); }); // Obsidian logo - const logo = ` + const logo = `
-
+
`); - resultsDiv.append(element); + $("#OmnisearchResultsList").append(element); } }, onerror: function (res) { @@ -155,24 +155,32 @@ function injectTitle() { const id = "OmnisearchObsidianConfig"; if (!$("#" + id)[0]) { - const btn = $(``); - $(`#${resultsDivId}`).append(btn); - $(document).on("click", "#" + id, function () { + `); + $(`#OmnisearchHeader`).append(btn); // Target the header span + $(document).on("click", "#" + id, function (e) { + e.preventDefault(); // Prevent collapse when clicking settings gmc.open(); }); } } function injectResultsContainer() { - const resultsDiv = $(`
`); + const resultsDiv = $(` +
+ + + +
+
+ `); $(sidebarSelector).prepend(resultsDiv); } function injectLoadingLabel() { if (!$("#" + loadingSpanId)[0]) { const label = $(`Loading...`); - $(`#${resultsDivId}`).append(label); + $(`#OmnisearchResultsList`).append(label); } } function removeLoadingLabel(foundResults = true) { From aa59b429aa6cd95443a3a419ee8db7a96846fdce Mon Sep 17 00:00:00 2001 From: Anwesh Gangula Date: Sat, 21 Feb 2026 12:37:23 +0530 Subject: [PATCH 2/4] `` found matches in search result --- dist/obsidian-omnisearch-google.user.js | 112 ++++++++++++++---------- 1 file changed, 68 insertions(+), 44 deletions(-) diff --git a/dist/obsidian-omnisearch-google.user.js b/dist/obsidian-omnisearch-google.user.js index 00dc2dd..ef921e1 100644 --- a/dist/obsidian-omnisearch-google.user.js +++ b/dist/obsidian-omnisearch-google.user.js @@ -65,14 +65,38 @@ `; + + + /** + * Highlights keywords within a text string using the tag. + * Follows SOLID by separating the formatting logic. + */ + function highlightText(text, words) { + if (!words || words.length === 0) return text; + + // Sort words by length descending to prevent partial replacements (e.g., 'plugin' before 'plugins') + const sortedWords = [...new Set(words)].sort((a, b) => b.length - a.length); + + let highlighted = text; + for (const word of sortedWords) { + // Escape special regex characters in the word + const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + // Use word boundaries \b to ensure we don't mark inside HTML tags or partial matches incorrectly + const regex = new RegExp(`\\b(${escapedWord})\\b`, 'gi'); + highlighted = highlighted.replace(regex, '$1'); + } + return highlighted; + } + function omnisearch() { const port = gmc.get("port"); const nbResults = gmc.get("nbResults"); // Extract the ?q= part of the URL with URLSearchParams const params = new URLSearchParams(window.location.search); const query = params.get("q"); - if (!query) + if (!query){ return; + } injectLoadingLabel(); GM.xmlHttpRequest({ method: "GET", @@ -85,60 +109,60 @@ removeLoadingLabel(data.length > 0); // Keep the x first results data.splice(nbResults); - const resultsDiv = $(`#${resultsDivId}`); + // Delete all existing data-omnisearch-result + $("#OmnisearchResultsList").empty(); $("[data-omnisearch-result]").remove(); // Inject results for (const item of data) { const url = `obsidian://open?vault=${encodeURIComponent(item.vault)}&file=${encodeURIComponent(item.path)}`; + + // Prepare highlighted excerpt + const cleanExcerpt = item.excerpt.replaceAll("
", " ").replaceAll("
", " "); + const highlightedExcerpt = highlightText(cleanExcerpt, item.foundWords); + const element = $(`
-
-
-
-
-
- - -

${item.basename}

-
- `); $("#OmnisearchResultsList").append(element); } }, @@ -147,7 +171,8 @@ const span = $("#" + loadingSpanId)[0]; if (span) { span.innerHTML = `Error: Obsidian is not running or the Omnisearch server is not enabled. -
Open Obsidian.`; +
+ Open Obsidian.`; } }, }); @@ -186,8 +211,7 @@ function removeLoadingLabel(foundResults = true) { if (foundResults) { $("#" + loadingSpanId).remove(); - } - else { + } else { $("#" + loadingSpanId).text("No results found"); } } @@ -204,7 +228,7 @@ console.log("Loaded Omnisearch injector"); // Keep the results on top waitForKeyElements(sidebarSelector, () => { - $(resultsDivId).prependTo(sidebarSelector); + $(`#${resultsDivId}`).prependTo(sidebarSelector); }); }); })(); From b1c22f958a2df6b9b53c0f07d1f2598fce7615c3 Mon Sep 17 00:00:00 2001 From: Anwesh Gangula Date: Sat, 21 Feb 2026 13:13:38 +0530 Subject: [PATCH 3/4] retain some logic from original code --- dist/obsidian-omnisearch-google.user.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/dist/obsidian-omnisearch-google.user.js b/dist/obsidian-omnisearch-google.user.js index ef921e1..d39559f 100644 --- a/dist/obsidian-omnisearch-google.user.js +++ b/dist/obsidian-omnisearch-google.user.js @@ -109,9 +109,9 @@ removeLoadingLabel(data.length > 0); // Keep the x first results data.splice(nbResults); - + const resultsDiv = $(`#${resultsDivId}`); // Delete all existing data-omnisearch-result - $("#OmnisearchResultsList").empty(); + resultsDiv.empty(); $("[data-omnisearch-result]").remove(); // Inject results for (const item of data) { @@ -163,7 +163,7 @@
`); - $("#OmnisearchResultsList").append(element); + resultsDiv.append(element); } }, onerror: function (res) { @@ -181,10 +181,10 @@ const id = "OmnisearchObsidianConfig"; if (!$("#" + id)[0]) { const btn = $(`${logo} -  Omnisearch results + Omnisearch results () `); - $(`#OmnisearchHeader`).append(btn); // Target the header span + $(`#OmnisearchHeader`).append(btn); $(document).on("click", "#" + id, function (e) { e.preventDefault(); // Prevent collapse when clicking settings gmc.open(); @@ -192,20 +192,20 @@ } } function injectResultsContainer() { - const resultsDiv = $(` -
+ const resultsDetailsSummary = $(` +
-
+
`); - $(sidebarSelector).prepend(resultsDiv); + $(sidebarSelector).prepend(resultsDetailsSummary); } function injectLoadingLabel() { if (!$("#" + loadingSpanId)[0]) { const label = $(`Loading...`); - $(`#OmnisearchResultsList`).append(label); + $(`#${resultsDivId}`).append(label); } } function removeLoadingLabel(foundResults = true) { @@ -228,7 +228,7 @@ console.log("Loaded Omnisearch injector"); // Keep the results on top waitForKeyElements(sidebarSelector, () => { - $(`#${resultsDivId}`).prependTo(sidebarSelector); + $(resultsDivId).prependTo(sidebarSelector); }); }); })(); From 1cc206bff3b63b0a7b26bc44bb898d6c6a531e7a Mon Sep 17 00:00:00 2001 From: Anwesh Gangula Date: Sat, 21 Feb 2026 13:15:52 +0530 Subject: [PATCH 4/4] clear distinction for closing template string --- dist/obsidian-omnisearch-google.user.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dist/obsidian-omnisearch-google.user.js b/dist/obsidian-omnisearch-google.user.js index d39559f..7078d98 100644 --- a/dist/obsidian-omnisearch-google.user.js +++ b/dist/obsidian-omnisearch-google.user.js @@ -162,7 +162,8 @@
-
`); +
+ `); resultsDiv.append(element); } },