sundevista / local-quotes

šŸ“œ Collect your quotes from all over the vault and embed them in different locations with refresh delays
https://sundevista.github.io/local-quotes/
MIT License
40 stars 1 forks source link

[FR] Quote Author wildcard #33

Open sundevista opened 2 months ago

sundevista commented 2 months ago

Discussed in https://github.com/sundevista/local-quotes/discussions/32

Originally posted by **BigGHS** April 21, 2024 I have some of my quote aithors include a description, for example"Heraclitus of Ephesus, c. 535 BCE ā€“ 475 BCE". The "*" search option will pick up this author but to specifically search for Heraclitus quotes, I have to typi in the complete name. To address this I modified the following 2 functions in main.js to recognise the "!" as a wildcard, e.g. "!heracl" will pick up quotes by Heraclitus, as will "!535 BCE". Here is the revised code: ``` function getValidAuthorsFromAdvancedSearch(quoteVault, search) { const terms = search.split("||").map(term => term.trim()); let validAuthors = []; if (terms.includes("*")) { return quoteVault.map(a => a.author); // Return all authors if "*" is present } terms.forEach(term => { if (term.startsWith("!") && term.length > 1) { const substring = term.slice(1).toLowerCase(); validAuthors = validAuthors.concat( quoteVault.filter(a => a.author.toLowerCase().includes(substring)).map(a => a.author) ); } }); return Array.from(new Set(validAuthors)); // Remove duplicates and return } function searchQuote(quoteVault, search, useWeightedRandom) { const result = { author: null, text: null }; if (search === "*") { result.author = useWeightedRandom ? getWeightedRandomAuthor(quoteVault) : getRandomAuthor(quoteVault); } else { const authorList = getValidAuthorsFromAdvancedSearch(quoteVault, search); if (authorList.length > 0) { result.author = useWeightedRandom ? getWeightedRandomAuthor(quoteVault, authorList) : getRandomArrayItem(authorList); } } result.text = result.author ? getRandomQuoteOfAuthor(quoteVault, result.author) : "No quote found"; return result; } ```