feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
export interface SearchEntry {
|
||||
title: string;
|
||||
section: string;
|
||||
url: string;
|
||||
text: string;
|
||||
}
|
||||
export function searchDocs(
|
||||
entries: SearchEntry[],
|
||||
query: string,
|
||||
): SearchEntry[] {
|
||||
const terms = query.toLocaleLowerCase().trim().split(/\s+/).filter(Boolean);
|
||||
if (!terms.length) return [];
|
||||
return entries
|
||||
.map((entry, index) => {
|
||||
const title = entry.title.toLocaleLowerCase(),
|
||||
section = entry.section.toLocaleLowerCase(),
|
||||
text = entry.text.toLocaleLowerCase();
|
||||
if (
|
||||
!terms.every((term) =>
|
||||
(title + " " + section + " " + text).includes(term),
|
||||
)
|
||||
)
|
||||
return null;
|
||||
const score = terms.reduce(
|
||||
(score, term) =>
|
||||
score +
|
||||
(title.includes(term) ? 12 : 0) +
|
||||
(section.includes(term) ? 8 : 0) +
|
||||
(text.includes(term) ? 1 : 0),
|
||||
0,
|
||||
);
|
||||
return { entry, score, index };
|
||||
})
|
||||
.filter((item): item is NonNullable<typeof item> => item !== null)
|
||||
.sort((a, b) => b.score - a.score || a.index - b.index)
|
||||
.slice(0, 20)
|
||||
.map((item) => item.entry);
|
||||
}
|
||||
|
||||
export function searchExcerpt(entry: SearchEntry, query: string): string {
|
||||
const text = entry.text
|
||||
.replace(/[-:|]{3,}/g, " ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
const terms = query.trim().toLocaleLowerCase().split(/\s+/).filter(Boolean);
|
||||
const positions = terms
|
||||
.map((term) => text.toLocaleLowerCase().indexOf(term))
|
||||
.filter((index) => index >= 0);
|
||||
const start = positions.length ? Math.max(0, Math.min(...positions) - 28) : 0;
|
||||
return (
|
||||
(start ? "…" : "") +
|
||||
text.slice(start, start + 140) +
|
||||
(text.length > start + 140 ? "…" : "")
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user