|
| 1 | +import { appendHeader } from 'h3' |
1 | 2 | import { withBase } from 'ufo' |
2 | 3 | import type { NitroApp } from 'nitropack/types' |
3 | 4 | import type { ContentLLMSCollectionSection } from './utils' |
4 | 5 | import { createDocumentGenerator, prepareContentSections } from './utils' |
5 | 6 | import type { PageCollectionItemBase } from '@nuxt/content' |
6 | 7 | // @ts-expect-error - typecheck does not detect defineNitroPlugin in imports |
7 | 8 | import { defineNitroPlugin, queryCollection } from '#imports' |
| 9 | +import type { ModuleOptions } from 'nuxt-llms' |
8 | 10 |
|
9 | 11 | export default defineNitroPlugin((nitroApp: NitroApp) => { |
| 12 | + const prerenderPaths = new Set<string>() |
10 | 13 | nitroApp.hooks.hook('llms:generate', async (event, options) => { |
11 | 14 | prepareContentSections(options.sections) |
12 | 15 |
|
@@ -38,7 +41,7 @@ export default defineNitroPlugin((nitroApp: NitroApp) => { |
38 | 41 | return { |
39 | 42 | title: doc.title || doc?.seo?.title || '', |
40 | 43 | description: doc.description || doc?.seo?.description || '', |
41 | | - href: withBase(doc.path, options.domain), |
| 44 | + href: getDocumentLink(doc.path, section.contentCollection!, options), |
42 | 45 | } |
43 | 46 | })) |
44 | 47 | } |
@@ -78,4 +81,29 @@ export default defineNitroPlugin((nitroApp: NitroApp) => { |
78 | 81 | } |
79 | 82 | } |
80 | 83 | }) |
| 84 | + |
| 85 | + if (['nitro-prerender', 'nitro-dev'].includes(import.meta.preset as string)) { |
| 86 | + nitroApp.hooks.hook('beforeResponse', (event) => { |
| 87 | + if (event.path === '/') { |
| 88 | + appendHeader(event, 'x-nitro-prerender', Array.from(prerenderPaths)) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + function getDocumentLink(link: string, collection: string, options: ModuleOptions) { |
| 94 | + const contentRawMD = (options as unknown as { contentRawMD: false | { excludeCollections: string[] } })?.contentRawMD |
| 95 | + if (contentRawMD === false || contentRawMD?.excludeCollections?.includes(collection)) { |
| 96 | + return withBase(link, options.domain) |
| 97 | + } |
| 98 | + |
| 99 | + link = `/raw${link}.md` |
| 100 | + |
| 101 | + if (link.endsWith('/.md')) { |
| 102 | + link = link.slice(0, -3) + 'index.md' |
| 103 | + } |
| 104 | + |
| 105 | + prerenderPaths.add(link) |
| 106 | + |
| 107 | + return withBase(link, options.domain) |
| 108 | + } |
81 | 109 | }) |
0 commit comments