diff --git a/plugin.json b/plugin.json index 5092416..115e12f 100644 --- a/plugin.json +++ b/plugin.json @@ -2,7 +2,7 @@ "name": "siyuan-plugin-hsr-mdzz2048-fork", "author": "TCOTC", "url": "https://github.com/TCOTC/siyuan-plugin-hsr-mdzz2048-fork", - "version": "0.0.9", + "version": "0.0.10", "minAppVersion": "2.9.8", "backends": ["windows", "linux", "darwin"], "frontends": ["desktop", "browser-desktop"], diff --git a/src/Search.vue b/src/Search.vue index e98b25d..63cd7b3 100644 --- a/src/Search.vue +++ b/src/Search.vue @@ -93,6 +93,7 @@ function highlightHitResult(value: string) { const str = value.trim().toLowerCase() if (!str) return +/* // 查找所有文本节点是否包含搜索词 const ranges = allTextNodes .map((el) => { @@ -116,7 +117,30 @@ function highlightHitResult(value: string) { return range }) }) - +*/ +// 替换为: + // 查找所有文本节点是否包含搜索词,并创建对应的 Range 对象 + let ranges = []; + allTextNodes.forEach((node) => { + const textContent = node.textContent.toLowerCase(); + let startIndex = 0; + while ((startIndex = textContent.indexOf(str, startIndex)) !== -1) { + const range = document.createRange(); + try { + range.setStart(node, startIndex); + range.setEnd(node, startIndex + str.length); + ranges.push(range); + } catch (error) { + console.error("Error setting range in node:", node, error); + } + startIndex += str.length; + } + }); + + + + + // 创建高亮对象 const searchResultsHighlight = new Highlight(...ranges.flat()) resultCount.value = ranges.flat().length