diff --git a/editor/src/editor.js b/editor/src/editor.js index efbd8cd..d93c569 100644 --- a/editor/src/editor.js +++ b/editor/src/editor.js @@ -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 }) diff --git a/editor/src/search.js b/editor/src/search.js index 7238ceb..fe7d57d 100644 --- a/editor/src/search.js +++ b/editor/src/search.js @@ -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({