1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00
Files
Boostnote/browser/lib/search.js
bimlas aae584106a Look for tags in context too
The previous commit broke this behaviour.

Looking for a tag means the union of **tags** AND **tag in content**, so
it has to search in the in currently found notes separetely, thus it has
to clone the list first (`.slice(0)`).
2018-03-20 17:11:08 +01:00

45 lines
1.2 KiB
JavaScript

import _ from 'lodash'
export default function searchFromNotes (notes, search) {
if (search.trim().length === 0) return []
const searchBlocks = search.split(' ').filter(block => { return block !== '' })
let foundNotes = notes
searchBlocks.forEach((block) => {
if (block.match(/^#.+/)) {
foundNotes = findByTag(foundNotes.slice(0), block).concat(findByWord(foundNotes.slice(0), block))
} else {
foundNotes = findByWord(foundNotes, block)
}
})
return foundNotes
}
function findByTag (notes, block) {
const tag = block.match(/#(.+)/)[1]
const regExp = new RegExp(_.escapeRegExp(tag), 'i')
return notes.filter((note) => {
if (!_.isArray(note.tags)) return false
return note.tags.some((_tag) => {
return _tag.match(regExp)
})
})
}
function findByWord (notes, block) {
const regExp = new RegExp(_.escapeRegExp(block), 'i')
return notes.filter((note) => {
if (_.isArray(note.tags) && note.tags.some((_tag) => {
return _tag.match(regExp)
})) {
return true
}
if (note.type === 'SNIPPET_NOTE') {
return note.description.match(regExp)
} else if (note.type === 'MARKDOWN_NOTE') {
return note.content.match(regExp)
}
return false
})
}