Files
Meghdad 9d24ef1251
Tests / Public checks (push) Canceled after 0s
Tests / Nova integration (push) Canceled after 0s
fix: prevent duplicate Nova base path in menu search navigation
2026-09-14 00:00:35 +03:30

72 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const ARABIC_CHARACTER_EQUIVALENTS = {
ك: 'ک',
ي: 'ی',
ى: 'ی',
ة: 'ه',
ۀ: 'ه',
}
export function normalizeForSearch(value) {
return String(value ?? '')
.normalize('NFKD')
.replace(/[\u064B-\u065F\u0670\u06D6-\u06ED]/g, '')
.replace(/\u0640/g, '')
.replace(/[\u200C\u200D]/g, ' ')
.replace(/[كيىةۀ]/g, character => ARABIC_CHARACTER_EQUIVALENTS[character])
.toLocaleLowerCase()
.replace(/\s+/g, ' ')
.trim()
}
export function collectSearchableItems(menuItems, ancestors = []) {
return (Array.isArray(menuItems) ? menuItems : []).flatMap(item => {
const name = String(item?.name ?? '').trim()
const trail = name === '' ? ancestors : [...ancestors, name]
const method = String(item?.method ?? 'GET').toUpperCase()
const current = item?.path && method === 'GET'
? [{
name,
path: item.path,
external: item.external === true,
target: item.target ?? null,
trail,
searchableText: normalizeForSearch(trail.join(' ')),
}]
: []
return [
...current,
...collectSearchableItems(item?.items, trail),
]
})
}
export function filterSearchableItems(items, query, maximumResults = 10) {
const terms = normalizeForSearch(query).split(' ').filter(Boolean)
if (terms.length === 0) {
return []
}
const seen = new Set()
return items
.filter(item => terms.every(term => item.searchableText.includes(term)))
.filter(item => {
const key = `${item.path}\u0000${item.name}`
if (seen.has(key)) {
return false
}
seen.add(key)
return true
})
.slice(0, Math.max(1, Number(maximumResults) || 10))
}
export function internalNavigationTarget(path) {
return { url: path, remote: false }
}