@md-plugins/search-ui 0.1.0-rc.9
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.md +21 -0
- package/README.md +97 -0
- package/dist/index.d.mts +133 -0
- package/dist/index.d.ts +133 -0
- package/dist/index.mjs +991 -0
- package/package.json +46 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present MD-Plugins
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# @md-plugins/search-ui
|
|
2
|
+
|
|
3
|
+
Framework-agnostic search UI for search indexes generated by
|
|
4
|
+
`@md-plugins/vite-search-plugin`.
|
|
5
|
+
|
|
6
|
+
The package provides:
|
|
7
|
+
|
|
8
|
+
- a static JSON search provider for `search/search-index.json`
|
|
9
|
+
- normalized search result types
|
|
10
|
+
- the `<md-search>` Web Component
|
|
11
|
+
- CSS custom properties and `part` attributes for site-specific styling
|
|
12
|
+
- an icon-only mobile trigger that keeps search available in narrow headers
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @md-plugins/search-ui
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Static JSON Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { defineMdSearchElement } from '@md-plugins/search-ui'
|
|
24
|
+
|
|
25
|
+
defineMdSearchElement()
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```html
|
|
29
|
+
<md-search src="/search/search-index.json"></md-search>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Search results collapse duplicate records by default. This keeps generated heading/content pairs
|
|
33
|
+
that point to the same URL from appearing twice in the panel. Add `show-duplicate-results` when
|
|
34
|
+
you want to inspect the raw index output:
|
|
35
|
+
|
|
36
|
+
```html
|
|
37
|
+
<md-search src="/search/search-index.json" show-duplicate-results></md-search>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Styling
|
|
41
|
+
|
|
42
|
+
The component uses Shadow DOM, CSS custom properties, and `part` attributes:
|
|
43
|
+
|
|
44
|
+
```css
|
|
45
|
+
md-search {
|
|
46
|
+
--md-search-accent: #1976d2;
|
|
47
|
+
--md-search-surface: white;
|
|
48
|
+
--md-search-text: #102033;
|
|
49
|
+
--md-search-radius: 14px;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
md-search::part(dialog) {
|
|
53
|
+
box-shadow: 0 24px 80px rgb(0 0 0 / 24%);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The component supports light and dark modes. By default it follows `prefers-color-scheme`. Use the
|
|
58
|
+
`theme` attribute when your application owns the active theme:
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<md-search src="/search/search-index.json" theme="dark"></md-search>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Use `theme="light"` to force light mode. Site CSS variables can still override either theme.
|
|
65
|
+
|
|
66
|
+
## Accessibility
|
|
67
|
+
|
|
68
|
+
`<md-search>` includes keyboard and screen-reader support:
|
|
69
|
+
|
|
70
|
+
- `Ctrl+K` / `Cmd+K` opens the search by default.
|
|
71
|
+
- `Escape` closes the dialog.
|
|
72
|
+
- `ArrowUp` and `ArrowDown` move between results.
|
|
73
|
+
- `Enter` selects the active result.
|
|
74
|
+
- The dialog uses `role="dialog"` and `aria-modal`.
|
|
75
|
+
- The input exposes combobox/listbox relationships through ARIA attributes.
|
|
76
|
+
- Result rows use `role="option"` and update the active descendant.
|
|
77
|
+
|
|
78
|
+
Use `search-label` to provide a project-specific input label:
|
|
79
|
+
|
|
80
|
+
```html
|
|
81
|
+
<md-search src="/search/search-index.json" search-label="Search product documentation"></md-search>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Custom Providers
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { createSearch } from '@md-plugins/search-ui'
|
|
88
|
+
|
|
89
|
+
createSearch({
|
|
90
|
+
target: document.querySelector('#search')!,
|
|
91
|
+
provider: {
|
|
92
|
+
async search(query) {
|
|
93
|
+
return mySearchService(query)
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
2
|
+
type SearchRecordType = 'page' | 'heading' | 'content';
|
|
3
|
+
interface SearchRecordSource {
|
|
4
|
+
file: string;
|
|
5
|
+
relativeFile: string;
|
|
6
|
+
}
|
|
7
|
+
interface SearchRecord {
|
|
8
|
+
id: string;
|
|
9
|
+
type: SearchRecordType;
|
|
10
|
+
title: string;
|
|
11
|
+
content: string;
|
|
12
|
+
url: string;
|
|
13
|
+
path: string;
|
|
14
|
+
anchor?: string;
|
|
15
|
+
section?: string;
|
|
16
|
+
hierarchy: string[];
|
|
17
|
+
tags: string[];
|
|
18
|
+
source: SearchRecordSource;
|
|
19
|
+
meta?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
interface SearchIndex {
|
|
22
|
+
generatedAt: string;
|
|
23
|
+
recordCount: number;
|
|
24
|
+
records: SearchRecord[];
|
|
25
|
+
}
|
|
26
|
+
interface SearchResult {
|
|
27
|
+
id: string;
|
|
28
|
+
title: string;
|
|
29
|
+
url: string;
|
|
30
|
+
content: string;
|
|
31
|
+
score: number;
|
|
32
|
+
type: SearchRecordType;
|
|
33
|
+
path: string;
|
|
34
|
+
section?: string;
|
|
35
|
+
hierarchy: string[];
|
|
36
|
+
tags: string[];
|
|
37
|
+
record: SearchRecord;
|
|
38
|
+
}
|
|
39
|
+
interface SearchOptions {
|
|
40
|
+
limit?: number;
|
|
41
|
+
collapseDuplicateResults?: boolean;
|
|
42
|
+
}
|
|
43
|
+
interface SearchProvider {
|
|
44
|
+
search: (query: string, options?: SearchOptions) => MaybePromise<SearchResult[]>;
|
|
45
|
+
}
|
|
46
|
+
interface JsonSearchProviderOptions {
|
|
47
|
+
src?: string;
|
|
48
|
+
index?: SearchIndex | SearchRecord[];
|
|
49
|
+
fetcher?: typeof fetch;
|
|
50
|
+
}
|
|
51
|
+
interface MdSearchSelectEventDetail {
|
|
52
|
+
result: SearchResult;
|
|
53
|
+
}
|
|
54
|
+
interface CreateSearchOptions {
|
|
55
|
+
target: Element;
|
|
56
|
+
src?: string;
|
|
57
|
+
provider?: SearchProvider;
|
|
58
|
+
placeholder?: string;
|
|
59
|
+
triggerLabel?: string;
|
|
60
|
+
panelTitle?: string;
|
|
61
|
+
searchLabel?: string;
|
|
62
|
+
theme?: 'light' | 'dark';
|
|
63
|
+
shortcut?: string;
|
|
64
|
+
minQueryLength?: number;
|
|
65
|
+
maxResults?: number;
|
|
66
|
+
showDuplicateResults?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
declare const HTMLElementBase: {
|
|
70
|
+
new (): HTMLElement;
|
|
71
|
+
prototype: HTMLElement;
|
|
72
|
+
};
|
|
73
|
+
declare class MdSearchElement extends HTMLElementBase {
|
|
74
|
+
static observedAttributes: string[];
|
|
75
|
+
provider?: SearchProvider;
|
|
76
|
+
private readonly root;
|
|
77
|
+
private readonly elementId;
|
|
78
|
+
private results;
|
|
79
|
+
private query;
|
|
80
|
+
private opened;
|
|
81
|
+
private loading;
|
|
82
|
+
private errorMessage;
|
|
83
|
+
private activeIndex;
|
|
84
|
+
private searchTimer;
|
|
85
|
+
private searchRequestId;
|
|
86
|
+
private selectionStart;
|
|
87
|
+
private selectionEnd;
|
|
88
|
+
connectedCallback(): void;
|
|
89
|
+
disconnectedCallback(): void;
|
|
90
|
+
attributeChangedCallback(name: string): void;
|
|
91
|
+
open(): void;
|
|
92
|
+
close(): void;
|
|
93
|
+
toggle(): void;
|
|
94
|
+
private get src();
|
|
95
|
+
private get placeholder();
|
|
96
|
+
private get triggerLabel();
|
|
97
|
+
private get panelTitle();
|
|
98
|
+
private get searchLabel();
|
|
99
|
+
private get shortcut();
|
|
100
|
+
private get minQueryLength();
|
|
101
|
+
private get maxResults();
|
|
102
|
+
private get showDuplicateResults();
|
|
103
|
+
private get activeResult();
|
|
104
|
+
private getProvider;
|
|
105
|
+
private readonly onGlobalKeydown;
|
|
106
|
+
private readonly onInput;
|
|
107
|
+
private readonly onDialogKeydown;
|
|
108
|
+
private runSearch;
|
|
109
|
+
private focusInput;
|
|
110
|
+
private setActiveIndex;
|
|
111
|
+
private syncInputAria;
|
|
112
|
+
private syncActiveResult;
|
|
113
|
+
private selectResult;
|
|
114
|
+
private bindResultEvents;
|
|
115
|
+
private renderResultsContainer;
|
|
116
|
+
private renderResults;
|
|
117
|
+
private render;
|
|
118
|
+
private bindEvents;
|
|
119
|
+
}
|
|
120
|
+
declare function defineMdSearchElement(name?: string): void;
|
|
121
|
+
declare function createSearch(options: CreateSearchOptions): MdSearchElement;
|
|
122
|
+
|
|
123
|
+
declare function searchRecords(records: SearchRecord[], query: string, options?: SearchOptions): SearchResult[];
|
|
124
|
+
declare function createStaticSearchProvider(records: SearchRecord[]): SearchProvider;
|
|
125
|
+
declare function createJsonSearchProvider(options?: JsonSearchProviderOptions): SearchProvider;
|
|
126
|
+
|
|
127
|
+
declare function normalizeSearchQuery(value: string): string;
|
|
128
|
+
declare function createSearchTerms(query: string): string[];
|
|
129
|
+
declare function normalizeSearchableText(value: unknown): string;
|
|
130
|
+
declare function createSearchSnippet(content: string, terms: string[], length?: number): string;
|
|
131
|
+
|
|
132
|
+
export { MdSearchElement, createJsonSearchProvider, createSearch, createSearchSnippet, createSearchTerms, createStaticSearchProvider, defineMdSearchElement, normalizeSearchQuery, normalizeSearchableText, searchRecords };
|
|
133
|
+
export type { CreateSearchOptions, JsonSearchProviderOptions, MaybePromise, MdSearchSelectEventDetail, SearchIndex, SearchOptions, SearchProvider, SearchRecord, SearchRecordSource, SearchRecordType, SearchResult };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
2
|
+
type SearchRecordType = 'page' | 'heading' | 'content';
|
|
3
|
+
interface SearchRecordSource {
|
|
4
|
+
file: string;
|
|
5
|
+
relativeFile: string;
|
|
6
|
+
}
|
|
7
|
+
interface SearchRecord {
|
|
8
|
+
id: string;
|
|
9
|
+
type: SearchRecordType;
|
|
10
|
+
title: string;
|
|
11
|
+
content: string;
|
|
12
|
+
url: string;
|
|
13
|
+
path: string;
|
|
14
|
+
anchor?: string;
|
|
15
|
+
section?: string;
|
|
16
|
+
hierarchy: string[];
|
|
17
|
+
tags: string[];
|
|
18
|
+
source: SearchRecordSource;
|
|
19
|
+
meta?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
interface SearchIndex {
|
|
22
|
+
generatedAt: string;
|
|
23
|
+
recordCount: number;
|
|
24
|
+
records: SearchRecord[];
|
|
25
|
+
}
|
|
26
|
+
interface SearchResult {
|
|
27
|
+
id: string;
|
|
28
|
+
title: string;
|
|
29
|
+
url: string;
|
|
30
|
+
content: string;
|
|
31
|
+
score: number;
|
|
32
|
+
type: SearchRecordType;
|
|
33
|
+
path: string;
|
|
34
|
+
section?: string;
|
|
35
|
+
hierarchy: string[];
|
|
36
|
+
tags: string[];
|
|
37
|
+
record: SearchRecord;
|
|
38
|
+
}
|
|
39
|
+
interface SearchOptions {
|
|
40
|
+
limit?: number;
|
|
41
|
+
collapseDuplicateResults?: boolean;
|
|
42
|
+
}
|
|
43
|
+
interface SearchProvider {
|
|
44
|
+
search: (query: string, options?: SearchOptions) => MaybePromise<SearchResult[]>;
|
|
45
|
+
}
|
|
46
|
+
interface JsonSearchProviderOptions {
|
|
47
|
+
src?: string;
|
|
48
|
+
index?: SearchIndex | SearchRecord[];
|
|
49
|
+
fetcher?: typeof fetch;
|
|
50
|
+
}
|
|
51
|
+
interface MdSearchSelectEventDetail {
|
|
52
|
+
result: SearchResult;
|
|
53
|
+
}
|
|
54
|
+
interface CreateSearchOptions {
|
|
55
|
+
target: Element;
|
|
56
|
+
src?: string;
|
|
57
|
+
provider?: SearchProvider;
|
|
58
|
+
placeholder?: string;
|
|
59
|
+
triggerLabel?: string;
|
|
60
|
+
panelTitle?: string;
|
|
61
|
+
searchLabel?: string;
|
|
62
|
+
theme?: 'light' | 'dark';
|
|
63
|
+
shortcut?: string;
|
|
64
|
+
minQueryLength?: number;
|
|
65
|
+
maxResults?: number;
|
|
66
|
+
showDuplicateResults?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
declare const HTMLElementBase: {
|
|
70
|
+
new (): HTMLElement;
|
|
71
|
+
prototype: HTMLElement;
|
|
72
|
+
};
|
|
73
|
+
declare class MdSearchElement extends HTMLElementBase {
|
|
74
|
+
static observedAttributes: string[];
|
|
75
|
+
provider?: SearchProvider;
|
|
76
|
+
private readonly root;
|
|
77
|
+
private readonly elementId;
|
|
78
|
+
private results;
|
|
79
|
+
private query;
|
|
80
|
+
private opened;
|
|
81
|
+
private loading;
|
|
82
|
+
private errorMessage;
|
|
83
|
+
private activeIndex;
|
|
84
|
+
private searchTimer;
|
|
85
|
+
private searchRequestId;
|
|
86
|
+
private selectionStart;
|
|
87
|
+
private selectionEnd;
|
|
88
|
+
connectedCallback(): void;
|
|
89
|
+
disconnectedCallback(): void;
|
|
90
|
+
attributeChangedCallback(name: string): void;
|
|
91
|
+
open(): void;
|
|
92
|
+
close(): void;
|
|
93
|
+
toggle(): void;
|
|
94
|
+
private get src();
|
|
95
|
+
private get placeholder();
|
|
96
|
+
private get triggerLabel();
|
|
97
|
+
private get panelTitle();
|
|
98
|
+
private get searchLabel();
|
|
99
|
+
private get shortcut();
|
|
100
|
+
private get minQueryLength();
|
|
101
|
+
private get maxResults();
|
|
102
|
+
private get showDuplicateResults();
|
|
103
|
+
private get activeResult();
|
|
104
|
+
private getProvider;
|
|
105
|
+
private readonly onGlobalKeydown;
|
|
106
|
+
private readonly onInput;
|
|
107
|
+
private readonly onDialogKeydown;
|
|
108
|
+
private runSearch;
|
|
109
|
+
private focusInput;
|
|
110
|
+
private setActiveIndex;
|
|
111
|
+
private syncInputAria;
|
|
112
|
+
private syncActiveResult;
|
|
113
|
+
private selectResult;
|
|
114
|
+
private bindResultEvents;
|
|
115
|
+
private renderResultsContainer;
|
|
116
|
+
private renderResults;
|
|
117
|
+
private render;
|
|
118
|
+
private bindEvents;
|
|
119
|
+
}
|
|
120
|
+
declare function defineMdSearchElement(name?: string): void;
|
|
121
|
+
declare function createSearch(options: CreateSearchOptions): MdSearchElement;
|
|
122
|
+
|
|
123
|
+
declare function searchRecords(records: SearchRecord[], query: string, options?: SearchOptions): SearchResult[];
|
|
124
|
+
declare function createStaticSearchProvider(records: SearchRecord[]): SearchProvider;
|
|
125
|
+
declare function createJsonSearchProvider(options?: JsonSearchProviderOptions): SearchProvider;
|
|
126
|
+
|
|
127
|
+
declare function normalizeSearchQuery(value: string): string;
|
|
128
|
+
declare function createSearchTerms(query: string): string[];
|
|
129
|
+
declare function normalizeSearchableText(value: unknown): string;
|
|
130
|
+
declare function createSearchSnippet(content: string, terms: string[], length?: number): string;
|
|
131
|
+
|
|
132
|
+
export { MdSearchElement, createJsonSearchProvider, createSearch, createSearchSnippet, createSearchTerms, createStaticSearchProvider, defineMdSearchElement, normalizeSearchQuery, normalizeSearchableText, searchRecords };
|
|
133
|
+
export type { CreateSearchOptions, JsonSearchProviderOptions, MaybePromise, MdSearchSelectEventDetail, SearchIndex, SearchOptions, SearchProvider, SearchRecord, SearchRecordSource, SearchRecordType, SearchResult };
|