-import { aboutPage, footerData, navbarData } from '~/data'
+import { aboutPage, footerData, navbarData, socialLinks } from '~/data'
useHead({
title: 'About',
@@ -31,7 +31,7 @@ defineOgImageComponent('About', {
+import Fuse from 'fuse.js'
+
const { data } = await useAsyncData('home', () => queryContent('/blogs').sort({ _id: -1 }).find())
const elementPerPage = ref(5)
@@ -23,26 +25,28 @@ const formattedData = computed(() => {
)
})
+const fuse = computed(() => {
+ return new Fuse(formattedData.value, {
+ keys: ['title', 'description'],
+ threshold: 0.4,
+ includeScore: false,
+ })
+})
+
const searchData = computed(() => {
- return (
- formattedData.value.filter((data) => {
- const lowerTitle = data.title.toLocaleLowerCase()
- if (lowerTitle.search(searchTest.value) !== -1) return true
- else return false
- }) || []
- )
+ if (!searchTest.value.trim()) {
+ return formattedData.value
+ }
+
+ const results = fuse.value.search(searchTest.value)
+ return results.map((result) => result.item)
})
const paginatedData = computed(() => {
- return (
- searchData.value.filter((data, idx) => {
- const startInd = (pageNumber.value - 1) * elementPerPage.value
- const endInd = pageNumber.value * elementPerPage.value - 1
+ const startInd = (pageNumber.value - 1) * elementPerPage.value
+ const endInd = pageNumber.value * elementPerPage.value
- if (idx >= startInd && idx <= endInd) return true
- else return false
- }) || []
- )
+ return searchData.value.slice(startInd, endInd)
})
function onPreviousPageClick() {
@@ -51,8 +55,7 @@ function onPreviousPageClick() {
const totalPage = computed(() => {
const ttlContent = searchData.value.length || 0
- const totalPage = Math.ceil(ttlContent / elementPerPage.value)
- return totalPage
+ return Math.ceil(ttlContent / elementPerPage.value)
})
function onNextPageClick() {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 99b5651..235af85 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -55,6 +55,9 @@ importers:
feed:
specifier: ^4.2.2
version: 4.2.2
+ fuse.js:
+ specifier: ^7.0.0
+ version: 7.0.0
nuxt:
specifier: ^3.14.159
version: 3.14.159(@parcel/watcher@2.5.0)(@types/node@22.9.0)(eslint@9.14.0(jiti@2.4.0))(ioredis@5.4.1)(magicast@0.3.5)(optionator@0.9.4)(rollup@4.24.4)(terser@5.36.0)(typescript@5.6.3)(vite@5.4.10(@types/node@22.9.0)(terser@5.36.0))
@@ -4228,6 +4231,13 @@ packages:
integrity: sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==,
}
+ fuse.js@7.0.0:
+ resolution:
+ {
+ integrity: sha512-14F4hBIxqKvD4Zz/XjDc3y94mNZN6pRv3U13Udo0lNLCWRBUsrMv2xwcF/y/Z5sV6+FQW+/ow68cHpm4sunt8Q==,
+ }
+ engines: { node: '>=10' }
+
gauge@3.0.2:
resolution:
{
@@ -11785,6 +11795,8 @@ snapshots:
function-bind@1.1.2: {}
+ fuse.js@7.0.0: {}
+
gauge@3.0.2:
dependencies:
aproba: 2.0.0