|
3 | 3 |
|
4 | 4 | const algoliasearch = require('algoliasearch/lite') |
5 | 5 |
|
| 6 | + const MAX_SNIPPET_LENGTH = 200 |
| 7 | + const RESULTS_LIMIT = 10 |
| 8 | + |
6 | 9 | // Sub-projects to exclude from main search - users can browse these directly |
7 | 10 | const EXCLUDED_SUBPROJECTS = [ |
8 | 11 | '/camel-k/', |
|
21 | 24 | }) |
22 | 25 | } |
23 | 26 |
|
| 27 | + function truncateHighlightedHtml (html, maxChars) { |
| 28 | + if (!html || maxChars <= 0) return '' |
| 29 | + |
| 30 | + const template = document.createElement('template') |
| 31 | + template.innerHTML = html |
| 32 | + |
| 33 | + let remaining = maxChars |
| 34 | + let truncated = false |
| 35 | + |
| 36 | + const TEXT_NODE = 3 |
| 37 | + const ELEMENT_NODE = 1 |
| 38 | + |
| 39 | + function cloneUntilLimit (node) { |
| 40 | + if (remaining <= 0) return null |
| 41 | + |
| 42 | + if (node.nodeType === TEXT_NODE) { |
| 43 | + const text = node.nodeValue || '' |
| 44 | + if (text.length <= remaining) { |
| 45 | + remaining -= text.length |
| 46 | + return document.createTextNode(text) |
| 47 | + } |
| 48 | + truncated = true |
| 49 | + const slice = text.slice(0, remaining) |
| 50 | + remaining = 0 |
| 51 | + return document.createTextNode(slice) |
| 52 | + } |
| 53 | + |
| 54 | + if (node.nodeType === ELEMENT_NODE) { |
| 55 | + const el = node |
| 56 | + const cloned = el.cloneNode(false) |
| 57 | + for (const child of Array.from(el.childNodes)) { |
| 58 | + if (remaining <= 0) break |
| 59 | + const childClone = cloneUntilLimit(child) |
| 60 | + if (childClone) cloned.appendChild(childClone) |
| 61 | + } |
| 62 | + return cloned |
| 63 | + } |
| 64 | + |
| 65 | + return null |
| 66 | + } |
| 67 | + |
| 68 | + const outFragment = document.createDocumentFragment() |
| 69 | + for (const child of Array.from(template.content.childNodes)) { |
| 70 | + if (remaining <= 0) break |
| 71 | + const childClone = cloneUntilLimit(child) |
| 72 | + if (childClone) outFragment.appendChild(childClone) |
| 73 | + } |
| 74 | + if (truncated) outFragment.appendChild(document.createTextNode('…')) |
| 75 | + |
| 76 | + const container = document.createElement('div') |
| 77 | + container.appendChild(outFragment) |
| 78 | + return container.innerHTML |
| 79 | + } |
| 80 | + |
24 | 81 | window.addEventListener('load', () => { |
25 | 82 | const client = algoliasearch('V62SL4FFIW', '1b7e52df4759e32dd49adedb286997f6') |
26 | 83 | const index = client.initIndex('apache_camel') |
|
70 | 127 | // Filter out sub-project results to focus on camel-core documentation |
71 | 128 | const filteredHits = results.hits.filter(function (hit) { |
72 | 129 | return !isSubProjectUrl(hit.url) |
73 | | - }).slice(0, 5) |
| 130 | + }).slice(0, RESULTS_LIMIT) |
74 | 131 | const data = filteredHits.reduce((data, hit) => { |
75 | 132 | const section = hit.hierarchy.lvl0 |
76 | 133 | const sectionKey = `${section}-${hit.version || ''}` |
|
86 | 143 | .slice(1) |
87 | 144 | .filter((lvl) => lvl !== null) |
88 | 145 | .join(' / '), |
89 | | - snippet: hit._highlightResult.content.value, |
| 146 | + snippet: truncateHighlightedHtml(hit._highlightResult.content.value, MAX_SNIPPET_LENGTH), |
90 | 147 | }) |
91 | 148 |
|
92 | 149 | return data |
|
0 commit comments