Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

add: preview to docs search results

Ben Fornefeld9784065930f2
diff --git a/apps/web/src/components/Search.tsx b/apps/web/src/components/Search.tsx
index 96526b30c..8ef416862 100644
--- a/apps/web/src/components/Search.tsx
+++ b/apps/web/src/components/Search.tsx
@@ -9,6 +9,7 @@ import {
useId,
useRef,
useState,
+ useMemo,
} from 'react'
import Highlighter from 'react-highlight-words'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
@@ -169,7 +170,7 @@ function SearchResult({
collection,
query,
}: {
- result: Result
+ result: Result & { preview?: string }
resultIndex: number
autocomplete: Autocomplete
collection: AutocompleteCollection<Result>
@@ -191,6 +192,21 @@ function SearchResult({
(x): x is string => typeof x === 'string'
)
+ const contextPreview = useMemo(() => {
+ if (!result.preview || !query) return result.preview
+
+ const previewText = result.preview.toLowerCase()
+ const queryIndex = previewText.indexOf(query.toLowerCase())
+
+ if (queryIndex === -1) return result.preview
+
+ const prefixChars = 10
+ const start = Math.max(0, queryIndex - prefixChars)
+ const prefix = start > 0 ? '...' : ''
+
+ return prefix + result.preview.slice(start)
+ }, [result.preview, query])
+
return (
<li
className={clsx(
@@ -232,6 +248,11 @@ function SearchResult({
))}
</div>
)}
+ {contextPreview && (
+ <div className="mt-2 text-xs text-zinc-600 dark:text-zinc-400 line-clamp-2">
+ <HighlightQuery text={contextPreview} query={query} />
+ </div>
+ )}
</li>
)
}
diff --git a/apps/web/src/mdx/search.mjs b/apps/web/src/mdx/search.mjs
index 26b958860..7c6c5d7bb 100644
--- a/apps/web/src/mdx/search.mjs
+++ b/apps/web/src/mdx/search.mjs
@@ -96,7 +96,7 @@ export default function (nextConfig = {}) {
document: {
id: 'url',
index: 'content',
- store: ['title', 'pageTitle'],
+ store: ['title', 'pageTitle', 'preview'],
},
context: {
resolution: 9,
@@ -114,6 +114,7 @@ export default function (nextConfig = {}) {
title,
content: [title, ...content].join('\\n'),
pageTitle: hash ? sections[0][0] : undefined,
+ preview: content.join('\\n'),
})
}
}
@@ -130,6 +131,7 @@ export default function (nextConfig = {}) {
url: item.id,
title: item.doc.title,
pageTitle: item.doc.pageTitle,
+ preview: item.doc.preview,
}))
}
`
diff --git a/apps/web/types.d.ts b/apps/web/types.d.ts
index 2a2370723..c08e065f1 100644
--- a/apps/web/types.d.ts
+++ b/apps/web/types.d.ts
@@ -5,6 +5,7 @@ declare module '@/mdx/search.mjs' {
url: string
title: string
pageTitle?: string
+ preview?: string
}
export function search(query: string, options?: SearchOptions): Array<Result>