Skip to content

feat(LLMS): call hook before generating markdown #3323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2025
Merged
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
9 changes: 8 additions & 1 deletion src/features/llms/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineNuxtModule({
filename: 'content/llms.d.ts' as `${string}.d.ts`,
getContents: () => {
return `
import type { SQLOperator, PageCollections } from '@nuxt/content'
import type { SQLOperator, PageCollections, PageCollectionItemBase } from '@nuxt/content'
declare module 'nuxt-llms' {
interface LLMsSection {
contentCollection?: keyof PageCollections
Expand All @@ -26,6 +26,13 @@ declare module 'nuxt-llms' {
}>
}
}

declare module 'nitropack/types' {
interface NitroRuntimeHooks {
'content:llms:generate:document': (event: H3Event, doc: PageCollectionItemBase, options: ModuleOptions) => void
}
}

`
},
write: true,
Expand Down
2 changes: 2 additions & 0 deletions src/features/llms/runtime/server/content-llms.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default defineNitroPlugin((nitroApp: NitroApp) => {
const docs = await query.all()

for (const doc of docs) {
// @ts-expect-error -- TODO: fix types
await nitroApp.hooks.callHook('content:llms:generate:document', event, doc, options)
const markdown = await generateDocument(doc, options)
contents.push(markdown)
}
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/internal/abstract-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ function compressNode(input: MDCElement | MDCText | MDCComment): MinimalNode | u
...input.children.map(compressNode).filter(v => v !== undefined),
]
}

export function visit(tree: MinimalTree, checker: (node: MinimalNode) => boolean, visitor: (node: MinimalNode) => MinimalNode | undefined) {
function walk(node: MinimalNode, parent: MinimalElement | MinimalNode[], index: number) {
if (checker(node)) {
const res = visitor(node)
if (res !== undefined) {
parent[index] = res
}
}
if (Array.isArray(node) && node.length > 2) {
for (let i = 2; i < node.length; i++) {
walk(node[i] as MinimalNode, node, i)
}
}
}

tree.value.forEach((node, i) => {
walk(node, tree.value, i)
})
}
2 changes: 1 addition & 1 deletion src/runtime/internal/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface SectionablePage {
path: string
title: string
description: string
body: MDCRoot
body: MDCRoot | MinimalTree
}

export async function generateSearchSections<T extends PageCollectionItemBase>(queryBuilder: CollectionQueryBuilder<T>, opts?: { ignoredTags?: string[], extraFields?: Array<keyof T> }) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/collection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ZodObject, ZodRawShape } from 'zod'
import type { zodToJsonSchema } from 'zod-to-json-schema'
import type { MarkdownRoot } from './content'
import type { MinimalTree } from './tree'

export interface PageCollections {}
export interface Collections {}
Expand Down Expand Up @@ -103,7 +103,7 @@ export interface PageCollectionItemBase extends CollectionItemBase {

[key: string]: unknown
}
body: MarkdownRoot
body: MinimalTree
navigation?: boolean | {
title: string
description: string
Expand Down