Skip to content

Commit 89c0b25

Browse files
authored
fix(llms): avoid import(variableName) pattern (#3733)
1 parent f406942 commit 89c0b25

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default defineNitroPlugin((nitroApp: NitroApp) => {
7777
// @ts-expect-error -- TODO: fix types
7878
await nitroApp.hooks.callHook('content:llms:generate:document', event, doc, options)
7979
const markdown = await generateDocument(doc, options)
80-
contents.push(markdown)
80+
contents.push(markdown!)
8181
}
8282
}
8383
})

src/features/llms/runtime/server/utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MDCElement } from '@nuxtjs/mdc'
1+
import type { MDCElement, MDCRoot } from '@nuxtjs/mdc'
22
import { withBase } from 'ufo'
33
import { pascalCase } from 'scule'
44
import type { LLMsSection } from 'nuxt-llms'
@@ -19,17 +19,15 @@ export interface ContentLLMSCollectionSection extends LLMsSection {
1919

2020
const linkProps = ['href', 'src', 'to']
2121

22-
const importExternalPackage = async (name: string) => await import(name)
23-
2422
export async function createDocumentGenerator() {
25-
const visit = await importExternalPackage('unist-util-visit').then(res => res.visit)
26-
const stringifyMarkdown = await importExternalPackage('@nuxtjs/mdc/runtime').then(res => res.stringifyMarkdown)
23+
const { visit } = await import('unist-util-visit')
24+
const { stringifyMarkdown } = await import('@nuxtjs/mdc/runtime')
2725

2826
return generateDocument
2927

3028
async function generateDocument(doc: PageCollectionItemBase, options: { domain: string }) {
3129
const hastTree = refineDocumentBody(doc.body as unknown as MinimarkTree, options)
32-
let markdown = await stringifyMarkdown(hastTree, {})
30+
let markdown = await stringifyMarkdown(hastTree as unknown as MDCRoot, {})
3331

3432
if (!markdown?.trim().startsWith('# ')) {
3533
const title = doc.title || doc.seo?.title || ''
@@ -43,10 +41,14 @@ export async function createDocumentGenerator() {
4341
function refineDocumentBody(body: MinimarkTree, options: { domain: string }) {
4442
const hastTree = toHast(body)
4543

46-
visit(hastTree, (node: MDCElement) => !!node.props?.to || !!node.props?.href || !!node.props?.src, (node: MDCElement) => {
44+
visit(hastTree, (node) => {
45+
const el = node as unknown as MDCElement
46+
if (!el.props?.to && !el.props?.href && !el.props?.src) {
47+
return
48+
}
4749
for (const prop of linkProps) {
48-
if (node.props?.[prop]) {
49-
node.props[prop] = withBase(node.props[prop], options.domain)
50+
if (el.props?.[prop]) {
51+
el.props[prop] = withBase(el.props[prop], options.domain)
5052
}
5153
}
5254
})

0 commit comments

Comments
 (0)