Skip to content

Commit f9ed661

Browse files
fix(search): limit search result snippets to 200 chars and increase result cap
Truncates each search result description to 200 characters Prevents result list overflow that hides other hits Updated search result return limit from 5 to 10 Added CSS line-clamp for cleaner result display
1 parent be897a1 commit f9ed661

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

antora-ui-camel/src/css/header.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ html:not([data-scroll='0']) .navbar {
380380
.result_summary {
381381
color: var(--color-jet-50);
382382
padding-top: 0.3rem;
383+
display: -webkit-box;
384+
-webkit-box-orient: vertical;
385+
-webkit-line-clamp: 3;
386+
overflow: hidden;
383387
}
384388

385389
#search_results div.footer-search {

antora-ui-camel/src/js/vendor/algoliasearch.bundle.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
const algoliasearch = require('algoliasearch/lite')
55

6+
const MAX_SNIPPET_LENGTH = 200
7+
const RESULTS_LIMIT = 10
8+
69
// Sub-projects to exclude from main search - users can browse these directly
710
const EXCLUDED_SUBPROJECTS = [
811
'/camel-k/',
@@ -21,6 +24,60 @@
2124
})
2225
}
2326

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+
2481
window.addEventListener('load', () => {
2582
const client = algoliasearch('V62SL4FFIW', '1b7e52df4759e32dd49adedb286997f6')
2683
const index = client.initIndex('apache_camel')
@@ -70,7 +127,7 @@
70127
// Filter out sub-project results to focus on camel-core documentation
71128
const filteredHits = results.hits.filter(function (hit) {
72129
return !isSubProjectUrl(hit.url)
73-
}).slice(0, 5)
130+
}).slice(0, RESULTS_LIMIT)
74131
const data = filteredHits.reduce((data, hit) => {
75132
const section = hit.hierarchy.lvl0
76133
const sectionKey = `${section}-${hit.version || ''}`
@@ -86,7 +143,7 @@
86143
.slice(1)
87144
.filter((lvl) => lvl !== null)
88145
.join(' / '),
89-
snippet: hit._highlightResult.content.value,
146+
snippet: truncateHighlightedHtml(hit._highlightResult.content.value, MAX_SNIPPET_LENGTH),
90147
})
91148

92149
return data

0 commit comments

Comments
 (0)