implement search
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { useTitle } from '@vueuse/core'
|
||||
import { mdToHtml } from '@/utils/markdown'
|
||||
|
||||
export const notes = ref<Note[]>([])
|
||||
const baseNotes = ref<BaseNote[]>([])
|
||||
export const notes = computed<Note[]>(() => {
|
||||
// extract links and add word count
|
||||
return baseNotes.value as Note[]
|
||||
})
|
||||
|
||||
export const activeNote = ref<Note>()
|
||||
watch(activeNote, () => {
|
||||
@@ -19,12 +24,32 @@ watch(
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
export const setDefaultNotes = (defaultNotes: Note[]) => {
|
||||
notes.value = defaultNotes.map(
|
||||
(note): Note => ({
|
||||
export const setDefaultNotes = (defaultNotes: BaseNote[]) => {
|
||||
baseNotes.value = defaultNotes.map(
|
||||
(note): BaseNote => ({
|
||||
...note,
|
||||
created: new Date().getTime(),
|
||||
modified: new Date().getTime(),
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export const getNoteById = (noteId: string) => {
|
||||
return notes.value.find((note) => note.id === noteId)
|
||||
}
|
||||
|
||||
export const findNotes = (query: string): Note[] => {
|
||||
const removeMdFromText = (mdText: string): string => {
|
||||
const div = document.createElement('div')
|
||||
div.innerHTML = mdToHtml(mdText, 'c@')
|
||||
const textWithoutMd = div.textContent || div.innerText || ''
|
||||
return textWithoutMd
|
||||
}
|
||||
return notes.value.filter((note) => {
|
||||
const matchTitle = note.title.toLowerCase().includes(query.toLowerCase())
|
||||
const matchContent = removeMdFromText(note.content)
|
||||
.toLowerCase()
|
||||
.includes(query.toLowerCase())
|
||||
return matchTitle || matchContent
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user