Skip to content

Commit fd14e32

Browse files
authored
feat(LLMS): call hook before generating markdown (#3323)
1 parent 9419ccc commit fd14e32

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/features/llms/module.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineNuxtModule({
1515
filename: 'content/llms.d.ts' as `${string}.d.ts`,
1616
getContents: () => {
1717
return `
18-
import type { SQLOperator, PageCollections } from '@nuxt/content'
18+
import type { SQLOperator, PageCollections, PageCollectionItemBase } from '@nuxt/content'
1919
declare module 'nuxt-llms' {
2020
interface LLMsSection {
2121
contentCollection?: keyof PageCollections
@@ -26,6 +26,13 @@ declare module 'nuxt-llms' {
2626
}>
2727
}
2828
}
29+
30+
declare module 'nitropack/types' {
31+
interface NitroRuntimeHooks {
32+
'content:llms:generate:document': (event: H3Event, doc: PageCollectionItemBase, options: ModuleOptions) => void
33+
}
34+
}
35+
2936
`
3037
},
3138
write: true,

src/features/llms/runtime/server/content-llms.plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export default defineNitroPlugin((nitroApp: NitroApp) => {
7171
const docs = await query.all()
7272

7373
for (const doc of docs) {
74+
// @ts-expect-error -- TODO: fix types
75+
await nitroApp.hooks.callHook('content:llms:generate:document', event, doc, options)
7476
const markdown = await generateDocument(doc, options)
7577
contents.push(markdown)
7678
}

src/runtime/internal/abstract-tree.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,23 @@ function compressNode(input: MDCElement | MDCText | MDCComment): MinimalNode | u
5151
...input.children.map(compressNode).filter(v => v !== undefined),
5252
]
5353
}
54+
55+
export function visit(tree: MinimalTree, checker: (node: MinimalNode) => boolean, visitor: (node: MinimalNode) => MinimalNode | undefined) {
56+
function walk(node: MinimalNode, parent: MinimalElement | MinimalNode[], index: number) {
57+
if (checker(node)) {
58+
const res = visitor(node)
59+
if (res !== undefined) {
60+
parent[index] = res
61+
}
62+
}
63+
if (Array.isArray(node) && node.length > 2) {
64+
for (let i = 2; i < node.length; i++) {
65+
walk(node[i] as MinimalNode, node, i)
66+
}
67+
}
68+
}
69+
70+
tree.value.forEach((node, i) => {
71+
walk(node, tree.value, i)
72+
})
73+
}

src/runtime/internal/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface SectionablePage {
2424
path: string
2525
title: string
2626
description: string
27-
body: MDCRoot
27+
body: MDCRoot | MinimalTree
2828
}
2929

3030
export async function generateSearchSections<T extends PageCollectionItemBase>(queryBuilder: CollectionQueryBuilder<T>, opts?: { ignoredTags?: string[], extraFields?: Array<keyof T> }) {

src/types/collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ZodObject, ZodRawShape } from 'zod'
22
import type { zodToJsonSchema } from 'zod-to-json-schema'
3-
import type { MarkdownRoot } from './content'
3+
import type { MinimalTree } from './tree'
44

55
export interface PageCollections {}
66
export interface Collections {}
@@ -103,7 +103,7 @@ export interface PageCollectionItemBase extends CollectionItemBase {
103103

104104
[key: string]: unknown
105105
}
106-
body: MarkdownRoot
106+
body: MinimalTree
107107
navigation?: boolean | {
108108
title: string
109109
description: string

0 commit comments

Comments
 (0)