You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB
JavaScript

; (async function() {
const encoder = (str) => str.toLowerCase().split(/([^a-z]|[^\x00-\x7F])+/)
const contentIndex = new FlexSearch.Document({
cache: true,
3 years ago
charset: "latin:extra",
optimize: true,
3 years ago
index: [
{
3 years ago
field: "content",
tokenize: "reverse",
3 years ago
encode: encoder,
},
{
3 years ago
field: "title",
tokenize: "forward",
3 years ago
encode: encoder,
},
],
})
const { content } = await fetchData
for (const [key, value] of Object.entries(content)) {
contentIndex.add({
id: key,
title: value.title,
content: removeMarkdown(value.content),
})
}
3 years ago
const formatForDisplay = (id) => ({
id,
url: id,
title: content[id].title,
3 years ago
content: content[id].content,
})
registerHandlers((e) => {
term = e.target.value
const searchResults = contentIndex.search(term, [
{
3 years ago
field: "content",
limit: 10,
},
{
3 years ago
field: "title",
limit: 5,
3 years ago
},
])
3 years ago
const getByField = (field) => {
const results = searchResults.filter((x) => x.field === field)
if (results.length === 0) {
return []
} else {
return [...results[0].result]
}
}
3 years ago
const allIds = new Set([...getByField("title"), ...getByField("content")])
const finalResults = [...allIds].map(formatForDisplay)
displayResults(finalResults)
})
})()