|
| 1 | +const ABSOLUTE_URL_RE = /^(?:[a-z]+:)?\/\//i; |
| 2 | + |
| 3 | +const normalizeBundlePath = (path, baseURL) => { |
| 4 | + let bundlePath = typeof path === 'string' && path.length > 0 ? path : 'pagefind/'; |
| 5 | + if (!bundlePath.endsWith('/')) { |
| 6 | + bundlePath = `${bundlePath}/`; |
| 7 | + } |
| 8 | + if (ABSOLUTE_URL_RE.test(bundlePath)) { |
| 9 | + return bundlePath; |
| 10 | + } |
| 11 | + return new URL(bundlePath, baseURL || document.baseURI).toString(); |
| 12 | +}; |
| 13 | + |
| 14 | +const toObject = (value) => (value && typeof value === 'object' ? value : {}); |
| 15 | + |
| 16 | +const normalizeSortOrder = (value) => ( |
| 17 | + String(value).toLowerCase() === 'asc' |
| 18 | + ? 'asc' |
| 19 | + : 'desc' |
| 20 | +); |
| 21 | + |
| 22 | +const replaceExcerptHighlightTag = (excerpt, highlightTag) => { |
| 23 | + if (!excerpt || !highlightTag || highlightTag === 'mark') { |
| 24 | + return excerpt || ''; |
| 25 | + } |
| 26 | + |
| 27 | + return excerpt |
| 28 | + .replaceAll('<mark>', `<${highlightTag}>`) |
| 29 | + .replaceAll('</mark>', `</${highlightTag}>`); |
| 30 | +}; |
| 31 | + |
| 32 | +export function createPagefindSearch(searchConfig) { |
| 33 | + const pagefindConfig = toObject(searchConfig.pagefind); |
| 34 | + const bundlePath = normalizeBundlePath(pagefindConfig.bundlePath, pagefindConfig.baseURL); |
| 35 | + const rawDebounceTimeout = Number(pagefindConfig.debounceTimeoutMs ?? 300); |
| 36 | + const debounceTimeout = Number.isFinite(rawDebounceTimeout) ? Math.max(0, rawDebounceTimeout) : 300; |
| 37 | + const builtInFiltersEnabled = pagefindConfig.useBuiltInFilters !== false; |
| 38 | + const sortBy = typeof pagefindConfig.sortBy === 'string' ? pagefindConfig.sortBy.trim() : ''; |
| 39 | + const sortOrder = normalizeSortOrder(pagefindConfig.sortOrder); |
| 40 | + const highlightTag = searchConfig.highlightTag ?? 'em'; |
| 41 | + const excerptLength = Number(searchConfig.snippetLength ?? 30); |
| 42 | + |
| 43 | + const state = { |
| 44 | + loading: null, |
| 45 | + initialized: false, |
| 46 | + availableFilters: null, |
| 47 | + }; |
| 48 | + |
| 49 | + const ensurePagefind = async () => { |
| 50 | + if (!state.loading) { |
| 51 | + state.loading = import(`${bundlePath}pagefind.js`) |
| 52 | + .then(async (mod) => { |
| 53 | + if (!state.initialized) { |
| 54 | + const options = {}; |
| 55 | + if (Number.isFinite(excerptLength) && excerptLength >= 0) { |
| 56 | + options.excerptLength = excerptLength; |
| 57 | + } |
| 58 | + if (Object.keys(options).length && typeof mod.options === 'function') { |
| 59 | + await mod.options(options); |
| 60 | + } |
| 61 | + await mod.init(); |
| 62 | + state.initialized = true; |
| 63 | + } |
| 64 | + return mod; |
| 65 | + }) |
| 66 | + .catch((error) => { |
| 67 | + state.loading = null; |
| 68 | + throw error; |
| 69 | + }); |
| 70 | + } |
| 71 | + return state.loading; |
| 72 | + }; |
| 73 | + |
| 74 | + const getAvailableFilters = async () => { |
| 75 | + if (state.availableFilters) return state.availableFilters; |
| 76 | + |
| 77 | + const pagefind = await ensurePagefind(); |
| 78 | + if (typeof pagefind.filters !== 'function') { |
| 79 | + state.availableFilters = {}; |
| 80 | + return state.availableFilters; |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + state.availableFilters = toObject(await pagefind.filters()); |
| 85 | + } catch (error) { |
| 86 | + console.warn('[FixIt] failed to read Pagefind filters:', error); |
| 87 | + state.availableFilters = {}; |
| 88 | + } |
| 89 | + return state.availableFilters; |
| 90 | + }; |
| 91 | + |
| 92 | + return { |
| 93 | + preload() { |
| 94 | + return ensurePagefind(); |
| 95 | + }, |
| 96 | + async search(query, maxResultLength) { |
| 97 | + if (!query || !query.trim()) return []; |
| 98 | + |
| 99 | + const pagefind = await ensurePagefind(); |
| 100 | + const searchOptions = {}; |
| 101 | + |
| 102 | + if (builtInFiltersEnabled) { |
| 103 | + const availableFilters = await getAvailableFilters(); |
| 104 | + const filters = {}; |
| 105 | + if (Object.prototype.hasOwnProperty.call(availableFilters, 'hidden')) { |
| 106 | + filters.hidden = 'false'; |
| 107 | + } |
| 108 | + if (Object.prototype.hasOwnProperty.call(availableFilters, 'encrypted')) { |
| 109 | + filters.encrypted = 'false'; |
| 110 | + } |
| 111 | + if (Object.keys(filters).length) { |
| 112 | + searchOptions.filters = filters; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (sortBy) { |
| 117 | + searchOptions.sort = { [sortBy]: sortOrder }; |
| 118 | + } |
| 119 | + |
| 120 | + const resultLimit = Number.isFinite(maxResultLength) |
| 121 | + ? Math.max(0, Math.floor(maxResultLength)) |
| 122 | + : 10; |
| 123 | + |
| 124 | + const searched = debounceTimeout > 0 && typeof pagefind.debouncedSearch === 'function' |
| 125 | + ? await pagefind.debouncedSearch(query, searchOptions, debounceTimeout) |
| 126 | + : await pagefind.search(query, searchOptions); |
| 127 | + |
| 128 | + if (searched === null) return null; |
| 129 | + |
| 130 | + const records = await Promise.all( |
| 131 | + (searched.results || []).slice(0, resultLimit).map((entry) => entry.data()), |
| 132 | + ); |
| 133 | + |
| 134 | + return records.map((item) => ({ |
| 135 | + uri: item.url || '#', |
| 136 | + title: item.meta?.title || item.url || '', |
| 137 | + date: item.meta?.date || '', |
| 138 | + context: replaceExcerptHighlightTag(item.excerpt || '', highlightTag), |
| 139 | + })); |
| 140 | + }, |
| 141 | + }; |
| 142 | +} |
0 commit comments