|
| 1 | +export function keywordsFor(query) { |
| 2 | + return Array.from( |
| 3 | + new Set( |
| 4 | + query |
| 5 | + .toLowerCase() |
| 6 | + .split(/[^a-z0-9._/-]+/) |
| 7 | + .map((keyword) => keyword.trim()) |
| 8 | + .filter(Boolean) |
| 9 | + ) |
| 10 | + ); |
| 11 | +} |
| 12 | + |
| 13 | +function distinctHits(text, keywords) { |
| 14 | + const lower = text.toLowerCase(); |
| 15 | + return keywords.filter((keyword) => lower.includes(keyword)); |
| 16 | +} |
| 17 | + |
| 18 | +function rankMatches(matches) { |
| 19 | + return matches.sort((a, b) => { |
| 20 | + if (b.score !== a.score) { |
| 21 | + return b.score - a.score; |
| 22 | + } |
| 23 | + return a.path.length - b.path.length; |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function makeSnippet(content, keywords) { |
| 28 | + const lower = content.toLowerCase(); |
| 29 | + const index = keywords.reduce((best, keyword) => { |
| 30 | + const found = lower.indexOf(keyword); |
| 31 | + if (found === -1) { |
| 32 | + return best; |
| 33 | + } |
| 34 | + return best === -1 ? found : Math.min(best, found); |
| 35 | + }, -1); |
| 36 | + |
| 37 | + if (index === -1) { |
| 38 | + return content.slice(0, 240).replace(/\s+/g, " ").trim(); |
| 39 | + } |
| 40 | + |
| 41 | + const start = Math.max(0, index - 120); |
| 42 | + const end = Math.min(content.length, index + 240); |
| 43 | + const prefix = start > 0 ? "..." : ""; |
| 44 | + const suffix = end < content.length ? "..." : ""; |
| 45 | + return `${prefix}${content.slice(start, end).replace(/\s+/g, " ").trim()}${suffix}`; |
| 46 | +} |
| 47 | + |
| 48 | +function toResult(doc, keywords, hits, match) { |
| 49 | + return { |
| 50 | + title: doc.title, |
| 51 | + path: doc.path, |
| 52 | + url: doc.url, |
| 53 | + markdown_url: doc.markdown_url, |
| 54 | + text_url: doc.text_url, |
| 55 | + match, |
| 56 | + score: hits.length, |
| 57 | + matched_keywords: hits, |
| 58 | + snippet: makeSnippet(doc.content, hits.length ? hits : keywords), |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +export function searchDocs(corpus, query, limit = 8) { |
| 63 | + const keywords = keywordsFor(query); |
| 64 | + if (!keywords.length) { |
| 65 | + return { keywords, results: [] }; |
| 66 | + } |
| 67 | + |
| 68 | + const docs = Array.isArray(corpus?.docs) ? corpus.docs : []; |
| 69 | + const pathMatches = []; |
| 70 | + const contentMatches = []; |
| 71 | + |
| 72 | + for (const doc of docs) { |
| 73 | + const pathHits = distinctHits(doc.path, keywords); |
| 74 | + if (pathHits.length) { |
| 75 | + pathMatches.push(toResult(doc, keywords, pathHits, "path")); |
| 76 | + } |
| 77 | + |
| 78 | + const contentHits = distinctHits(doc.content, keywords); |
| 79 | + if (contentHits.length) { |
| 80 | + contentMatches.push(toResult(doc, keywords, contentHits, "content")); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const seen = new Set(); |
| 85 | + const results = []; |
| 86 | + for (const result of [ |
| 87 | + ...rankMatches(pathMatches), |
| 88 | + ...rankMatches(contentMatches), |
| 89 | + ]) { |
| 90 | + if (seen.has(result.path)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + seen.add(result.path); |
| 94 | + results.push(result); |
| 95 | + if (results.length >= limit) { |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return { keywords, results }; |
| 101 | +} |
0 commit comments