@kawapress/plugin-search 0.0.1-beta.1

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.
@@ -0,0 +1,484 @@
1
+ import type { MarkdownItContainerOptions } from '@mdit/plugin-container'
2
+ import type { MarkdownExit, Token } from 'markdown-exit'
3
+ import type { Dirent } from 'node:fs'
4
+ import type { Plugin } from 'vite'
5
+ import type { SearchDocument } from './search'
6
+ import { readdir, readFile } from 'node:fs/promises'
7
+ import { isAbsolute, join, relative, resolve, sep } from 'node:path'
8
+ import { frontmatterPlugin } from '@mdit-vue/plugin-frontmatter'
9
+ import { alert as alertPlugin } from '@mdit/plugin-alert'
10
+ import { attrs as attrsPlugin } from '@mdit/plugin-attrs'
11
+ import { container as containerPlugin } from '@mdit/plugin-container'
12
+ import {
13
+ markdownPagePathToRoutePath,
14
+ normalizeFrontmatterSource,
15
+ resolvePageMetadata,
16
+ stringifyJson,
17
+ useMarkdownItPlugin,
18
+ } from 'kawapress'
19
+ import { createMarkdownExit } from 'markdown-exit'
20
+ import anchorPlugin from 'markdown-it-anchor'
21
+ import { createSearchIndex } from './search'
22
+
23
+ export const SEARCH_INDEX_MODULE_ID = 'virtual:kawapress-search-index'
24
+ const RESOLVED_MODULE_ID = `\0${SEARCH_INDEX_MODULE_ID}`
25
+ const INDEX_MODULE_PREFIX = `${SEARCH_INDEX_MODULE_ID}/`
26
+ const RESOLVED_INDEX_MODULE_PREFIX = `\0${INDEX_MODULE_PREFIX}`
27
+ const IGNORED_DIRECTORIES = new Set(['dist', 'node_modules'])
28
+
29
+ export type SearchGitHubAlertType = 'note'
30
+ | 'tip'
31
+ | 'important'
32
+ | 'warning'
33
+ | 'caution'
34
+
35
+ export type SearchGitHubAlertLabels = Record<SearchGitHubAlertType, string>
36
+
37
+ export interface SearchCalloutOptions {
38
+ containers?: boolean
39
+ githubAlerts?: boolean | {
40
+ labels?: Partial<SearchGitHubAlertLabels>
41
+ localeLabels?: Record<string, Partial<SearchGitHubAlertLabels>>
42
+ }
43
+ }
44
+
45
+ interface SearchIndexPluginOptions {
46
+ srcDir: string
47
+ locales: string[]
48
+ localeLanguages?: Record<string, string | undefined>
49
+ callouts?: SearchCalloutOptions
50
+ }
51
+
52
+ interface SearchMarkdownEnv {
53
+ frontmatter?: Record<string, unknown>
54
+ references?: Record<string, { title: string, href: string }>
55
+ }
56
+
57
+ interface CreateSearchDocumentsOptions {
58
+ language?: string
59
+ localeIndex?: string
60
+ callouts?: SearchCalloutOptions
61
+ }
62
+
63
+ interface LoadSearchIndexesOptions {
64
+ localeLanguages?: Record<string, string | undefined>
65
+ callouts?: SearchCalloutOptions
66
+ }
67
+
68
+ interface MutableSearchSection {
69
+ id: string
70
+ title: string
71
+ titles: string[]
72
+ text: string[]
73
+ }
74
+
75
+ const SEARCH_CONTAINER_TYPES = [
76
+ 'info',
77
+ 'tip',
78
+ 'warning',
79
+ 'danger',
80
+ 'details',
81
+ ] as const
82
+
83
+ const DEFAULT_GITHUB_ALERT_LABELS = {
84
+ en: {
85
+ note: 'Note',
86
+ tip: 'Tip',
87
+ important: 'Important',
88
+ warning: 'Warning',
89
+ caution: 'Caution',
90
+ },
91
+ zh: {
92
+ note: '注意',
93
+ tip: '提示',
94
+ important: '重要',
95
+ warning: '警告',
96
+ caution: '小心',
97
+ },
98
+ } as const
99
+
100
+ const searchMarkdownCache = new Map<string, MarkdownExit>()
101
+
102
+ function getSearchMarkdown(callouts: SearchCalloutOptions = {}): MarkdownExit {
103
+ const key = `${Boolean(callouts.containers)}:${Boolean(callouts.githubAlerts)}`
104
+ const cached = searchMarkdownCache.get(key)
105
+ if (cached) {
106
+ return cached
107
+ }
108
+
109
+ const markdown = createMarkdownExit({ html: true })
110
+ useMarkdownItPlugin(markdown, frontmatterPlugin)
111
+ const parse = markdown.parse.bind(markdown)
112
+ markdown.parse = (source, env = {}) => parse(
113
+ normalizeFrontmatterSource(source),
114
+ env,
115
+ )
116
+ useMarkdownItPlugin(markdown, attrsPlugin, {
117
+ allowed: ['id'],
118
+ rule: ['heading'],
119
+ })
120
+ useMarkdownItPlugin(markdown, anchorPlugin, { level: [1, 2, 3, 4, 5, 6] })
121
+ if (callouts.githubAlerts) {
122
+ useMarkdownItPlugin(markdown, alertPlugin, {
123
+ alertNames: ['note', 'tip', 'important', 'warning', 'caution'],
124
+ deep: false,
125
+ })
126
+ }
127
+ if (callouts.containers) {
128
+ for (const type of SEARCH_CONTAINER_TYPES) {
129
+ const options: MarkdownItContainerOptions = {
130
+ name: type,
131
+ validate: params => params.trim().split(/\s+/, 1)[0] === type,
132
+ }
133
+ useMarkdownItPlugin(markdown, containerPlugin, options)
134
+ }
135
+ }
136
+
137
+ searchMarkdownCache.set(key, markdown)
138
+ return markdown
139
+ }
140
+
141
+ export function searchIndexPlugin(
142
+ options: SearchIndexPluginOptions,
143
+ ): Plugin {
144
+ const localeIndexes = [...new Set(['root', ...options.locales])]
145
+ let sourceRoot = resolve(options.srcDir)
146
+ let indexVersion = 0
147
+ let cachedIndexes: Promise<Record<string, string>> | undefined
148
+ const resolvedIndexModules = new Set<string>()
149
+
150
+ function getIndexes(): Promise<Record<string, string>> {
151
+ return cachedIndexes ??= loadSearchIndexes(
152
+ sourceRoot,
153
+ localeIndexes,
154
+ {
155
+ localeLanguages: options.localeLanguages,
156
+ callouts: options.callouts,
157
+ },
158
+ )
159
+ }
160
+
161
+ return {
162
+ name: 'kawapress:search-index',
163
+ configResolved(config) {
164
+ sourceRoot = resolve(config.root, options.srcDir)
165
+ },
166
+ resolveId(id) {
167
+ if (id === SEARCH_INDEX_MODULE_ID) {
168
+ return RESOLVED_MODULE_ID
169
+ }
170
+ if (id.startsWith(INDEX_MODULE_PREFIX)) {
171
+ const resolvedId = `\0${id}`
172
+ resolvedIndexModules.add(resolvedId)
173
+ return resolvedId
174
+ }
175
+ },
176
+ async load(id) {
177
+ if (id === RESOLVED_MODULE_ID) {
178
+ const loaders = localeIndexes.map((locale) => {
179
+ const indexId = `${INDEX_MODULE_PREFIX}${encodeURIComponent(locale)}?v=${indexVersion}`
180
+ return `${JSON.stringify(locale)}: () => import(${JSON.stringify(indexId)})`
181
+ })
182
+ return `export const searchIndexLoaders = {${loaders.join(',')}}`
183
+ }
184
+ if (!id.startsWith(RESOLVED_INDEX_MODULE_PREFIX)) {
185
+ return
186
+ }
187
+
188
+ const locale = decodeURIComponent(id
189
+ .slice(RESOLVED_INDEX_MODULE_PREFIX.length)
190
+ .replace(/\?.*$/, ''))
191
+ const indexes = await getIndexes()
192
+ const serializedIndex = indexes[locale] ?? JSON.stringify(createSearchIndex())
193
+ return `export default ${stringifyJson(serializedIndex, {
194
+ label: `search index for locale ${JSON.stringify(locale)}`,
195
+ path: 'searchIndex',
196
+ })}`
197
+ },
198
+ handleHotUpdate(context) {
199
+ if (!isMarkdownFile(context.file, sourceRoot)) {
200
+ return
201
+ }
202
+
203
+ cachedIndexes = undefined
204
+ indexVersion++
205
+ const modules = [
206
+ context.server.moduleGraph.getModuleById(RESOLVED_MODULE_ID),
207
+ ...[...resolvedIndexModules].map(id => (
208
+ context.server.moduleGraph.getModuleById(id)
209
+ )),
210
+ ].filter(module => module !== undefined)
211
+
212
+ for (const module of modules) {
213
+ context.server.moduleGraph.invalidateModule(module)
214
+ }
215
+ return [...new Set([...context.modules, ...modules])]
216
+ },
217
+ }
218
+ }
219
+
220
+ export async function loadSearchIndexes(
221
+ sourceRoot: string,
222
+ locales: string[],
223
+ options: LoadSearchIndexesOptions = {},
224
+ ): Promise<Record<string, string>> {
225
+ const localeIndexes = Object.fromEntries(locales.map(locale => (
226
+ [locale, createSearchIndex()]
227
+ )))
228
+ const files = await findMarkdownFiles(sourceRoot)
229
+
230
+ for (const file of files) {
231
+ const source = await readFile(file, 'utf8')
232
+ const route = markdownPagePathToRoutePath(relative(sourceRoot, file))
233
+ const locale = getLocaleIndex(route, locales)
234
+ const index = localeIndexes[locale]
235
+ if (index) {
236
+ index.addAll(createSearchDocuments(
237
+ source,
238
+ route,
239
+ {
240
+ language: options.localeLanguages?.[locale],
241
+ localeIndex: locale,
242
+ callouts: options.callouts,
243
+ },
244
+ ))
245
+ }
246
+ }
247
+
248
+ return Object.fromEntries(Object.entries(localeIndexes).map(([locale, index]) => (
249
+ [locale, JSON.stringify(index)]
250
+ )))
251
+ }
252
+
253
+ export function createSearchDocuments(
254
+ source: string,
255
+ route: string,
256
+ options: CreateSearchDocumentsOptions = {},
257
+ ): SearchDocument[] {
258
+ const env: SearchMarkdownEnv = {}
259
+ const markdown = getSearchMarkdown(options.callouts)
260
+ const tokens = markdown.parse(source, env)
261
+ const alertLabels = resolveGitHubAlertLabels(
262
+ options.callouts?.githubAlerts,
263
+ options.localeIndex ?? 'root',
264
+ options.language ?? 'en',
265
+ )
266
+ if (env.frontmatter?.search === false) {
267
+ return []
268
+ }
269
+
270
+ const pageTitle = resolvePageMetadata(
271
+ env.frontmatter ?? {},
272
+ getSearchPageHeaders(tokens),
273
+ ).title
274
+ const documents: SearchDocument[] = []
275
+ const headingTitles: string[] = []
276
+ let section: MutableSearchSection | undefined
277
+ let headingCount = 0
278
+
279
+ function ensureSection(): MutableSearchSection | undefined {
280
+ if (!section && pageTitle) {
281
+ section = {
282
+ id: route,
283
+ title: pageTitle,
284
+ titles: [],
285
+ text: [],
286
+ }
287
+ }
288
+ return section
289
+ }
290
+
291
+ function finishSection(): void {
292
+ if (!section) {
293
+ return
294
+ }
295
+ const text = normalizeText(section.text.join(' '))
296
+ documents.push({
297
+ id: section.id,
298
+ title: section.title,
299
+ titles: section.titles,
300
+ text,
301
+ })
302
+ section = undefined
303
+ }
304
+
305
+ for (let index = 0; index < tokens.length; index++) {
306
+ const token = tokens[index]
307
+ if (token.type === 'heading_open') {
308
+ finishSection()
309
+ const inline = tokens[index + 1]
310
+ const title = inline ? getInlineText(inline) : ''
311
+ const level = Number.parseInt(token.tag.slice(1), 10)
312
+ if (!title || !Number.isInteger(level)) {
313
+ continue
314
+ }
315
+
316
+ headingCount++
317
+ headingTitles.length = Math.max(0, level - 1)
318
+ const parentTitles = headingTitles.filter(Boolean)
319
+ if (parentTitles.length === 0 && level > 1 && pageTitle) {
320
+ parentTitles.push(pageTitle)
321
+ }
322
+ headingTitles[level - 1] = title
323
+ const slug = token.attrGet('id')
324
+ section = {
325
+ id: headingCount === 1 && level === 1
326
+ && !documents.some(document => document.id === route)
327
+ ? route
328
+ : slug ? `${route}#${slug}` : route,
329
+ title: headingCount === 1 && level === 1 && pageTitle
330
+ ? pageTitle
331
+ : title,
332
+ titles: parentTitles,
333
+ text: [],
334
+ }
335
+ index += 2
336
+ continue
337
+ }
338
+
339
+ const text = getTokenText(token, markdown, env, alertLabels)
340
+ if (text) {
341
+ ensureSection()?.text.push(text)
342
+ }
343
+ }
344
+
345
+ finishSection()
346
+ return documents
347
+ }
348
+
349
+ function getSearchPageHeaders(tokens: Token[]): Array<{
350
+ level: number
351
+ title: string
352
+ }> {
353
+ const headers: Array<{ level: number, title: string }> = []
354
+ for (let index = 0; index < tokens.length; index++) {
355
+ const token = tokens[index]
356
+ if (token?.type !== 'heading_open') {
357
+ continue
358
+ }
359
+ const level = Number.parseInt(token.tag.slice(1), 10)
360
+ const title = tokens[index + 1] ? getInlineText(tokens[index + 1]) : ''
361
+ if (title && Number.isInteger(level)) {
362
+ headers.push({ level, title })
363
+ }
364
+ }
365
+ return headers
366
+ }
367
+
368
+ function getInlineText(token: Token): string {
369
+ return normalizeText(getInlineRawText(token))
370
+ }
371
+
372
+ function getInlineRawText(token: Token): string {
373
+ return (token.children ?? [])
374
+ .filter(child => child.type === 'text'
375
+ || child.type === 'code_inline'
376
+ || child.type === 'softbreak'
377
+ || child.type === 'hardbreak')
378
+ .map(child => child.type === 'softbreak' || child.type === 'hardbreak'
379
+ ? '\n'
380
+ : child.content)
381
+ .join('')
382
+ }
383
+
384
+ function getTokenText(
385
+ token: Token,
386
+ markdown: MarkdownExit,
387
+ env: SearchMarkdownEnv,
388
+ alertLabels: SearchGitHubAlertLabels | undefined,
389
+ ): string {
390
+ if (token.type === 'alert_title') {
391
+ return alertLabels?.[token.markup.toLowerCase() as SearchGitHubAlertType] ?? ''
392
+ }
393
+ if (token.type.startsWith('container_') && token.type.endsWith('_open')) {
394
+ const type = token.type.slice('container_'.length, -'_open'.length)
395
+ const customTitle = token.info.trim().slice(type.length).trim()
396
+ return customTitle ? getInlineSourceText(customTitle, markdown, env) : ''
397
+ }
398
+ if (token.type === 'inline') {
399
+ return normalizeText(getInlineRawText(token))
400
+ }
401
+ if (token.type === 'fence' || token.type === 'code_block') {
402
+ return token.content
403
+ }
404
+ return ''
405
+ }
406
+
407
+ function getInlineSourceText(
408
+ source: string,
409
+ markdown: MarkdownExit,
410
+ env: SearchMarkdownEnv,
411
+ ): string {
412
+ const inline = markdown.parseInline(source, {
413
+ references: env.references,
414
+ })[0]
415
+ return inline ? getInlineText(inline) : ''
416
+ }
417
+
418
+ function resolveGitHubAlertLabels(
419
+ options: SearchCalloutOptions['githubAlerts'],
420
+ localeIndex: string,
421
+ language: string,
422
+ ): SearchGitHubAlertLabels | undefined {
423
+ if (!options) {
424
+ return undefined
425
+ }
426
+ const builtIn = language.toLowerCase().startsWith('zh')
427
+ ? DEFAULT_GITHUB_ALERT_LABELS.zh
428
+ : DEFAULT_GITHUB_ALERT_LABELS.en
429
+ if (options === true) {
430
+ return builtIn
431
+ }
432
+ return {
433
+ ...builtIn,
434
+ ...options.labels,
435
+ ...options.localeLabels?.[localeIndex],
436
+ }
437
+ }
438
+
439
+ function normalizeText(text: string): string {
440
+ return text.replace(/\s+/g, ' ').trim()
441
+ }
442
+
443
+ async function findMarkdownFiles(directory: string): Promise<string[]> {
444
+ const entries = await readdir(directory, { withFileTypes: true })
445
+ const files = await Promise.all(entries
446
+ .sort(compareEntries)
447
+ .filter(entry => !shouldIgnore(entry))
448
+ .map(async (entry) => {
449
+ const path = join(directory, entry.name)
450
+ if (entry.isDirectory()) {
451
+ return findMarkdownFiles(path)
452
+ }
453
+ return entry.isFile() && entry.name.endsWith('.md') ? [path] : []
454
+ }))
455
+ return files.flat()
456
+ }
457
+
458
+ function getLocaleIndex(route: string, locales: string[]): string {
459
+ const locale = locales
460
+ .filter(item => item !== 'root')
461
+ .sort((left, right) => right.length - left.length)
462
+ .find((item) => {
463
+ const prefix = `/${item}`
464
+ return route === prefix || route.startsWith(`${prefix}/`)
465
+ })
466
+ return locale ?? 'root'
467
+ }
468
+
469
+ function isMarkdownFile(file: string, sourceRoot: string): boolean {
470
+ const relativePath = relative(sourceRoot, file)
471
+ return file.endsWith('.md')
472
+ && !isAbsolute(relativePath)
473
+ && relativePath !== '..'
474
+ && !relativePath.startsWith(`..${sep}`)
475
+ }
476
+
477
+ function compareEntries(a: Dirent, b: Dirent): number {
478
+ return a.name.localeCompare(b.name)
479
+ }
480
+
481
+ function shouldIgnore(entry: Dirent): boolean {
482
+ return entry.name.startsWith('.')
483
+ || (entry.isDirectory() && IGNORED_DIRECTORIES.has(entry.name))
484
+ }