search WIP
This commit is contained in:
@@ -14,8 +14,7 @@ export default class NotesAPI {
|
||||
this._sodiumReady = false
|
||||
|
||||
this.index = new Index({
|
||||
tokenize: 'tolerant',
|
||||
resolution: 9,
|
||||
tokenize: 'forward',
|
||||
})
|
||||
}
|
||||
|
||||
@@ -118,13 +117,28 @@ export default class NotesAPI {
|
||||
const note = this._decrypt(encryptedNote.data || encryptedNote)
|
||||
|
||||
this.notesCache.set(note.id, note)
|
||||
this.index.add(note.id, note.title + '\n' + note.content)
|
||||
const searchText =
|
||||
note.plainText || this._extractPlainText(note.content)
|
||||
this.index.add(note.id, note.title + '\n' + searchText)
|
||||
} catch (error) {
|
||||
console.error('Failed to decrypt note:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_extractPlainText(content) {
|
||||
if (!content) return ''
|
||||
if (typeof content === 'string') return content
|
||||
|
||||
const extractText = (node) => {
|
||||
if (typeof node === 'string') return node
|
||||
if (!node || !node.content) return ''
|
||||
return node.content.map(extractText).join(' ')
|
||||
}
|
||||
|
||||
return extractText(content)
|
||||
}
|
||||
|
||||
/* -----------------------
|
||||
Public API
|
||||
------------------------*/
|
||||
@@ -150,7 +164,7 @@ export default class NotesAPI {
|
||||
return this.notesCache.get(id) ?? null
|
||||
}
|
||||
|
||||
async createNote(metadata = {}, content = '') {
|
||||
async createNote(metadata = {}, content = '', plainText = '') {
|
||||
const id = crypto.randomUUID()
|
||||
const now = new Date().toISOString()
|
||||
|
||||
@@ -161,6 +175,7 @@ export default class NotesAPI {
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
content,
|
||||
plainText,
|
||||
}
|
||||
|
||||
const encryptedNote = {
|
||||
@@ -169,7 +184,7 @@ export default class NotesAPI {
|
||||
}
|
||||
|
||||
this.notesCache.set(id, note)
|
||||
this.index.add(id, note.title + '\n' + content)
|
||||
this.index.add(id, note.title + '\n' + plainText)
|
||||
|
||||
await this.adapter.create(encryptedNote)
|
||||
|
||||
@@ -183,11 +198,12 @@ export default class NotesAPI {
|
||||
this.index.remove(id)
|
||||
}
|
||||
|
||||
async updateNote(id, content) {
|
||||
async updateNote(id, content, plainText = '') {
|
||||
const note = this.notesCache.get(id)
|
||||
if (!note) throw new Error('Note not found')
|
||||
|
||||
note.content = content
|
||||
note.plainText = plainText
|
||||
note.updatedAt = new Date().toISOString()
|
||||
|
||||
const encryptedNote = {
|
||||
@@ -195,7 +211,7 @@ export default class NotesAPI {
|
||||
data: this._encrypt(note),
|
||||
}
|
||||
|
||||
this.index.update(id, note.title + '\n' + content)
|
||||
this.index.update(id, note.title + '\n' + plainText)
|
||||
|
||||
await this.adapter.update(encryptedNote)
|
||||
|
||||
@@ -228,7 +244,12 @@ export default class NotesAPI {
|
||||
data: this._encrypt(note),
|
||||
}
|
||||
|
||||
this.index.update(id, note.title + '\n' + note.content)
|
||||
this.index.update(
|
||||
id,
|
||||
note.title +
|
||||
'\n' +
|
||||
(note.plainText || this._extractPlainText(note.content)),
|
||||
)
|
||||
|
||||
await this.adapter.update(encryptedNote)
|
||||
|
||||
@@ -236,7 +257,10 @@ export default class NotesAPI {
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const ids = this.index.search(query)
|
||||
const ids = this.index.search(query, {
|
||||
limit: 50,
|
||||
suggest: true,
|
||||
})
|
||||
return ids.map((id) => this.notesCache.get(id))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user