Jacky Zhao
2022-01-03 2b432d7f0b35987ca6886e7041dce861b7e21abb
layouts/partials/search.html
@@ -177,16 +177,23 @@
                limit: 5,
            }
        ]).then(searchResults => {
        const titleIds = [...searchResults.filter(x => x.field === 'title')?.[0].result] ?? []
        const contentIds = [...searchResults.filter(x => x.field === 'content')?.[0].result] ?? []
        const allIds = [...titleIds, ...contentIds]
        const finalResults = allIds.map(fetch)
            const getByField = field => {
                const results = searchResults.filter(x => x.field === field)
                if (results.length === 0) {
                    return []
                } else {
                    return [...results[0].result]
                }
            }
            const allIds = new Set([...getByField('title'), ...getByField('content')])
            const finalResults = [...allIds].map(fetch)
            // display
            if (finalResults.length === 0) {
                results.innerHTML = `<div class="result-card">
                    <p>No results.</p>
                </div>`
                results.innerHTML = `<button class="result-card">
                    <h3>No results.</h3>
                    <p>Try another search term?</p>
                </button>`
            } else {
                results.innerHTML = finalResults
                    .map(result => resultToHTML({
@@ -246,4 +253,4 @@
        })
    })
</script>
</script>