Repository navigation
Expand file tree
/
Copy pathsearch-index.plugin.ts
More file actions
221 lines (208 loc) · 7.21 KB
/
Copy pathsearch-index.plugin.ts
File metadata and controls
221 lines (208 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {readFileSync, statSync} from 'node:fs';
import {join} from 'node:path';
import type {Plugin, ViteDevServer} from 'vite';
import type {IndexDoc, SearchHitKind} from './src/types/search.ts';
import {
createSlugger,
fenceTracker,
headingText as headingTextOf,
isNoIndex,
parseFrontmatter,
walkContentFiles,
} from './plugin-utils.ts';
/**
* Build-time search index. Walks `src/content/**\/*.md` and emits a flat list
* of `IndexDoc` records (one per page + one per `##` heading + one per
* paragraph chunk) under the virtual id `virtual:ngmd/search-index`.
*
* The runtime Orama provider takes this raw list, builds an in-memory
* index, and queries it. Algolia's hosted index uses a similar
* page → section → snippet shape, so the same `SearchHit` UI works against
* either backend.
*
* Pages can opt out by setting `noIndex: true` in their frontmatter.
*/
const VIRTUAL_ID = 'virtual:ngmd/search-index';
const RESOLVED_ID = '\0' + VIRTUAL_ID;
const ENTITIES: Record<string, string> = {
'<': '<',
'>': '>',
'&': '&',
'"': '"',
''': "'",
''': "'",
'@': '@',
' ': ' ',
};
/** Strip markdown syntax so search hits show clean prose, not markup. */
function stripMarkdown(s: string): string {
const code: string[] = [];
return s
.replace(/(`+)([\s\S]*?)\1/g, (_, _ticks: string, inner: string) => {
code.push(inner.trim());
return `\u0000${code.length - 1}\u0000`;
})
.replace(/<[^>]+>/g, ' ')
.replace(/!?\[([^\]]*)\]\([^)]+\)/g, '$1')
.replace(/[*_#>]/g, '')
.replace(/&(?:lt|gt|amp|quot|apos|nbsp|#39|#64);/g, (m) => ENTITIES[m] ?? m)
.replace(/\u0000(\d+)\u0000/g, (_, i: string) => code[Number(i)])
.replace(/\s+/g, ' ')
.trim();
}
/** Split raw markdown body into sections delimited by `##`+ headings.
* Returns an array where each entry has the heading text (or empty for
* the lead-in before the first heading) and the prose that follows. */
function splitSections(body: string): Array<{heading: string; body: string}> {
const lines = body.split(/\r?\n/);
const sections: Array<{heading: string; body: string}> = [];
let current: {heading: string; body: string} = {heading: '', body: ''};
const inFence = fenceTracker();
for (const line of lines) {
if (inFence(line)) continue;
const m = line.match(/^ {0,3}(#{2,6})\s+(.+?)\s*$/);
if (m) {
if (current.heading || current.body.trim()) sections.push(current);
current = {heading: m[2], body: ''};
} else {
current.body += line + '\n';
}
}
if (current.heading || current.body.trim()) sections.push(current);
return sections;
}
/** Break a stripped body into snippet chunks. ~280 chars each, snapped to
* a sentence boundary if one is nearby and to a word boundary otherwise so
* chunks never start or end mid-word. */
function chunkBody(body: string, target = 280): string[] {
if (!body) return [];
const chunks: string[] = [];
let i = 0;
while (i < body.length) {
let end = Math.min(body.length, i + target);
if (end < body.length) {
const sentence = body.lastIndexOf('. ', end);
if (sentence > i + 80) {
end = sentence + 1;
} else {
const word = body.lastIndexOf(' ', end);
if (word > i + 80) end = word;
}
}
const piece = body.slice(i, end).trim();
if (piece) chunks.push(piece);
i = end;
}
return chunks;
}
export function searchIndexPlugin(): Plugin {
let root = process.cwd();
let server: ViteDevServer | undefined;
return {
name: 'ngmd-search-index',
configResolved(cfg) {
root = cfg.root;
},
configureServer(s) {
server = s;
},
watchChange(id) {
if (!server || !id.endsWith('.md')) return;
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
if (mod) server.moduleGraph.invalidateModule(mod);
},
resolveId(id) {
if (id === VIRTUAL_ID) return RESOLVED_ID;
return null;
},
load(id) {
if (id !== RESOLVED_ID) return null;
const docs: IndexDoc[] = [];
const contentDir = join(root, 'src/content');
try {
statSync(contentDir);
} catch {
return `export const searchIndex = [];`;
}
for (const [rel, url] of walkContentFiles(contentDir, contentDir)) {
let raw: string;
try {
raw = readFileSync(join(contentDir, rel), 'utf8');
} catch {
continue;
}
const {attributes, body} = parseFrontmatter(raw);
if (isNoIndex(attributes)) continue;
const slug = url.split('/').pop() || '';
const pageTitle =
typeof attributes['title'] === 'string'
? attributes['title']
: slug.replace(/-/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
// page record: title-only. Body matches surface through snippet
// records below, which carry their enclosing heading's anchor so
// a click jumps to the right section instead of the page top.
// Keeping body off the page record stops fuzzy hits from dragging
// unrelated pages into the result list on short queries.
docs.push({
id: `page:${url}`,
url,
anchor: '',
kind: 'page' as SearchHitKind,
pageTitle,
heading: pageTitle,
body: '',
});
// Split the body into sections delimited by `##`+ headings. Pure
// prose before the first heading goes under an empty anchor (lands
// on the page top). Each section produces (1) a section record at
// its heading anchor and (2) snippet records anchored to the same
// heading so clicking a snippet jumps to its section, not the top.
const anchorFor = createSlugger();
for (const section of splitSections(body)) {
if (section.heading) {
const headingText = stripMarkdown(
section.heading.replace(/<ngmd-badge\b[^>]*>[\s\S]*?<\/ngmd-badge>/g, ''),
);
const anchor = anchorFor(headingTextOf(section.heading));
docs.push({
id: `section:${url}#${anchor}`,
url,
anchor,
kind: 'section',
pageTitle,
heading: headingText,
body: '',
});
const sectionBody = stripMarkdown(section.body);
for (const [i, chunk] of chunkBody(sectionBody).entries()) {
docs.push({
id: `snippet:${url}#${anchor}:${i}`,
url,
anchor,
kind: 'snippet',
pageTitle,
heading: headingText,
body: chunk,
});
}
} else {
// Pre-first-heading prose. Anchorless.
const sectionBody = stripMarkdown(section.body);
for (const [i, chunk] of chunkBody(sectionBody).entries()) {
docs.push({
id: `snippet:${url}:lead:${i}`,
url,
anchor: '',
kind: 'snippet',
pageTitle,
heading: '',
body: chunk,
});
}
}
}
}
return `export const searchIndex = ${JSON.stringify(docs)};`;
},
};
}