Problem: you can not search for lines without grouping
All checks were successful
continuous-integration/drone/push Build is passing

Solution: add search operator "query@" to find lines without grouping on
titles.
This commit is contained in:
Peter Stuifzand 2022-01-07 21:39:01 +01:00
parent cba1d002d9
commit fa9f34e42f
2 changed files with 41 additions and 19 deletions

View File

@ -176,6 +176,17 @@ function formatLineResult(hits, key) {
]
}
function formatLineWithoutTitleResult(hit, key) {
if (hit.line.match(/query[!@]?:/)) return []
return [{
text: hit.line,
indented: 0,
fold: 'open',
hidden: false,
fleeting: true
}]
}
function formatTitleResult(hit) {
return [
{
@ -649,23 +660,31 @@ function Editor(holder, input) {
let query = $input.val()
match(query, /{{query(!?):\s*([^}]+)}}/)
match(query, /{{query([!@]?):\s*([^}]+)}}/)
.then(res => {
if (res[1] === '!') {
return search.startQuery(res[2])
if (res[1] === '@') {
return search.startQuery(res[2], {internal: false})
.then(hits => _.flatMap(hits, formatLineWithoutTitleResult))
.then(results => editor.replaceChildren(id, results))
.catch(e => console.log('search query', e))
.finally(() => editor.render())
} else if (res[1] === '!') {
return search.startQuery(res[2], {internal: false})
.then(hits => _.uniqBy(_.flatMap(hits, formatTitleResult), _.property('text')))
.then(results => editor.replaceChildren(id, results))
.catch(e => console.log('search query', e))
.finally(() => editor.render())
} else {
return search.startQuery(res[2])
return search.startQuery(res[2], {internal: false})
.then(hits => _.groupBy(hits, (it) => it.title))
.then(hits => _.flatMap(hits, formatLineResult))
.then(results => editor.replaceChildren(id, results))
.catch(e => console.log('search query', e))
.finally(() => editor.render())
}
}).catch(e => {})
}).catch(e => {
console.log(e)
})
});
return editor
})

View File

@ -17,26 +17,29 @@ function search(element) {
}
}
function startQuery(query) {
function startQuery(query, opt) {
opt ||= {internal:true}
return fetch('/search/?' + qs.encode({q: query}))
.then(res => res.json())
.then(data => {
let actualResult = [];
actualResult.push({
ref: query,
title: 'Create new page "' + query + '"',
line: 'New page',
text: 'New page',
})
let parseResult = chrono.nl.casual.parse(query)
if (parseResult.length) {
let m = moment(parseResult[0].start.date())
if (opt.internal) {
actualResult.push({
ref: m.format('LL').replace(/\s+/g, '_'),
title: m.format('LL'),
line: 'Suggested page',
text: 'Suggested page'
ref: query,
title: 'Create new page "' + query + '"',
line: 'New page',
text: 'New page',
})
let parseResult = chrono.nl.casual.parse(query)
if (parseResult.length) {
let m = moment(parseResult[0].start.date())
actualResult.push({
ref: m.format('LL').replace(/\s+/g, '_'),
title: m.format('LL'),
line: 'Suggested page',
text: 'Suggested page'
})
}
}
$.each(data.hits, (key, value) => {
actualResult.push({