Browse Source

fix: 规范化文本比较,处理特殊空格字符

何文松 4 weeks ago
parent
commit
2e65ec8199
1 changed files with 11 additions and 2 deletions
  1. 11 2
      frontend/vue-demo/src/views/Editor.vue

+ 11 - 2
frontend/vue-demo/src/views/Editor.vue

@@ -514,9 +514,18 @@ function getTocBullet(level) {
   return bullets[Math.min(level - 1, bullets.length - 1)]
 }
 
+// 规范化文本用于比较
+function normalizeForCompare(text) {
+  if (!text) return ''
+  return text
+    .replace(/\s+/g, ' ')  // 多个空格合并
+    .replace(/[\u00A0\u202F\u2009]/g, ' ')  // 特殊空格转普通空格
+    .trim()
+}
+
 // 滚动到指定章节(只匹配 h1-h6 标题元素)
 function scrollToHeading(item) {
-  const titleText = (item.title || item.text || '').trim()
+  const titleText = normalizeForCompare(item.title || item.text)
   if (!titleText) return
   
   // 在文档内容区域中查找标题元素
@@ -527,7 +536,7 @@ function scrollToHeading(item) {
   const headings = editorContent.querySelectorAll('h1, h2, h3, h4, h5, h6')
   
   for (const heading of headings) {
-    const headingText = heading.textContent?.trim()
+    const headingText = normalizeForCompare(heading.textContent)
     if (headingText === titleText) {
       heading.scrollIntoView({ behavior: 'smooth', block: 'start' })
       highlightElement(heading)