Skip to content

Commit b29e9d7

Browse files
add: bool to check if query too long and prevent other warnings
1 parent f610e50 commit b29e9d7

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

‎assets/js/search_functions.js‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var RMAX = 100; // Max number of results to display
44

55
// Dylan's sutta finder button
66
const suttaFinder = '<a href="https://name.readingfaithfully.org/" class="btn" target="_blank">Sutta Finder</a>'
7+
const suttaCentralCommunity = '<a href="https://discourse.suttacentral.net/search?context=topic&context_id=26591&q=my%20search%20query&skip_context=true" class="btn" target="_blank">Sutta Central Community</a>'
78

89
function getPositions(result, field) {
910
var positions = [];
@@ -244,18 +245,19 @@ function handleSearchMessage(data, searchFn) {
244245
}
245246
}
246247

247-
// Now back to my other edits
248+
// Now back to Dylan's other edits
248249

249-
// Dylan's edit - remove quotes? - line 178 - 187
250+
// Dylan's edit - remove quotes?
250251
// /["']/g is a JavaScript regular expression literal - g = global flag (match all occurrences, not just the first)
251252
var preWordsParse = data.q.replace(/["']/g, "");
252253

253254
// Normalize leading nikaya index, e.g. "MN6" -> "MN 6"
254255
// I wrote out some for loops and char checks but AI suggested regex when I had him check my code.
255256
// Regex seems straight forward you just need to research it as I still need to. I would have preferred manually doing it so I can practice my ability
256-
preWordsParse = preWordsParse.replace(/^(\s*)(MN|SN|SNP|AN|DN)\s*(\d+)/i, function(_, leadingSpace, nikaya, number) {
257-
return leadingSpace + nikaya.toUpperCase() + " " + number;
258-
});
257+
preWordsParse = preWordsParse.replace(
258+
/^(\s*)(DN|MN|SN|AN|SNP|DHP|ITI|THAG|THIG|UD)\s*(\d+(?:\.\d+)?)/i,
259+
(_, lead, coll, num) => `${lead}${coll.toUpperCase()} ${num}`
260+
);
259261
// this is to make a start on querying suttas from database. I'm starting with getting a space before sutta.
260262
// then I'll have to figure something else out until I understand the lunrjs better and how the database querying is actually handled
261263
preWordsParse = preWordsParse.replace(/\b([\p{L}]+?)sutta\b/giu, "$1 sutta");
@@ -267,8 +269,20 @@ function handleSearchMessage(data, searchFn) {
267269
}
268270

269271
//--------------------------------------------------------------------------------------------------
272+
// Dylan's edit to check if query is a potential question or if query is longer than usual
273+
// there is a bool flag to stop other warnings
274+
const hasQuestion = /\?/.test(preWordsParse);
275+
let stopOtherWarnings = false;
276+
if (preWordsParse.length >= 40){
277+
stopOtherWarnings = true;
278+
279+
} else if(hasQuestion){
280+
stopOtherWarnings = true;
281+
} else {
282+
stopOtherWarnings = false;
283+
}
270284

271-
// Back to the original functionality
285+
// Back to the original functionality - this was before my edits above
272286
var words = preWordsParse.trim().split(" ");
273287
for (var i = 0; i < words.length; i++) {
274288
const s = words[i].trim();
@@ -281,6 +295,7 @@ function handleSearchMessage(data, searchFn) {
281295
var query = words.join(' ').trim();
282296
try {
283297
results = searchFn(query);
298+
284299
if (!results.length){
285300
warning = "<li><strong>No results</strong> found matching all of your terms. Results found matching <em>any</em> term:</li>";
286301
results = searchFn(data.q.trim());
@@ -310,6 +325,9 @@ function handleSearchMessage(data, searchFn) {
310325
});
311326
});
312327
}
328+
if(stopOtherWarnings){
329+
warning = "Are you querying a question? You can check for potential answers in the Sutta Central Community chat as well." + "<li>" + suttaCentralCommunity + "</li>"
330+
}
313331
return {
314332
"warninghtml": warning,
315333
"html": displaySearchResults(results),

0 commit comments

Comments
 (0)