@kawapress/plugin-search 0.3.0 → 0.4.0
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/package.json +5 -5
- package/src/components/Search.vue +2 -9
- package/src/plugin.ts +2 -0
- package/src/search-index-plugin.ts +41 -48
- package/src/platform-detection.js +0 -8
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kawapress/plugin-search",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"description": "Lazy local full-text search plugin for KawaPress",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://kawapress.dev/",
|
|
@@ -44,22 +44,22 @@
|
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"vue": "^3.5.41",
|
|
47
|
-
"kawapress": "^0.
|
|
47
|
+
"kawapress": "^0.4.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@mdit-vue/plugin-frontmatter": "^3.0.2",
|
|
51
51
|
"@mdit/plugin-alert": "1.0.3",
|
|
52
52
|
"@mdit/plugin-attrs": "1.1.1",
|
|
53
53
|
"@mdit/plugin-container": "^1.0.2",
|
|
54
|
-
"@unhead/vue": "^3.3.2",
|
|
55
54
|
"markdown-exit": "^1.3.0",
|
|
56
55
|
"markdown-it-anchor": "^9.2.0",
|
|
57
|
-
"minisearch": "^7.2.0"
|
|
56
|
+
"minisearch": "^7.2.0",
|
|
57
|
+
"@kawapress/plugin-toc": "^0.4.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"typescript": "^6.0.3",
|
|
61
61
|
"vite": "^8.2.2",
|
|
62
62
|
"vue": "^3.5.41",
|
|
63
|
-
"kawapress": "0.
|
|
63
|
+
"kawapress": "0.4.0"
|
|
64
64
|
}
|
|
65
65
|
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
import type { SearchTranslations } from '../translations'
|
|
3
|
-
import {
|
|
4
|
-
import { useSite } from 'kawapress/client'
|
|
3
|
+
import { usePlatformAttribute, useSite } from 'kawapress/client'
|
|
5
4
|
import { computed, nextTick, onBeforeUnmount, onMounted, shallowRef, useTemplateRef } from 'vue'
|
|
6
|
-
import platformDetectionScript from '../platform-detection.js?raw'
|
|
7
5
|
import { resolveSearchTranslations } from '../translations'
|
|
8
6
|
import SearchOverlay from './SearchOverlay.vue'
|
|
9
7
|
|
|
@@ -23,12 +21,7 @@ const resolvedTranslations = computed(() => resolveSearchTranslations(
|
|
|
23
21
|
const open = shallowRef(false)
|
|
24
22
|
const trigger = useTemplateRef<HTMLButtonElement>('trigger')
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
script: [{
|
|
28
|
-
id: 'kawa-search-platform',
|
|
29
|
-
textContent: platformDetectionScript,
|
|
30
|
-
}],
|
|
31
|
-
})
|
|
24
|
+
usePlatformAttribute()
|
|
32
25
|
|
|
33
26
|
onMounted(() => {
|
|
34
27
|
window.addEventListener('keydown', handleGlobalKeydown)
|
package/src/plugin.ts
CHANGED
|
@@ -18,6 +18,8 @@ export function searchPlugin(options: SearchPluginOptions = {}): KawaPressPlugin
|
|
|
18
18
|
config.plugins ??= []
|
|
19
19
|
config.plugins.push(searchIndexPlugin({
|
|
20
20
|
srcDir: siteConfig?.srcDir ?? '.',
|
|
21
|
+
srcExclude: siteConfig?.srcExclude,
|
|
22
|
+
rewrites: siteConfig?.rewrites,
|
|
21
23
|
locales: Object.keys(siteConfig?.locales ?? { root: {} }),
|
|
22
24
|
localeLanguages: Object.fromEntries(
|
|
23
25
|
Object.entries(siteConfig?.locales ?? {}).map(([locale, config]) => (
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import type { MarkdownItContainerOptions } from '@mdit/plugin-container'
|
|
2
|
-
import type { PageFrontmatter } from 'kawapress'
|
|
2
|
+
import type { PageFrontmatter, PageRewrites } from 'kawapress'
|
|
3
|
+
import type { MarkdownPageScope } from 'kawapress/generator'
|
|
3
4
|
import type { MarkdownExit, Token } from 'markdown-exit'
|
|
4
|
-
import type { Dirent } from 'node:fs'
|
|
5
5
|
import type { Plugin } from 'vite'
|
|
6
6
|
import type { SearchDocument } from './search'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { resolve } from 'node:path'
|
|
8
|
+
import { installToc } from '@kawapress/plugin-toc'
|
|
9
9
|
import { frontmatterPlugin } from '@mdit-vue/plugin-frontmatter'
|
|
10
10
|
import { alert as alertPlugin } from '@mdit/plugin-alert'
|
|
11
11
|
import { attrs as attrsPlugin } from '@mdit/plugin-attrs'
|
|
12
12
|
import { container as containerPlugin } from '@mdit/plugin-container'
|
|
13
13
|
import {
|
|
14
|
-
markdownPagePathToRoutePath,
|
|
15
14
|
normalizeFrontmatterSource,
|
|
16
15
|
resolveHeadingTitle,
|
|
17
16
|
resolveLocaleIndex,
|
|
@@ -19,6 +18,7 @@ import {
|
|
|
19
18
|
stringifyJson,
|
|
20
19
|
useMarkdownItPlugin,
|
|
21
20
|
} from 'kawapress'
|
|
21
|
+
import { isMarkdownPage, listMarkdownPages, readMarkdownSource } from 'kawapress/generator'
|
|
22
22
|
import { createMarkdownExit } from 'markdown-exit'
|
|
23
23
|
import anchorPlugin from 'markdown-it-anchor'
|
|
24
24
|
import { createSearchIndex } from './search'
|
|
@@ -27,7 +27,6 @@ export const SEARCH_INDEX_MODULE_ID = 'virtual:kawapress-search-index'
|
|
|
27
27
|
const RESOLVED_MODULE_ID = `\0${SEARCH_INDEX_MODULE_ID}`
|
|
28
28
|
const INDEX_MODULE_PREFIX = `${SEARCH_INDEX_MODULE_ID}/`
|
|
29
29
|
const RESOLVED_INDEX_MODULE_PREFIX = `\0${INDEX_MODULE_PREFIX}`
|
|
30
|
-
const IGNORED_DIRECTORIES = new Set(['dist', 'node_modules'])
|
|
31
30
|
|
|
32
31
|
export type SearchGitHubAlertType = 'note'
|
|
33
32
|
| 'tip'
|
|
@@ -43,10 +42,16 @@ export interface SearchCalloutOptions {
|
|
|
43
42
|
labels?: Partial<SearchGitHubAlertLabels>
|
|
44
43
|
localeLabels?: Record<string, Partial<SearchGitHubAlertLabels>>
|
|
45
44
|
}
|
|
45
|
+
/** Reads `[[toc]]` lines as a table of contents, which adds no text. */
|
|
46
|
+
toc?: boolean
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
interface SearchIndexPluginOptions {
|
|
49
50
|
srcDir: string
|
|
51
|
+
/** `srcExclude` of the site config: Markdown files that are not pages. */
|
|
52
|
+
srcExclude?: readonly string[]
|
|
53
|
+
/** `rewrites` of the site config, which moves pages to other routes. */
|
|
54
|
+
rewrites?: PageRewrites | undefined
|
|
50
55
|
locales: string[]
|
|
51
56
|
localeLanguages?: Record<string, string | undefined>
|
|
52
57
|
callouts?: SearchCalloutOptions
|
|
@@ -66,6 +71,8 @@ interface CreateSearchDocumentsOptions {
|
|
|
66
71
|
interface LoadSearchIndexesOptions {
|
|
67
72
|
localeLanguages?: Record<string, string | undefined>
|
|
68
73
|
callouts?: SearchCalloutOptions
|
|
74
|
+
/** Receives every file the pages read through includes and snippets. */
|
|
75
|
+
dependencies?: Set<string>
|
|
69
76
|
}
|
|
70
77
|
|
|
71
78
|
interface MutableSearchSection {
|
|
@@ -103,7 +110,7 @@ const DEFAULT_GITHUB_ALERT_LABELS = {
|
|
|
103
110
|
const searchMarkdownCache = new Map<string, MarkdownExit>()
|
|
104
111
|
|
|
105
112
|
function getSearchMarkdown(callouts: SearchCalloutOptions = {}): MarkdownExit {
|
|
106
|
-
const key =
|
|
113
|
+
const key = [callouts.containers, callouts.githubAlerts, callouts.toc].map(Boolean).join(':')
|
|
107
114
|
const cached = searchMarkdownCache.get(key)
|
|
108
115
|
if (cached) {
|
|
109
116
|
return cached
|
|
@@ -137,6 +144,10 @@ function getSearchMarkdown(callouts: SearchCalloutOptions = {}): MarkdownExit {
|
|
|
137
144
|
}
|
|
138
145
|
}
|
|
139
146
|
|
|
147
|
+
if (callouts.toc) {
|
|
148
|
+
installToc(markdown)
|
|
149
|
+
}
|
|
150
|
+
|
|
140
151
|
searchMarkdownCache.set(key, markdown)
|
|
141
152
|
return markdown
|
|
142
153
|
}
|
|
@@ -145,18 +156,25 @@ export function searchIndexPlugin(
|
|
|
145
156
|
options: SearchIndexPluginOptions,
|
|
146
157
|
): Plugin {
|
|
147
158
|
const localeIndexes = [...new Set(['root', ...options.locales])]
|
|
148
|
-
let
|
|
159
|
+
let scope: MarkdownPageScope = {
|
|
160
|
+
sourceRoot: resolve(options.srcDir),
|
|
161
|
+
exclude: options.srcExclude,
|
|
162
|
+
rewrites: options.rewrites,
|
|
163
|
+
}
|
|
149
164
|
let indexVersion = 0
|
|
150
165
|
let cachedIndexes: Promise<Record<string, string>> | undefined
|
|
151
166
|
const resolvedIndexModules = new Set<string>()
|
|
167
|
+
/** Files that pages read through includes and snippets. */
|
|
168
|
+
const dependencies = new Set<string>()
|
|
152
169
|
|
|
153
170
|
function getIndexes(): Promise<Record<string, string>> {
|
|
154
171
|
return cachedIndexes ??= loadSearchIndexes(
|
|
155
|
-
|
|
172
|
+
scope,
|
|
156
173
|
localeIndexes,
|
|
157
174
|
{
|
|
158
175
|
localeLanguages: options.localeLanguages,
|
|
159
176
|
callouts: options.callouts,
|
|
177
|
+
dependencies,
|
|
160
178
|
},
|
|
161
179
|
)
|
|
162
180
|
}
|
|
@@ -164,7 +182,12 @@ export function searchIndexPlugin(
|
|
|
164
182
|
return {
|
|
165
183
|
name: 'kawapress:search-index',
|
|
166
184
|
configResolved(config) {
|
|
167
|
-
|
|
185
|
+
scope = {
|
|
186
|
+
sourceRoot: resolve(config.root, options.srcDir),
|
|
187
|
+
publicDir: config.publicDir ? resolve(config.publicDir) : undefined,
|
|
188
|
+
exclude: options.srcExclude,
|
|
189
|
+
rewrites: options.rewrites,
|
|
190
|
+
}
|
|
168
191
|
},
|
|
169
192
|
resolveId(id) {
|
|
170
193
|
if (id === SEARCH_INDEX_MODULE_ID) {
|
|
@@ -199,7 +222,7 @@ export function searchIndexPlugin(
|
|
|
199
222
|
})}`
|
|
200
223
|
},
|
|
201
224
|
handleHotUpdate(context) {
|
|
202
|
-
if (!
|
|
225
|
+
if (!isMarkdownPage(context.file, scope) && !dependencies.has(context.file)) {
|
|
203
226
|
return
|
|
204
227
|
}
|
|
205
228
|
|
|
@@ -221,18 +244,20 @@ export function searchIndexPlugin(
|
|
|
221
244
|
}
|
|
222
245
|
|
|
223
246
|
export async function loadSearchIndexes(
|
|
224
|
-
|
|
247
|
+
scope: MarkdownPageScope,
|
|
225
248
|
locales: string[],
|
|
226
249
|
options: LoadSearchIndexesOptions = {},
|
|
227
250
|
): Promise<Record<string, string>> {
|
|
228
251
|
const localeIndexes = Object.fromEntries(locales.map(locale => (
|
|
229
252
|
[locale, createSearchIndex()]
|
|
230
253
|
)))
|
|
231
|
-
const
|
|
254
|
+
const pages = await listMarkdownPages(scope)
|
|
232
255
|
|
|
233
|
-
for (const file of
|
|
234
|
-
const source =
|
|
235
|
-
const
|
|
256
|
+
for (const { file, routePath: route } of pages) {
|
|
257
|
+
const { source, dependencies } = readMarkdownSource({ file, sourceRoot: scope.sourceRoot })
|
|
258
|
+
for (const dependency of dependencies) {
|
|
259
|
+
options.dependencies?.add(dependency)
|
|
260
|
+
}
|
|
236
261
|
const locale = resolveLocaleIndex(route, locales)
|
|
237
262
|
const index = localeIndexes[locale]
|
|
238
263
|
if (index) {
|
|
@@ -438,35 +463,3 @@ function resolveGitHubAlertLabels(
|
|
|
438
463
|
function normalizeText(text: string): string {
|
|
439
464
|
return text.replace(/\s+/g, ' ').trim()
|
|
440
465
|
}
|
|
441
|
-
|
|
442
|
-
async function findMarkdownFiles(directory: string): Promise<string[]> {
|
|
443
|
-
const entries = await readdir(directory, { withFileTypes: true })
|
|
444
|
-
const files = await Promise.all(entries
|
|
445
|
-
.sort(compareEntries)
|
|
446
|
-
.filter(entry => !shouldIgnore(entry))
|
|
447
|
-
.map(async (entry) => {
|
|
448
|
-
const path = join(directory, entry.name)
|
|
449
|
-
if (entry.isDirectory()) {
|
|
450
|
-
return findMarkdownFiles(path)
|
|
451
|
-
}
|
|
452
|
-
return entry.isFile() && entry.name.endsWith('.md') ? [path] : []
|
|
453
|
-
}))
|
|
454
|
-
return files.flat()
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function isMarkdownFile(file: string, sourceRoot: string): boolean {
|
|
458
|
-
const relativePath = relative(sourceRoot, file)
|
|
459
|
-
return file.endsWith('.md')
|
|
460
|
-
&& !isAbsolute(relativePath)
|
|
461
|
-
&& relativePath !== '..'
|
|
462
|
-
&& !relativePath.startsWith(`..${sep}`)
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
function compareEntries(a: Dirent, b: Dirent): number {
|
|
466
|
-
return a.name.localeCompare(b.name)
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
function shouldIgnore(entry: Dirent): boolean {
|
|
470
|
-
return entry.name.startsWith('.')
|
|
471
|
-
|| (entry.isDirectory() && IGNORED_DIRECTORIES.has(entry.name))
|
|
472
|
-
}
|