|
| 1 | +import { appendHeader } from 'h3' |
| 2 | +import type { NavigationItem } from 'comark-content' |
| 3 | +import { useAppConfig } from 'nitropack/runtime' |
| 4 | +import type { NitroApp } from 'nitropack/types' |
| 5 | +import { joinURL } from 'ufo' |
| 6 | +import type { LLMsSection } from 'nuxt-llms' |
| 7 | + |
| 8 | +export default defineNitroPlugin((nitroApp: NitroApp) => { |
| 9 | + const prerenderPaths = new Set<string>() |
| 10 | + |
| 11 | + nitroApp.hooks.hook('llms:generate', async (event, options) => { |
| 12 | + const content = await getProdContent() |
| 13 | + const navigation = await content.navigation() |
| 14 | + const site = getSiteConfig(event) |
| 15 | + const appConfig = useAppConfig(event) |
| 16 | + const siteName = appConfig.seo?.siteName || site.name || options.title || '' |
| 17 | + const siteUrl = options.domain || site.url || '/' |
| 18 | + |
| 19 | + options.title ||= siteName |
| 20 | + options.description ||= appConfig.docs?.llms?.description || site.description || '' |
| 21 | + |
| 22 | + const introLinks = [ |
| 23 | + { |
| 24 | + title: 'Landing page', |
| 25 | + description: `Overview of ${siteName}`, |
| 26 | + href: documentLink('/', siteUrl, prerenderPaths), |
| 27 | + }, |
| 28 | + ...standaloneLinks(navigation, siteUrl, prerenderPaths), |
| 29 | + ] |
| 30 | + |
| 31 | + options.sections.unshift({ |
| 32 | + title: 'Documentation', |
| 33 | + description: 'Every page below is available as raw markdown. Fetch any URL directly.', |
| 34 | + links: introLinks, |
| 35 | + }) |
| 36 | + |
| 37 | + for (const item of navigation) { |
| 38 | + if (!item.children?.length) continue |
| 39 | + |
| 40 | + const links = sectionLinks(item, siteUrl, prerenderPaths) |
| 41 | + if (!links.length) continue |
| 42 | + |
| 43 | + options.sections.push({ |
| 44 | + title: item.title, |
| 45 | + description: item.description, |
| 46 | + links, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + const extraLinks = (appConfig.docs?.llms?.links ?? []) as LLMsSection['links'] |
| 51 | + if (extraLinks?.length) { |
| 52 | + options.sections.push({ |
| 53 | + title: 'Optional', |
| 54 | + links: extraLinks, |
| 55 | + }) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + nitroApp.hooks.hook('llms:generate:full', async (_event, _options, contents) => { |
| 60 | + const content = await getProdContent() |
| 61 | + const navigation = await content.navigation() |
| 62 | + |
| 63 | + const paths: string[] = [] |
| 64 | + const collect = (items: NavigationItem[]) => { |
| 65 | + for (const item of items) { |
| 66 | + if (item.page !== false && item.path && item.path !== '/') paths.push(item.path) |
| 67 | + if (item.children?.length) collect(item.children) |
| 68 | + } |
| 69 | + } |
| 70 | + collect(navigation) |
| 71 | + |
| 72 | + const pages = await Promise.all( |
| 73 | + [...new Set(paths)].map((path) => renderPageMarkdown(content, path)) |
| 74 | + ) |
| 75 | + contents.push(...pages.filter((page): page is string => Boolean(page))) |
| 76 | + }) |
| 77 | + |
| 78 | + if (['nitro-prerender', 'nitro-dev'].includes(import.meta.preset as string)) { |
| 79 | + nitroApp.hooks.hook('beforeResponse', (event) => { |
| 80 | + if (event.path === '/') { |
| 81 | + appendHeader(event, 'x-nitro-prerender', Array.from(prerenderPaths)) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +}) |
| 86 | + |
| 87 | +function standaloneLinks( |
| 88 | + navigation: NavigationItem[], |
| 89 | + siteUrl: string, |
| 90 | + prerenderPaths: Set<string> |
| 91 | +): NonNullable<LLMsSection['links']> { |
| 92 | + return navigation |
| 93 | + .filter((item) => !item.children?.length && item.page !== false && item.path && item.path !== '/') |
| 94 | + .map((item) => ({ |
| 95 | + title: item.title, |
| 96 | + description: item.description, |
| 97 | + href: documentLink(item.path, siteUrl, prerenderPaths), |
| 98 | + })) |
| 99 | +} |
| 100 | + |
| 101 | +function sectionLinks( |
| 102 | + item: NavigationItem, |
| 103 | + siteUrl: string, |
| 104 | + prerenderPaths: Set<string> |
| 105 | +): NonNullable<LLMsSection['links']> { |
| 106 | + const links: NonNullable<LLMsSection['links']> = [] |
| 107 | + |
| 108 | + const collect = (items: NavigationItem[]) => { |
| 109 | + for (const entry of items) { |
| 110 | + if (entry.page !== false && entry.path && entry.path !== '/') { |
| 111 | + links.push({ |
| 112 | + title: entry.title, |
| 113 | + description: entry.description, |
| 114 | + href: documentLink(entry.path, siteUrl, prerenderPaths), |
| 115 | + }) |
| 116 | + } |
| 117 | + if (entry.children?.length) collect(entry.children) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (item.page !== false && item.path && item.path !== '/') { |
| 122 | + links.push({ |
| 123 | + title: item.title, |
| 124 | + description: item.description, |
| 125 | + href: documentLink(item.path, siteUrl, prerenderPaths), |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + if (item.children?.length) collect(item.children) |
| 130 | + |
| 131 | + return links |
| 132 | +} |
| 133 | + |
| 134 | +function documentLink(path: string, domain: string, prerenderPaths: Set<string>) { |
| 135 | + const href = joinURL(domain, rawUrlForPage(path)) |
| 136 | + prerenderPaths.add(rawUrlForPage(path)) |
| 137 | + return href |
| 138 | +} |
0 commit comments