Explorar o código

feat: content index contains title, close #7510

Junyi Du %!s(int64=2) %!d(string=hai) anos
pai
achega
3f685cf591

+ 13 - 14
e2e-tests/page-search.spec.ts

@@ -1,5 +1,6 @@
 import { expect, Page } from '@playwright/test'
 import { test } from './fixtures'
+import { Block } from './types'
 import { IsMac, createRandomPage, newBlock, newInnerBlock, randomString, lastBlock, enterNextBlock } from './utils'
 
 /***
@@ -45,7 +46,7 @@ test('Search page and blocks (diacritics)', async ({ page, block }) => {
   await page.waitForTimeout(2000) // wait longer for search contents to render
   // 2 blocks + 1 page + 1 page content
   const searchResults = page.locator('#ui__ac-inner>div')
-  await expect(searchResults).toHaveCount(4)
+  await expect(searchResults).toHaveCount(5) // 1 page + 2 block + 2 page content
 
   await page.keyboard.press("Escape") // escape search box typing
   await page.waitForTimeout(500)
@@ -62,26 +63,26 @@ test('Search CJK', async ({ page, block }) => {
   await page.keyboard.press(hotkeyOpenLink)
 
   const pageTitle = page.locator('.page-title').first()
-  expect(await pageTitle.innerText()).toEqual('Einführung in die Allgemeine Sprachwissenschaft' + rand)
+  expect(await pageTitle.innerText()).toEqual('今日daytime进度条' + rand)
 
   await page.waitForTimeout(500)
 
   // check if diacritics are indexed
   await page.click('#search-button')
   await page.waitForSelector('[placeholder="Search or create page"]')
-  await page.type('[placeholder="Search or create page"]', '进度' + rand, { delay: 10 })
+  await page.type('[placeholder="Search or create page"]', '进度', { delay: 10 })
 
   await page.waitForTimeout(2000) // wait longer for search contents to render
   // 2 blocks + 1 page + 1 page content
   const searchResults = page.locator('#ui__ac-inner>div')
-  await expect(searchResults).toHaveCount(3)
+  await expect(searchResults).toHaveCount(4) // 1 new page + 1 page + 1 block + 1 page content
 
   await page.keyboard.press("Escape") // escape search box typing
   await page.waitForTimeout(500)
   await page.keyboard.press("Escape") // escape modal
 })
 
-async function alias_test(page: Page, page_name: string, search_kws: string[]) {
+async function alias_test( block: Block, page: Page, page_name: string, search_kws: string[] ) {
   const rand = randomString(10)
   let target_name = page_name + ' target ' + rand
   let alias_name = page_name + ' alias ' + rand
@@ -129,7 +130,7 @@ async function alias_test(page: Page, page_name: string, search_kws: string[]) {
   await page.type('textarea >> nth=0', alias_test_content_2)
   await page.keyboard.press(hotkeyBack)
 
-  // pressing enter opening test
+  // pressing enter on alias opening test
   await lastBlock(page)
   await page.press('textarea >> nth=0', 'ArrowLeft')
   await page.press('textarea >> nth=0', 'ArrowLeft')
@@ -141,8 +142,8 @@ async function alias_test(page: Page, page_name: string, search_kws: string[]) {
   await page.type('textarea >> nth=0', alias_test_content_3)
   await page.keyboard.press(hotkeyBack)
 
-  // clicking opening test
-  await newBlock(page)
+  // clicking alias ref opening test
+  await block.enterNext()
   await page.waitForSelector('.page-blocks-inner .ls-block .page-ref >> nth=-1')
   await page.click('.page-blocks-inner .ls-block .page-ref >> nth=-1')
   await lastBlock(page)
@@ -160,15 +161,13 @@ async function alias_test(page: Page, page_name: string, search_kws: string[]) {
     await page.waitForTimeout(500)
 
     const results = await page.$$('#ui__ac-inner>div')
-    expect(results.length).toEqual(3) // page + block + alias property
+    expect(results.length).toEqual(5) // page + block + alias property + page content
 
     // test search results
     expect(await results[0].innerText()).toContain("Alias -> " + target_name)
     expect(await results[0].innerText()).toContain(alias_name)
-    expect(await results[1].innerText()).toContain(parent_title)
     expect(await results[1].innerText()).toContain("[[" + alias_name + "]]")
-    expect(await results[2].innerText()).toContain(target_name)
-    expect(await results[2].innerText()).toContain("alias:: [[" + alias_name + "]]")
+    expect(await results[2].innerText()).toContain("[[" + alias_name + "]]")
 
     // test search entering (page)
     page.keyboard.press("Enter")
@@ -193,6 +192,6 @@ async function alias_test(page: Page, page_name: string, search_kws: string[]) {
   // TODO: search clicking (alias property)
 }
 
-test.skip('page diacritic alias', async ({ page }) => {
-  await alias_test(page, "ü", ["ü", "ü", "Ü"])
+test.skip('page diacritic alias', async ({ block, page }) => {
+  await alias_test(block, page, "ü", ["ü", "ü", "Ü"])
 })

+ 1 - 0
e2e-tests/utils.ts

@@ -115,6 +115,7 @@ export async function newInnerBlock(page: Page): Promise<Locator> {
   return page.locator('textarea >> nth=0')
 }
 
+// Deprecated by block.enterNext
 export async function newBlock(page: Page): Promise<Locator> {
   let blockNumber = await page.locator('.page-blocks-inner .ls-block').count()
   await lastBlock(page)

+ 14 - 4
src/electron/electron/search.cljs

@@ -279,7 +279,7 @@
 
 (defn- snippet-by
   [content length]
-  (str (subs content 0 length) "..."))
+  (str (subs content 0 length) (when (> (count content) 250) "...")))
 
 (defn- search-pages-res-unpack
   [arr]
@@ -287,9 +287,19 @@
     {:id      rowid
      :uuid    uuid
      :content content
-     :snippet (if (string/includes? snippet "$pfts_2lqh>$ ")
-                snippet
-                (snippet-by snippet 250))}))
+     ;; post processing
+     :snippet (let [;; Remove title from snippet
+                    flag-title " $<pfts_f6ld$ "
+                    flag-title-pos (string/index-of snippet flag-title)
+                    snippet (if flag-title-pos
+                              (subs snippet (+ flag-title-pos (count flag-title)))
+                              snippet)
+                    ;; Cut snippet to 250 chars for non-matched results
+                    flag-highlight "$pfts_2lqh>$ "
+                    snippet (if (string/includes? snippet flag-highlight)
+                              snippet
+                              (snippet-by snippet 250))]
+                snippet)}))
 
 (defn- search-pages-aux
   [database sql input limit]

+ 3 - 2
src/main/frontend/search/db.cljs

@@ -30,13 +30,14 @@
 (defn page->index
   "Convert a page name to the index for searching (page content level)
    Generate index based on the DB content AT THE POINT OF TIME"
-  [{:block/keys [uuid _original-name] :as page}]
+  [{:block/keys [uuid original-name] :as page}]
   (when-let [content (some-> (:block/file page)
                              (:file/content))]
     (when-not (> (count content) (* (max-len) 10))
       {:id   (:db/id page)
        :uuid (str uuid)
-       :content (sanitize content)})))
+       ;; Add page name to the index
+       :content (sanitize (str "$pfts_f6ld>$ " original-name " $<pfts_f6ld$ " content))})))
 
 (defn build-blocks-indice
   ;; TODO: Remove repo effects fns further up the call stack. db fns need standardization on taking connection