@docs-sdk-dev/solid 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/assets/agent.ts +11 -0
- package/assets/client.tsx +65 -0
- package/assets/copy.ts +30 -0
- package/assets/examples.tsx +96 -0
- package/assets/highlight.ts +64 -0
- package/assets/labels.ts +109 -0
- package/assets/render.tsx +198 -0
- package/assets/search.tsx +99 -0
- package/assets/style.css +779 -0
- package/assets/virtual.d.ts +6 -0
- package/dist/component-reference.d.ts +55 -0
- package/dist/component-reference.js +146 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +158 -0
- package/package.json +38 -4
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { closestSubheading, queryTerms } from "@docs-sdk-dev/core/reader"
|
|
2
|
+
import { Command, CommandEmpty, CommandInput, CommandItem, CommandList } from "@ui-sdk/solid/command"
|
|
3
|
+
import { Dialog, DialogBackdrop, DialogContent, DialogShortcut, DialogTitle, DialogTrigger } from "@ui-sdk/solid/dialog"
|
|
4
|
+
import { render } from "@solidjs/web"
|
|
5
|
+
import { createStaticSearchClient, type StaticSearchClient } from "@zbsearch/static"
|
|
6
|
+
import { createSignal, For } from "solid-js"
|
|
7
|
+
import { fill, readClientLabels } from "./labels.ts"
|
|
8
|
+
|
|
9
|
+
/** What a search record stores for a result; the index is built from these (see searchFiles). */
|
|
10
|
+
type Hit = { title: string; heading?: string; route: string; snippet: string; context: Partial<{ [key: string]: string }>;
|
|
11
|
+
subheadings?: { anchor: string; text: string }[] }
|
|
12
|
+
type SearchResult = { title: string; heading?: string; subheading?: string; route: string; snippet: string }
|
|
13
|
+
|
|
14
|
+
// As in Lenso Docs, the search box reads a static, sharded index: the dictionary first, then only the shards a query
|
|
15
|
+
// needs. Titles count most, then headings; body text least.
|
|
16
|
+
const fields = ["title", "heading", "subheadingText", "description", "text"]
|
|
17
|
+
const boost = { title: 4, heading: 3, subheadingText: 2, description: 2 }
|
|
18
|
+
let baseUrl = "/search/all/"
|
|
19
|
+
let client: Promise<StaticSearchClient> | undefined
|
|
20
|
+
const load = () => client ??= (async () => {
|
|
21
|
+
const searcher = createStaticSearchClient({ baseUrl })
|
|
22
|
+
await searcher.preload()
|
|
23
|
+
return searcher
|
|
24
|
+
})()
|
|
25
|
+
/** Editions agree when every dimension the record has matches the page's; a page without editions matches any. */
|
|
26
|
+
const compatible = (record: Hit["context"], context: { [key: string]: string }) =>
|
|
27
|
+
Object.entries(context).every(([key, value]) => !(key in record) || record[key] === value)
|
|
28
|
+
|
|
29
|
+
function Search(props: { context: Record<string, string>; initiallyOpen: boolean }) {
|
|
30
|
+
const labels = readClientLabels()
|
|
31
|
+
const [open, setOpen] = createSignal(props.initiallyOpen)
|
|
32
|
+
const [results, setResults] = createSignal<SearchResult[]>([])
|
|
33
|
+
const [failed, setFailed] = createSignal(false)
|
|
34
|
+
let latest = ""
|
|
35
|
+
async function query(text: string) {
|
|
36
|
+
latest = text
|
|
37
|
+
if (!text.trim()) {
|
|
38
|
+
setResults([])
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
// The words that carry the question, as agent search ranks them: "how do I" and "the" do not count.
|
|
43
|
+
const term = queryTerms(text, props.context.locale).join(" ")
|
|
44
|
+
const hits = (await (await load()).search<Hit>({ term, properties: fields, boost, limit: 40 })).hits
|
|
45
|
+
const found = hits.map(hit => hit.document).filter(record => compatible(record.context, props.context)).slice(0, 8)
|
|
46
|
+
.map((record): SearchResult => {
|
|
47
|
+
const subheading = closestSubheading(record.subheadings, text, props.context.locale)
|
|
48
|
+
return { title: record.title, ...(record.heading ? { heading: record.heading } : {}), snippet: record.snippet,
|
|
49
|
+
...(subheading ? { route: `${record.route.split("#")[0]}#${subheading.anchor}`, subheading: subheading.text } : { route: record.route }) }
|
|
50
|
+
})
|
|
51
|
+
if (latest === text) setResults(found)
|
|
52
|
+
} catch {
|
|
53
|
+
client = undefined
|
|
54
|
+
setFailed(true)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return (
|
|
58
|
+
<Dialog open={open()} onOpenChange={next => setOpen(next)}>
|
|
59
|
+
<DialogTrigger shortcut="mod+k">
|
|
60
|
+
{labels.search}
|
|
61
|
+
<DialogShortcut />
|
|
62
|
+
</DialogTrigger>
|
|
63
|
+
<DialogBackdrop />
|
|
64
|
+
<DialogContent>
|
|
65
|
+
<DialogTitle>{labels.searchTitle}</DialogTitle>
|
|
66
|
+
<Command filter={false} onQueryChange={query} onAction={route => {
|
|
67
|
+
setOpen(false)
|
|
68
|
+
location.assign(route)
|
|
69
|
+
}}>
|
|
70
|
+
<CommandInput aria-label={labels.searchQuery} placeholder={labels.searchPlaceholder} />
|
|
71
|
+
<CommandEmpty>
|
|
72
|
+
{state => failed() ? labels.searchFailed
|
|
73
|
+
: state.query.trim() ? fill(labels.searchNoResults, { query: state.query }) : labels.searchHint}
|
|
74
|
+
</CommandEmpty>
|
|
75
|
+
<CommandList>
|
|
76
|
+
<For each={results()}>
|
|
77
|
+
{result => (
|
|
78
|
+
<CommandItem value={result.route} label={[result.title, result.heading, result.subheading].filter(Boolean).join(" ")}>
|
|
79
|
+
<span data-search="title">{[result.title, result.heading, result.subheading].filter(Boolean).join(" › ")}</span>
|
|
80
|
+
<span data-search="snippet">{result.snippet}</span>
|
|
81
|
+
</CommandItem>
|
|
82
|
+
)}
|
|
83
|
+
</For>
|
|
84
|
+
</CommandList>
|
|
85
|
+
</Command>
|
|
86
|
+
</DialogContent>
|
|
87
|
+
</Dialog>
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function mountSearch(container: HTMLElement, initiallyOpen = true) {
|
|
92
|
+
const context = JSON.parse(container.dataset.docsContext ?? "{}") as Record<string, string>
|
|
93
|
+
baseUrl = container.dataset.docsSearchIndex ?? "/search/all/"
|
|
94
|
+
// Pages without a locale edition, such as guides, still have a language; search in it.
|
|
95
|
+
if (!context.locale && document.documentElement.lang) context.locale = document.documentElement.lang
|
|
96
|
+
container.replaceChildren()
|
|
97
|
+
render(() => <Search context={context} initiallyOpen={initiallyOpen} />, container)
|
|
98
|
+
void load().catch(() => undefined)
|
|
99
|
+
}
|