mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
Merge pull request #1701 from bimlas/fix-filter-by-tag-in-search-entry
Fix tag filtering in search entry
This commit is contained in:
@@ -4,39 +4,28 @@ export default function searchFromNotes (notes, search) {
|
|||||||
if (search.trim().length === 0) return []
|
if (search.trim().length === 0) return []
|
||||||
const searchBlocks = search.split(' ').filter(block => { return block !== '' })
|
const searchBlocks = search.split(' ').filter(block => { return block !== '' })
|
||||||
|
|
||||||
let foundNotes = findByWord(notes, searchBlocks[0])
|
let foundNotes = notes
|
||||||
searchBlocks.forEach((block) => {
|
searchBlocks.forEach((block) => {
|
||||||
foundNotes = findByWord(foundNotes, block)
|
foundNotes = findByWordOrTag(foundNotes, block)
|
||||||
if (block.match(/^#.+/)) {
|
|
||||||
foundNotes = foundNotes.concat(findByTag(notes, block))
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return foundNotes
|
return foundNotes
|
||||||
}
|
}
|
||||||
|
|
||||||
function findByTag (notes, block) {
|
function findByWordOrTag (notes, block) {
|
||||||
const tag = block.match(/#(.+)/)[1]
|
let tag = block
|
||||||
const regExp = new RegExp(_.escapeRegExp(tag), 'i')
|
if (tag.match(/^#.+/)) {
|
||||||
|
tag = tag.match(/#(.+)/)[1]
|
||||||
|
}
|
||||||
|
const tagRegExp = new RegExp(_.escapeRegExp(tag), 'i')
|
||||||
|
const wordRegExp = new RegExp(_.escapeRegExp(block), 'i')
|
||||||
return notes.filter((note) => {
|
return notes.filter((note) => {
|
||||||
if (!_.isArray(note.tags)) return false
|
if (_.isArray(note.tags) && note.tags.some((_tag) => _tag.match(tagRegExp))) {
|
||||||
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
|
return true
|
||||||
}
|
}
|
||||||
if (note.type === 'SNIPPET_NOTE') {
|
if (note.type === 'SNIPPET_NOTE') {
|
||||||
return note.description.match(regExp)
|
return note.description.match(wordRegExp)
|
||||||
} else if (note.type === 'MARKDOWN_NOTE') {
|
} else if (note.type === 'MARKDOWN_NOTE') {
|
||||||
return note.content.match(regExp)
|
return note.content.match(wordRegExp)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -20,18 +20,25 @@ test.before(t => {
|
|||||||
notes = [note1, note2, note3]
|
notes = [note1, note2, note3]
|
||||||
})
|
})
|
||||||
|
|
||||||
test('it can find notes by tags or words', t => {
|
test('it can find notes by tags and words', t => {
|
||||||
// [input, expected content (Array)]
|
// [input, expected content (Array)]
|
||||||
const testCases = [
|
const testWithTags = [
|
||||||
['#tag1', [note1.content, note2.content, note3.content]],
|
['#tag1', [note1.content, note2.content, note3.content]],
|
||||||
['#tag1 #tag2', [note2.content]],
|
['#tag1 #tag2', [note2.content]],
|
||||||
|
['#tag2 #tag1', [note2.content]],
|
||||||
['#tag1 #tag2 #tag3', []],
|
['#tag1 #tag2 #tag3', []],
|
||||||
['content1', [note1.content, note2.content]],
|
['content1', [note1.content, note2.content]],
|
||||||
['content1 content2', [note2.content]],
|
['content1 content2', [note2.content]],
|
||||||
['content1 content2 content3', []],
|
['content1 content2 content3', []],
|
||||||
['#content4', [note3.content]]
|
['#content4', [note3.content]],
|
||||||
|
['#tag2 content1', [note2.content]],
|
||||||
|
['content1 #tag2', [note2.content]]
|
||||||
]
|
]
|
||||||
|
const testWithTagsWithoutHash = testWithTags.map(function (testCase) {
|
||||||
|
return [testCase[0].replace(/#/g, ''), testCase[1]]
|
||||||
|
})
|
||||||
|
|
||||||
|
const testCases = testWithTags.concat(testWithTagsWithoutHash)
|
||||||
testCases.forEach((testCase) => {
|
testCases.forEach((testCase) => {
|
||||||
const [input, expectedContents] = testCase
|
const [input, expectedContents] = testCase
|
||||||
const results = searchFromNotes(notes, input)
|
const results = searchFromNotes(notes, input)
|
||||||
|
|||||||
Reference in New Issue
Block a user