Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions app/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import type { NavigationItem } from 'comark-content'
import type { SearchSection } from './utils/search-sections'

const { seo, docs } = useAppConfig()

Expand All @@ -9,25 +8,8 @@ const content = useDocsContent()
const { data: navigation } = await useAsyncData('navigation', () => content.value.client.navigation(), {
watch: [() => content.value.base],
})
const {
data: files,
status,
execute: loadSearchSections,
} = useLazyAsyncData('search-sections', () => content.value.client.searchSections(), {
server: false,
watch: [() => content.value.base],
immediate: false,
})

onNuxtReady(() => loadSearchSections())

const navTree = computed<NavigationItem[]>(() => prefixNavigation(navigation.value ?? [], content.value.base))
const searchFiles = computed<SearchSection[]>(() =>
(files.value ?? []).map((section) => {
const [path, hash] = section.id.split('#')
return { ...section, id: prefixLink(path!, content.value.base) + (hash ? `#${hash}` : '') }
})
)

useHead({
meta: [{ name: 'viewport', content: 'width=device-width, initial-scale=1' }],
Expand Down Expand Up @@ -89,14 +71,9 @@ defineShortcuts({

<AppFooter />

<AppSearch :navigation="navTree" />

<ClientOnly>
<LazyUContentSearch
:files="searchFiles"
:navigation="navTree"
:transition="false"
:loading="status !== 'success'"
:placeholder="status !== 'success' ? 'Loading...' : undefined"
/>
<LazyVersionHistory />
<LazyAssistantChat v-if="assistant?.enabled && assistantMounted" />
</ClientOnly>
Expand Down
67 changes: 67 additions & 0 deletions app/components/AppSearch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<script setup lang="ts">
import type { NavigationItem } from 'comark-content'

const props = defineProps<{
navigation: NavigationItem[]
}>()

const { search, status } = useSearch()

const appConfig = useAppConfig()

interface PageItem {
label: string
prefix?: string
suffix?: string
to: string
icon: string
}

/** Leaf pages, flattened; ancestor titles become the `Section > Page` prefix the palette renders. */
function pageItems(items: NavigationItem[], ancestors: string[] = []): PageItem[] {
return items.flatMap((item) => {
if (item.children?.length) return pageItems(item.children, [...ancestors, item.title])
if (!item.path || item.page === false) return []
return [{
label: item.title,
prefix: ancestors.length ? `${ancestors.join(' > ')} >` : undefined,
suffix: item.description,
to: item.path,
icon: (item.icon as string | undefined) || appConfig.ui.icons.file,
}]
})
}

function browseOnly(query: string, items?: PageItem[]): PageItem[] {
return query ? [] : (items ?? [])
}

// One group per top-level section, mirroring how `UContentSearch` groups navigation when it can.
const groups = computed(() => {
if (props.navigation.some((item) => item.children?.length)) {
return props.navigation
.filter((section) => section.children?.length)
.map((section) => ({
id: section.path,
label: section.title,
items: pageItems(section.children ?? []),
postFilter: browseOnly,
}))
.filter((group) => group.items.length > 0)
}
return [{ id: 'docs', items: pageItems(props.navigation), postFilter: browseOnly }]
})
</script>

<template>
<ClientOnly>
<LazyUContentSearch
:search="search"
:search-status="status"
:navigation="navigation"
:groups="groups"
:transition="false"
:loading="status === 'loading'"
/>
</ClientOnly>
</template>
3 changes: 0 additions & 3 deletions app/composables/useDocsContent.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { createContentClient } from 'comark-content/client'
import { searchSectionsClient } from '../utils/search-sections'
import type { ContentMode } from '../types/content'
import { withLeadingSlash } from 'ufo'

export const prodContent = createContentClient({
basePath: '/api/content',
fetch: $fetch,
plugins: [searchSectionsClient()],
})

const clients = new Map<string, typeof prodContent>()
Expand All @@ -17,7 +15,6 @@ function getClient(basePath: string) {
client = createContentClient({
basePath,
fetch: $fetch,
plugins: [searchSectionsClient()],
})
clients.set(basePath, client)
}
Expand Down
61 changes: 61 additions & 0 deletions app/composables/useSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { SearchOptions, SearchResult } from 'comark-content'

type SearchStatus = 'idle' | 'loading' | 'ready' | 'error'

const status = ref<SearchStatus>('idle')

/**
* Hydration logging switch: `?debug=search`
*/
function searchDebug(): boolean {
if (!import.meta.client) return false
return new URLSearchParams(location.search).get('debug') === 'search'
}

/**
* Client-side full-text search over production content (sqlite-wasm FTS5) hydrated from the
* per-commit snapshot artifacts.
*/
export function useSearch() {
const { data: headSha } = useAsyncData(
'content-head-sha',
() => $fetch<{ sha: string | null }>('/api/content/head').then(({ sha }) => sha),
{ default: () => null }
)

/**
* Load the database.
* No-op once loading or ready; retries after a failure.
*/
async function warmup(): Promise<void> {
if (status.value === 'loading' || status.value === 'ready') return
status.value = 'loading'
try {
if (!headSha.value && !import.meta.dev) {
throw new Error('[search] /api/content/head returned no commit pin')
}

// Immutable per-commit artifacts, CDN-cached forever. Only unpinned in dev, per the guard above.
const apiBase = headSha.value ? `/api/content/blob/${headSha.value}` : '/api/content'

const debug = searchDebug()
if (debug) console.info(`[search] warmup from ${apiBase} (head ${headSha.value ?? 'unpinned'})`)

await warmupSearch(apiBase, location.origin, debug)
status.value = 'ready'
} catch (error) {
status.value = 'error'
console.error('[search] could not load the search database', error)
}
}

if (import.meta.client) {
onNuxtReady(warmup)
}

async function search(query: string, opts?: SearchOptions): Promise<SearchResult[]> {
return searchContent(query, opts)
}

return { search, status: readonly(status), warmup }
}
10 changes: 1 addition & 9 deletions app/error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ useSeoMeta({
})

const { data: navigation } = await useAsyncData('navigation', () => prodContent.navigation())
const { data: files } = useLazyAsyncData('search-sections', () => prodContent.searchSections(), {
server: false,
})

provide('navigation', navigation)
</script>
Expand All @@ -32,11 +29,6 @@ provide('navigation', navigation)

<AppFooter />

<ClientOnly>
<LazyUContentSearch
:files="files ?? []"
:navigation="navigation ?? []"
/>
</ClientOnly>
<AppSearch :navigation="navigation ?? []" />
Comment thread
atinux marked this conversation as resolved.
</UApp>
</template>
23 changes: 0 additions & 23 deletions app/utils/search-sections.ts

This file was deleted.

69 changes: 69 additions & 0 deletions app/workers/internal/search-logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Logging for the search worker.
*
* Triggered by `?debug=search` param.
*/
import type { ContentFile, Logger, RelationalDatabase } from 'comark-content'

const PREFIX = '[search:worker]'

let debug = false

/** Called on every `warmup`; once on, it stays on for the life of the worker. */
export function setDebug(value: boolean): void {
debug = debug || value
}

export function isDebug(): boolean {
return debug
}

export function log(...args: unknown[]): void {
if (debug) console.info(PREFIX, ...args)
}

/** Milliseconds since `from`, for log lines. */
export function since(from: number): string {
return `${(performance.now() - from).toFixed(1)}ms`
}

/**
* Warn and error are deliberately ungated: the FTS plugin reports a missing snapshot through this
* channel, and that failure is otherwise indistinguishable from "the query matched nothing".
*/
export const logger: Logger = {
debug: (tag, ...args) => log(`${tag}:`, ...args),
info: (tag, ...args) => log(`${tag}:`, ...args),
warn: (tag, ...args) => console.warn(`${PREFIX} ${tag}:`, ...args),
error: (tag, ...args) => console.error(`${PREFIX} ${tag}:`, ...args),
}

/**
* What a decoded artifact holds: a snapshot decodes to the source's items, the manifest to an object
* keyed by path. `with nodes` is the number that matters — the FTS plugin indexes
* `kind === 'document' && nodes?.length`, so a bodies-less (partial) snapshot builds an empty index.
*/
export function describeArtifact(decoded: unknown): string {
if (Array.isArray(decoded)) {
const items = decoded as ContentFile[]
const documents = items.filter((item) => item.meta.kind === 'document')
const withNodes = documents.filter((item) => item.nodes?.length)
return `${items.length} item(s), ${documents.length} document(s), ${withNodes.length} with nodes`
}
const items = (decoded as { items?: Record<string, unknown> } | null)?.items
return `${items ? Object.keys(items).length : 0} manifest item(s)`
}

/**
* Rows in the FTS plugin's index — the one number that separates "nothing was indexed" from "the
* query found nothing", since `search()` catches SQL errors and returns `[]` either way. Reads the
* plugin's private table, so it is a diagnostic, not something to build on.
*/
export async function indexedRows(database: RelationalDatabase, source: string): Promise<number | string> {
try {
const rows = await database.all<{ n: number }>('SELECT count(*) as n FROM __fts_search WHERE source = ?', [source])
return rows?.[0]?.n ?? 'unknown'
} catch (error) {
return `unknown (${error instanceof Error ? error.message : String(error)})`
}
}
Loading
Loading