@farming-labs/nuxt 0.0.37 → 0.0.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api-reference.d.ts +3 -0
- package/dist/api-reference.d.ts.map +1 -0
- package/dist/api-reference.js +21 -0
- package/dist/api-reference.js.map +1 -0
- package/dist/docs/src/api-reference.d.ts +27 -0
- package/dist/docs/src/api-reference.d.ts.map +1 -0
- package/dist/docs/src/api-reference.js +594 -0
- package/dist/docs/src/api-reference.js.map +1 -0
- package/dist/docs/src/create-theme.d.ts +74 -0
- package/dist/docs/src/create-theme.d.ts.map +1 -0
- package/dist/docs/src/create-theme.js +86 -0
- package/dist/docs/src/create-theme.js.map +1 -0
- package/dist/docs/src/define-docs.d.ts +6 -0
- package/dist/docs/src/define-docs.d.ts.map +1 -0
- package/dist/docs/src/define-docs.js +27 -0
- package/dist/docs/src/define-docs.js.map +1 -0
- package/dist/docs/src/i18n.d.ts +15 -0
- package/dist/docs/src/i18n.d.ts.map +1 -0
- package/dist/docs/src/i18n.js +48 -0
- package/dist/docs/src/i18n.js.map +1 -0
- package/dist/docs/src/index.d.ts +16 -0
- package/dist/docs/src/index.d.ts.map +1 -0
- package/dist/docs/src/index.js +14 -0
- package/dist/docs/src/index.js.map +1 -0
- package/dist/docs/src/metadata.d.ts +24 -0
- package/dist/docs/src/metadata.d.ts.map +1 -0
- package/dist/docs/src/metadata.js +90 -0
- package/dist/docs/src/metadata.js.map +1 -0
- package/dist/docs/src/server.d.ts +3 -0
- package/dist/docs/src/server.d.ts.map +1 -0
- package/dist/docs/src/server.js +2 -0
- package/dist/docs/src/server.js.map +1 -0
- package/dist/docs/src/types.d.ts +1344 -0
- package/dist/docs/src/types.d.ts.map +1 -0
- package/dist/docs/src/types.js +6 -0
- package/dist/docs/src/types.js.map +1 -0
- package/dist/docs/src/utils.d.ts +6 -0
- package/dist/docs/src/utils.d.ts.map +1 -0
- package/dist/docs/src/utils.js +32 -0
- package/dist/docs/src/utils.js.map +1 -0
- package/dist/nuxt/src/api-reference.d.ts +3 -0
- package/dist/nuxt/src/api-reference.d.ts.map +1 -0
- package/dist/nuxt/src/api-reference.js +20 -0
- package/dist/nuxt/src/api-reference.js.map +1 -0
- package/dist/nuxt/src/content.d.ts +54 -0
- package/dist/nuxt/src/content.d.ts.map +1 -0
- package/dist/nuxt/src/content.js +202 -0
- package/dist/nuxt/src/content.js.map +1 -0
- package/dist/nuxt/src/index.d.ts +11 -0
- package/dist/nuxt/src/index.d.ts.map +1 -0
- package/dist/nuxt/src/index.js +11 -0
- package/dist/nuxt/src/index.js.map +1 -0
- package/dist/nuxt/src/markdown.d.ts +18 -0
- package/dist/nuxt/src/markdown.d.ts.map +1 -0
- package/dist/nuxt/src/markdown.js +277 -0
- package/dist/nuxt/src/markdown.js.map +1 -0
- package/dist/nuxt/src/server.d.ts +87 -0
- package/dist/nuxt/src/server.d.ts.map +1 -0
- package/dist/nuxt/src/server.js +726 -0
- package/dist/nuxt/src/server.js.map +1 -0
- package/dist/server.d.ts +3 -2
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +4 -2
- package/dist/server.js.map +1 -1
- package/package.json +9 -2
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for Nuxt docs routes.
|
|
3
|
+
*
|
|
4
|
+
* The simplest setup is a single file:
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* // server/api/docs.ts
|
|
9
|
+
* import { defineDocsHandler } from "@farming-labs/nuxt/server";
|
|
10
|
+
* import config from "../../docs.config";
|
|
11
|
+
* export default defineDocsHandler(config);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* That one handler serves page loads, search, and AI chat.
|
|
15
|
+
*/
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import matter from "gray-matter";
|
|
19
|
+
import { resolveDocsI18n, resolveDocsLocale, resolveDocsPath } from "@farming-labs/docs";
|
|
20
|
+
import { loadDocsNavTree, loadDocsContent, flattenNavTree } from "./content.js";
|
|
21
|
+
import { renderMarkdown } from "./markdown.js";
|
|
22
|
+
export { defineApiReferenceHandler } from "./api-reference.js";
|
|
23
|
+
function resolveAIModelAndProvider(aiConfig, requestedModelId) {
|
|
24
|
+
const raw = aiConfig.model;
|
|
25
|
+
const modelList = (typeof raw === "object" && raw?.models) || [];
|
|
26
|
+
let modelId = requestedModelId;
|
|
27
|
+
if (!modelId) {
|
|
28
|
+
if (typeof raw === "string")
|
|
29
|
+
modelId = raw;
|
|
30
|
+
else if (typeof raw === "object")
|
|
31
|
+
modelId = raw.defaultModel ?? raw.models?.[0]?.id;
|
|
32
|
+
if (!modelId)
|
|
33
|
+
modelId = "gpt-4o-mini";
|
|
34
|
+
}
|
|
35
|
+
const entry = modelList.find((m) => m.id === modelId);
|
|
36
|
+
const providerKey = entry?.provider;
|
|
37
|
+
const providerConfig = providerKey && aiConfig.providers?.[providerKey];
|
|
38
|
+
const baseUrl = ((providerConfig && providerConfig.baseUrl) ||
|
|
39
|
+
aiConfig.baseUrl ||
|
|
40
|
+
"https://api.openai.com/v1").replace(/\/$/, "");
|
|
41
|
+
const apiKey = (providerConfig && providerConfig.apiKey) ||
|
|
42
|
+
aiConfig.apiKey ||
|
|
43
|
+
(typeof process !== "undefined" ? process.env?.OPENAI_API_KEY : undefined);
|
|
44
|
+
return { model: modelId, baseUrl, apiKey };
|
|
45
|
+
}
|
|
46
|
+
function stripMarkdownText(content) {
|
|
47
|
+
return content
|
|
48
|
+
.replace(/^(import|export)\s.*$/gm, "")
|
|
49
|
+
.replace(/<[^>]+\/>/g, "")
|
|
50
|
+
.replace(/<\/?[A-Z][^>]*>/g, "")
|
|
51
|
+
.replace(/<\/?[a-z][^>]*>/g, "")
|
|
52
|
+
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
|
|
53
|
+
.replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1")
|
|
54
|
+
.replace(/^#{1,6}\s+/gm, "")
|
|
55
|
+
.replace(/(\*{1,3}|_{1,3})(.*?)\1/g, "$2")
|
|
56
|
+
.replace(/```[\s\S]*?```/g, "")
|
|
57
|
+
.replace(/`([^`]+)`/g, "$1")
|
|
58
|
+
.replace(/^>\s+/gm, "")
|
|
59
|
+
.replace(/^[-*_]{3,}\s*$/gm, "")
|
|
60
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
61
|
+
.trim();
|
|
62
|
+
}
|
|
63
|
+
function normalizePathSegment(value) {
|
|
64
|
+
return value.replace(/^\/+|\/+$/g, "");
|
|
65
|
+
}
|
|
66
|
+
function joinPathParts(...parts) {
|
|
67
|
+
return parts
|
|
68
|
+
.map((part) => normalizePathSegment(part))
|
|
69
|
+
.filter(Boolean)
|
|
70
|
+
.join("/");
|
|
71
|
+
}
|
|
72
|
+
function toPosixPath(value) {
|
|
73
|
+
return value.replace(/\\\\/g, "/");
|
|
74
|
+
}
|
|
75
|
+
function buildDirPrefix(contentDir) {
|
|
76
|
+
const rel = path.isAbsolute(contentDir)
|
|
77
|
+
? toPosixPath(path.relative(process.cwd(), contentDir))
|
|
78
|
+
: toPosixPath(contentDir);
|
|
79
|
+
const normalized = normalizePathSegment(rel);
|
|
80
|
+
return normalized ? `/${normalized}/` : "/";
|
|
81
|
+
}
|
|
82
|
+
function navTreeFromMap(contentMap, dirPrefix, entry, ordering) {
|
|
83
|
+
const dirs = [];
|
|
84
|
+
for (const key of Object.keys(contentMap)) {
|
|
85
|
+
if (!key.startsWith(dirPrefix))
|
|
86
|
+
continue;
|
|
87
|
+
const rel = key.slice(dirPrefix.length);
|
|
88
|
+
const segments = rel.split("/");
|
|
89
|
+
const fileName = segments.pop();
|
|
90
|
+
const base = fileName.replace(/\.(md|mdx|svx)$/, "");
|
|
91
|
+
if (base !== "page" && base !== "index" && base !== "+page")
|
|
92
|
+
continue;
|
|
93
|
+
const { data } = matter(contentMap[key]);
|
|
94
|
+
const dirParts = segments;
|
|
95
|
+
const slug = dirParts.join("/");
|
|
96
|
+
const url = slug ? `/${entry}/${slug}` : `/${entry}`;
|
|
97
|
+
const fallbackTitle = dirParts.length > 0
|
|
98
|
+
? dirParts[dirParts.length - 1].replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase())
|
|
99
|
+
: "Documentation";
|
|
100
|
+
dirs.push({
|
|
101
|
+
parts: dirParts,
|
|
102
|
+
title: data.title ?? fallbackTitle,
|
|
103
|
+
url,
|
|
104
|
+
icon: data.icon,
|
|
105
|
+
order: typeof data.order === "number" ? data.order : Infinity,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
dirs.sort((a, b) => {
|
|
109
|
+
if (a.parts.length !== b.parts.length)
|
|
110
|
+
return a.parts.length - b.parts.length;
|
|
111
|
+
return a.parts.join("/").localeCompare(b.parts.join("/"));
|
|
112
|
+
});
|
|
113
|
+
const children = [];
|
|
114
|
+
const rootInfo = dirs.find((d) => d.parts.length === 0);
|
|
115
|
+
if (rootInfo) {
|
|
116
|
+
children.push({
|
|
117
|
+
type: "page",
|
|
118
|
+
name: rootInfo.title,
|
|
119
|
+
url: rootInfo.url,
|
|
120
|
+
icon: rootInfo.icon,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function findSlugOrder(parentParts) {
|
|
124
|
+
if (!Array.isArray(ordering))
|
|
125
|
+
return undefined;
|
|
126
|
+
let items = ordering;
|
|
127
|
+
for (const part of parentParts) {
|
|
128
|
+
const found = items.find((i) => i.slug === part);
|
|
129
|
+
if (!found?.children)
|
|
130
|
+
return undefined;
|
|
131
|
+
items = found.children;
|
|
132
|
+
}
|
|
133
|
+
return items;
|
|
134
|
+
}
|
|
135
|
+
function buildLevel(parentParts) {
|
|
136
|
+
const depth = parentParts.length;
|
|
137
|
+
const directChildren = dirs.filter((d) => {
|
|
138
|
+
if (d.parts.length !== depth + 1)
|
|
139
|
+
return false;
|
|
140
|
+
for (let i = 0; i < depth; i++) {
|
|
141
|
+
if (d.parts[i] !== parentParts[i])
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
});
|
|
146
|
+
const slugOrder = findSlugOrder(parentParts);
|
|
147
|
+
if (slugOrder) {
|
|
148
|
+
const slugMap = new Set(slugOrder.map((i) => i.slug));
|
|
149
|
+
const ordered = [];
|
|
150
|
+
for (const item of slugOrder) {
|
|
151
|
+
const match = directChildren.find((d) => d.parts[depth] === item.slug);
|
|
152
|
+
if (match)
|
|
153
|
+
ordered.push(match);
|
|
154
|
+
}
|
|
155
|
+
for (const child of directChildren) {
|
|
156
|
+
if (!slugMap.has(child.parts[depth]))
|
|
157
|
+
ordered.push(child);
|
|
158
|
+
}
|
|
159
|
+
const nodes = [];
|
|
160
|
+
for (const child of ordered) {
|
|
161
|
+
const hasGrandChildren = dirs.some((d) => {
|
|
162
|
+
if (d.parts.length <= child.parts.length)
|
|
163
|
+
return false;
|
|
164
|
+
return child.parts.every((p, i) => d.parts[i] === p);
|
|
165
|
+
});
|
|
166
|
+
if (hasGrandChildren) {
|
|
167
|
+
nodes.push({
|
|
168
|
+
type: "folder",
|
|
169
|
+
name: child.title,
|
|
170
|
+
icon: child.icon,
|
|
171
|
+
index: { type: "page", name: child.title, url: child.url, icon: child.icon },
|
|
172
|
+
children: buildLevel(child.parts),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
nodes.push({ type: "page", name: child.title, url: child.url, icon: child.icon });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return nodes;
|
|
180
|
+
}
|
|
181
|
+
if (ordering === "numeric") {
|
|
182
|
+
directChildren.sort((a, b) => {
|
|
183
|
+
if (a.order === b.order)
|
|
184
|
+
return 0;
|
|
185
|
+
return a.order - b.order;
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
const nodes = [];
|
|
189
|
+
for (const child of directChildren) {
|
|
190
|
+
const hasGrandChildren = dirs.some((d) => {
|
|
191
|
+
if (d.parts.length <= child.parts.length)
|
|
192
|
+
return false;
|
|
193
|
+
return child.parts.every((p, i) => d.parts[i] === p);
|
|
194
|
+
});
|
|
195
|
+
if (hasGrandChildren) {
|
|
196
|
+
nodes.push({
|
|
197
|
+
type: "folder",
|
|
198
|
+
name: child.title,
|
|
199
|
+
icon: child.icon,
|
|
200
|
+
index: {
|
|
201
|
+
type: "page",
|
|
202
|
+
name: child.title,
|
|
203
|
+
url: child.url,
|
|
204
|
+
icon: child.icon,
|
|
205
|
+
},
|
|
206
|
+
children: buildLevel(child.parts),
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
nodes.push({
|
|
211
|
+
type: "page",
|
|
212
|
+
name: child.title,
|
|
213
|
+
url: child.url,
|
|
214
|
+
icon: child.icon,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return nodes;
|
|
219
|
+
}
|
|
220
|
+
children.push(...buildLevel([]));
|
|
221
|
+
return { name: "Docs", children };
|
|
222
|
+
}
|
|
223
|
+
function searchIndexFromMap(contentMap, dirPrefix, entry) {
|
|
224
|
+
const pages = [];
|
|
225
|
+
for (const [key, raw] of Object.entries(contentMap)) {
|
|
226
|
+
if (!key.startsWith(dirPrefix))
|
|
227
|
+
continue;
|
|
228
|
+
const rel = key.slice(dirPrefix.length);
|
|
229
|
+
const segments = rel.split("/");
|
|
230
|
+
const fileName = segments.pop();
|
|
231
|
+
const base = fileName.replace(/\.(md|mdx|svx)$/, "");
|
|
232
|
+
const isIdx = base === "page" || base === "index" || base === "+page";
|
|
233
|
+
const slug = isIdx ? segments.join("/") : [...segments, base].join("/");
|
|
234
|
+
const url = slug ? `/${entry}/${slug}` : `/${entry}`;
|
|
235
|
+
const { data, content } = matter(raw);
|
|
236
|
+
const title = data.title ?? base.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
|
237
|
+
pages.push({
|
|
238
|
+
slug,
|
|
239
|
+
url,
|
|
240
|
+
title,
|
|
241
|
+
description: data.description,
|
|
242
|
+
icon: data.icon,
|
|
243
|
+
content: stripMarkdownText(content),
|
|
244
|
+
rawContent: content,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return pages;
|
|
248
|
+
}
|
|
249
|
+
function findPageInMap(contentMap, dirPrefix, slug) {
|
|
250
|
+
const isIndex = slug === "";
|
|
251
|
+
const candidates = isIndex
|
|
252
|
+
? ["page.md", "page.mdx", "index.md"]
|
|
253
|
+
: [
|
|
254
|
+
`${slug}/page.md`,
|
|
255
|
+
`${slug}/page.mdx`,
|
|
256
|
+
`${slug}/index.md`,
|
|
257
|
+
`${slug}/index.svx`,
|
|
258
|
+
`${slug}.md`,
|
|
259
|
+
`${slug}.svx`,
|
|
260
|
+
];
|
|
261
|
+
for (const candidate of candidates) {
|
|
262
|
+
const key = `${dirPrefix}${candidate}`;
|
|
263
|
+
if (key in contentMap) {
|
|
264
|
+
return { raw: contentMap[key], relPath: candidate };
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Create all server-side functions needed for a Nuxt docs site.
|
|
271
|
+
*
|
|
272
|
+
* @param config - The `DocsConfig` object (from `defineDocs()` in `docs.config.ts`).
|
|
273
|
+
*
|
|
274
|
+
* Pass `_preloadedContent` (from `import.meta.glob`) to bundle markdown files
|
|
275
|
+
* at build time — required for serverless deployments (Vercel, Netlify, etc.)
|
|
276
|
+
* where the filesystem is not available at runtime.
|
|
277
|
+
*/
|
|
278
|
+
export function createDocsServer(config = {}) {
|
|
279
|
+
const entry = config.entry ?? "docs";
|
|
280
|
+
const contentDirBase = config.contentDir ?? entry;
|
|
281
|
+
const i18n = resolveDocsI18n(config.i18n);
|
|
282
|
+
const githubRaw = config.github;
|
|
283
|
+
const github = typeof githubRaw === "string" ? { url: githubRaw } : (githubRaw ?? null);
|
|
284
|
+
const githubRepo = github?.url;
|
|
285
|
+
const githubBranch = github?.branch ?? "main";
|
|
286
|
+
const githubContentPath = github?.directory;
|
|
287
|
+
const preloaded = config._preloadedContent;
|
|
288
|
+
const ordering = config.ordering;
|
|
289
|
+
const aiConfig = config.ai ?? {};
|
|
290
|
+
if (config.apiKey && !aiConfig.apiKey) {
|
|
291
|
+
aiConfig.apiKey = config.apiKey;
|
|
292
|
+
}
|
|
293
|
+
function resolveContentDirRel(locale) {
|
|
294
|
+
if (!locale)
|
|
295
|
+
return contentDirBase;
|
|
296
|
+
if (path.isAbsolute(contentDirBase))
|
|
297
|
+
return path.join(contentDirBase, locale);
|
|
298
|
+
return joinPathParts(contentDirBase, locale);
|
|
299
|
+
}
|
|
300
|
+
function resolveContextFromPath(pathname, locale) {
|
|
301
|
+
const match = resolveDocsPath(pathname, entry);
|
|
302
|
+
const contentDirRel = resolveContentDirRel(locale);
|
|
303
|
+
const contentDirAbs = path.isAbsolute(contentDirRel)
|
|
304
|
+
? contentDirRel
|
|
305
|
+
: path.resolve(process.cwd(), contentDirRel);
|
|
306
|
+
return {
|
|
307
|
+
...match,
|
|
308
|
+
locale,
|
|
309
|
+
contentDirRel,
|
|
310
|
+
contentDirAbs,
|
|
311
|
+
dirPrefix: buildDirPrefix(contentDirRel),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
function resolveLocaleFromRequest(request) {
|
|
315
|
+
if (!i18n)
|
|
316
|
+
return undefined;
|
|
317
|
+
const url = new URL(request.url);
|
|
318
|
+
const direct = resolveDocsLocale(url.searchParams, i18n);
|
|
319
|
+
if (direct)
|
|
320
|
+
return direct;
|
|
321
|
+
const referrer = request.headers.get("referer") ?? request.headers.get("referrer");
|
|
322
|
+
if (referrer) {
|
|
323
|
+
try {
|
|
324
|
+
const refUrl = new URL(referrer);
|
|
325
|
+
const fromRef = resolveDocsLocale(refUrl.searchParams, i18n);
|
|
326
|
+
if (fromRef)
|
|
327
|
+
return fromRef;
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
// ignore
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return i18n.defaultLocale;
|
|
334
|
+
}
|
|
335
|
+
function resolveContextFromRequest(request) {
|
|
336
|
+
const locale = resolveLocaleFromRequest(request);
|
|
337
|
+
const url = new URL(request.url);
|
|
338
|
+
const pathnameParam = url.searchParams.get("pathname");
|
|
339
|
+
const referrer = request.headers.get("referer") ?? request.headers.get("referrer");
|
|
340
|
+
const refPath = referrer ? new URL(referrer).pathname : undefined;
|
|
341
|
+
const pathname = pathnameParam ?? refPath ?? `/${entry}`;
|
|
342
|
+
return resolveContextFromPath(pathname, locale);
|
|
343
|
+
}
|
|
344
|
+
// ─── Unified load (tree + page content in one call) ────────
|
|
345
|
+
async function load(pathname) {
|
|
346
|
+
let url;
|
|
347
|
+
try {
|
|
348
|
+
url = new URL(pathname);
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
url = new URL(pathname, "http://localhost");
|
|
352
|
+
}
|
|
353
|
+
const locale = resolveDocsLocale(url.searchParams, i18n) ?? i18n?.defaultLocale;
|
|
354
|
+
const ctx = resolveContextFromPath(url.pathname, locale);
|
|
355
|
+
const tree = preloaded
|
|
356
|
+
? navTreeFromMap(preloaded, ctx.dirPrefix, entry, ordering)
|
|
357
|
+
: loadDocsNavTree(ctx.contentDirAbs, entry, ordering);
|
|
358
|
+
const flatPages = flattenNavTree(tree);
|
|
359
|
+
const slug = ctx.slug;
|
|
360
|
+
const isIndex = slug === "";
|
|
361
|
+
let raw;
|
|
362
|
+
let relPath;
|
|
363
|
+
let lastModified;
|
|
364
|
+
if (preloaded) {
|
|
365
|
+
const result = findPageInMap(preloaded, ctx.dirPrefix, slug);
|
|
366
|
+
if (!result) {
|
|
367
|
+
const err = new Error(`Page not found: /${entry}/${slug}`);
|
|
368
|
+
err.status = 404;
|
|
369
|
+
throw err;
|
|
370
|
+
}
|
|
371
|
+
raw = result.raw;
|
|
372
|
+
relPath = result.relPath;
|
|
373
|
+
lastModified = new Date().toLocaleDateString("en-US", {
|
|
374
|
+
year: "numeric",
|
|
375
|
+
month: "long",
|
|
376
|
+
day: "numeric",
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
let filePath = null;
|
|
381
|
+
relPath = "";
|
|
382
|
+
if (isIndex) {
|
|
383
|
+
for (const name of ["page.md", "page.mdx", "index.md"]) {
|
|
384
|
+
const candidate = path.join(ctx.contentDirAbs, name);
|
|
385
|
+
if (fs.existsSync(candidate)) {
|
|
386
|
+
filePath = candidate;
|
|
387
|
+
relPath = name;
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
const candidates = [
|
|
394
|
+
path.join(ctx.contentDirAbs, slug, "page.md"),
|
|
395
|
+
path.join(ctx.contentDirAbs, slug, "page.mdx"),
|
|
396
|
+
path.join(ctx.contentDirAbs, slug, "index.md"),
|
|
397
|
+
path.join(ctx.contentDirAbs, slug, "index.svx"),
|
|
398
|
+
path.join(ctx.contentDirAbs, `${slug}.md`),
|
|
399
|
+
path.join(ctx.contentDirAbs, `${slug}.svx`),
|
|
400
|
+
];
|
|
401
|
+
for (const candidate of candidates) {
|
|
402
|
+
if (fs.existsSync(candidate)) {
|
|
403
|
+
filePath = candidate;
|
|
404
|
+
relPath = path.relative(ctx.contentDirAbs, candidate);
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (!filePath) {
|
|
410
|
+
const err = new Error(`Page not found: /${entry}/${slug}`);
|
|
411
|
+
err.status = 404;
|
|
412
|
+
throw err;
|
|
413
|
+
}
|
|
414
|
+
raw = fs.readFileSync(filePath, "utf-8");
|
|
415
|
+
const stat = fs.statSync(filePath);
|
|
416
|
+
lastModified = stat.mtime.toLocaleDateString("en-US", {
|
|
417
|
+
year: "numeric",
|
|
418
|
+
month: "long",
|
|
419
|
+
day: "numeric",
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
const { data, content } = matter(raw);
|
|
423
|
+
const html = await renderMarkdown(content);
|
|
424
|
+
const currentUrl = isIndex ? `/${entry}` : `/${entry}/${slug}`;
|
|
425
|
+
const currentIndex = flatPages.findIndex((p) => p.url === currentUrl);
|
|
426
|
+
const previousPage = currentIndex > 0 ? flatPages[currentIndex - 1] : null;
|
|
427
|
+
const nextPage = currentIndex < flatPages.length - 1 ? flatPages[currentIndex + 1] : null;
|
|
428
|
+
let editOnGithub;
|
|
429
|
+
if (githubRepo && githubContentPath) {
|
|
430
|
+
const trimmed = githubContentPath.replace(/\/+$/, "");
|
|
431
|
+
const localePrefix = ctx.locale ? `${ctx.locale}/` : "";
|
|
432
|
+
editOnGithub = `${githubRepo}/blob/${githubBranch}/${trimmed}/${localePrefix}${relPath}`;
|
|
433
|
+
}
|
|
434
|
+
const fallbackTitle = isIndex
|
|
435
|
+
? "Documentation"
|
|
436
|
+
: (slug.split("/").pop()?.replace(/-/g, " ") ?? "Documentation");
|
|
437
|
+
return {
|
|
438
|
+
tree,
|
|
439
|
+
flatPages,
|
|
440
|
+
title: data.title ?? fallbackTitle,
|
|
441
|
+
description: data.description,
|
|
442
|
+
html,
|
|
443
|
+
entry,
|
|
444
|
+
locale: ctx.locale,
|
|
445
|
+
...(isIndex ? {} : { slug }),
|
|
446
|
+
previousPage,
|
|
447
|
+
nextPage,
|
|
448
|
+
editOnGithub,
|
|
449
|
+
lastModified,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
// ─── Search index ──────────────────────────────────────────
|
|
453
|
+
const searchIndexByEntry = new Map();
|
|
454
|
+
function getSearchIndex(ctx) {
|
|
455
|
+
const key = ctx.locale ?? "__default__";
|
|
456
|
+
const cached = searchIndexByEntry.get(key);
|
|
457
|
+
if (cached)
|
|
458
|
+
return cached;
|
|
459
|
+
const index = preloaded
|
|
460
|
+
? searchIndexFromMap(preloaded, ctx.dirPrefix, entry)
|
|
461
|
+
: loadDocsContent(ctx.contentDirAbs, entry);
|
|
462
|
+
searchIndexByEntry.set(key, index);
|
|
463
|
+
return index;
|
|
464
|
+
}
|
|
465
|
+
function searchByQuery(query, ctx) {
|
|
466
|
+
const index = getSearchIndex(ctx);
|
|
467
|
+
return index
|
|
468
|
+
.map((page) => {
|
|
469
|
+
const titleMatch = page.title.toLowerCase().includes(query) ? 10 : 0;
|
|
470
|
+
const words = query.split(/\s+/);
|
|
471
|
+
const contentMatch = words.reduce((score, word) => {
|
|
472
|
+
return score + (page.content.toLowerCase().includes(word) ? 1 : 0);
|
|
473
|
+
}, 0);
|
|
474
|
+
return { ...page, score: titleMatch + contentMatch };
|
|
475
|
+
})
|
|
476
|
+
.filter((r) => r.score > 0)
|
|
477
|
+
.sort((a, b) => b.score - a.score);
|
|
478
|
+
}
|
|
479
|
+
// ─── llms.txt content builder ────────────────────────────────
|
|
480
|
+
const llmsSiteTitle = typeof config.nav === "object" &&
|
|
481
|
+
typeof config.nav?.title === "string"
|
|
482
|
+
? config.nav.title
|
|
483
|
+
: "Documentation";
|
|
484
|
+
const llmsTxtConfig = config.llmsTxt;
|
|
485
|
+
const llmsBaseUrl = typeof llmsTxtConfig === "object" ? (llmsTxtConfig.baseUrl ?? "") : "";
|
|
486
|
+
const llmsTitle = typeof llmsTxtConfig === "object" ? (llmsTxtConfig.siteTitle ?? llmsSiteTitle) : llmsSiteTitle;
|
|
487
|
+
const llmsDesc = typeof llmsTxtConfig === "object" ? llmsTxtConfig.siteDescription : undefined;
|
|
488
|
+
const llmsCache = new Map();
|
|
489
|
+
function getLlmsContent(ctx) {
|
|
490
|
+
const key = ctx.locale ?? "__default__";
|
|
491
|
+
const cached = llmsCache.get(key);
|
|
492
|
+
if (cached)
|
|
493
|
+
return cached;
|
|
494
|
+
const pages = getSearchIndex(ctx);
|
|
495
|
+
let llmsTxt = `# ${llmsTitle}\n\n`;
|
|
496
|
+
let llmsFullTxt = `# ${llmsTitle}\n\n`;
|
|
497
|
+
if (llmsDesc) {
|
|
498
|
+
llmsTxt += `> ${llmsDesc}\n\n`;
|
|
499
|
+
llmsFullTxt += `> ${llmsDesc}\n\n`;
|
|
500
|
+
}
|
|
501
|
+
llmsTxt += `## Pages\n\n`;
|
|
502
|
+
for (const page of pages) {
|
|
503
|
+
llmsTxt += `- [${page.title}](${llmsBaseUrl}${page.url})`;
|
|
504
|
+
if (page.description)
|
|
505
|
+
llmsTxt += `: ${page.description}`;
|
|
506
|
+
llmsTxt += `\n`;
|
|
507
|
+
llmsFullTxt += `## ${page.title}\n\n`;
|
|
508
|
+
llmsFullTxt += `URL: ${llmsBaseUrl}${page.url}\n\n`;
|
|
509
|
+
if (page.description)
|
|
510
|
+
llmsFullTxt += `${page.description}\n\n`;
|
|
511
|
+
llmsFullTxt += `${page.content}\n\n---\n\n`;
|
|
512
|
+
}
|
|
513
|
+
const next = { llmsTxt, llmsFullTxt };
|
|
514
|
+
llmsCache.set(key, next);
|
|
515
|
+
return next;
|
|
516
|
+
}
|
|
517
|
+
// ─── GET /api/docs?query=… | ?format=llms | ?format=llms-full ──
|
|
518
|
+
function GET(context) {
|
|
519
|
+
const ctx = resolveContextFromRequest(context.request);
|
|
520
|
+
const url = new URL(context.request.url);
|
|
521
|
+
const format = url.searchParams.get("format");
|
|
522
|
+
if (format === "llms" || format === "llms-full") {
|
|
523
|
+
const llmsContent = getLlmsContent(ctx);
|
|
524
|
+
return new Response(format === "llms-full" ? llmsContent.llmsFullTxt : llmsContent.llmsTxt, {
|
|
525
|
+
headers: {
|
|
526
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
527
|
+
"Cache-Control": "public, max-age=3600",
|
|
528
|
+
},
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
const query = url.searchParams.get("query")?.toLowerCase().trim();
|
|
532
|
+
if (!query) {
|
|
533
|
+
return new Response(JSON.stringify([]), {
|
|
534
|
+
headers: { "Content-Type": "application/json" },
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
const results = searchByQuery(query, ctx)
|
|
538
|
+
.slice(0, 10)
|
|
539
|
+
.map(({ title, url, description }) => ({
|
|
540
|
+
content: title,
|
|
541
|
+
url,
|
|
542
|
+
description,
|
|
543
|
+
}));
|
|
544
|
+
return new Response(JSON.stringify(results), {
|
|
545
|
+
headers: { "Content-Type": "application/json" },
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
// ─── POST /api/docs — AI chat with RAG ────────────────────
|
|
549
|
+
const projectName = (typeof config.nav?.title === "string"
|
|
550
|
+
? config.nav.title
|
|
551
|
+
: null);
|
|
552
|
+
const packageName = aiConfig.packageName;
|
|
553
|
+
const docsUrl = aiConfig.docsUrl;
|
|
554
|
+
function buildDefaultSystemPrompt() {
|
|
555
|
+
const lines = [
|
|
556
|
+
`You are a helpful documentation assistant${projectName ? ` for ${projectName}` : ""}.`,
|
|
557
|
+
"Answer questions based on the provided documentation context.",
|
|
558
|
+
"Be concise and accurate. If the answer is not in the context, say so honestly.",
|
|
559
|
+
"Use markdown formatting for code examples and links.",
|
|
560
|
+
];
|
|
561
|
+
if (packageName) {
|
|
562
|
+
lines.push(`When showing import examples, always use "${packageName}" as the package name.`);
|
|
563
|
+
}
|
|
564
|
+
if (docsUrl) {
|
|
565
|
+
lines.push(`When linking to documentation pages, use "${docsUrl}" as the base URL (e.g. ${docsUrl}/docs/get-started).`);
|
|
566
|
+
}
|
|
567
|
+
return lines.join(" ");
|
|
568
|
+
}
|
|
569
|
+
const DEFAULT_SYSTEM_PROMPT = buildDefaultSystemPrompt();
|
|
570
|
+
async function POST(context) {
|
|
571
|
+
if (!aiConfig.enabled) {
|
|
572
|
+
return new Response(JSON.stringify({
|
|
573
|
+
error: "AI is not enabled. Set `ai: { enabled: true }` in your docs config to enable it.",
|
|
574
|
+
}), { status: 404, headers: { "Content-Type": "application/json" } });
|
|
575
|
+
}
|
|
576
|
+
const resolvedKey = aiConfig.apiKey ?? (typeof process !== "undefined" ? process.env?.OPENAI_API_KEY : undefined);
|
|
577
|
+
if (!resolvedKey) {
|
|
578
|
+
return new Response(JSON.stringify({
|
|
579
|
+
error: "AI is enabled but no API key was found. Set `apiKey` in your docs config `ai` section or add OPENAI_API_KEY to your environment.",
|
|
580
|
+
}), { status: 500, headers: { "Content-Type": "application/json" } });
|
|
581
|
+
}
|
|
582
|
+
const ctx = resolveContextFromRequest(context.request);
|
|
583
|
+
let body;
|
|
584
|
+
try {
|
|
585
|
+
body = await context.request.json();
|
|
586
|
+
}
|
|
587
|
+
catch {
|
|
588
|
+
return new Response(JSON.stringify({ error: "Invalid JSON body. Expected { messages: [...] }" }), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
589
|
+
}
|
|
590
|
+
const messages = body.messages;
|
|
591
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
592
|
+
return new Response(JSON.stringify({ error: "messages array is required and must not be empty." }), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
593
|
+
}
|
|
594
|
+
const lastUserMessage = [...messages].reverse().find((m) => m.role === "user");
|
|
595
|
+
if (!lastUserMessage) {
|
|
596
|
+
return new Response(JSON.stringify({ error: "At least one user message is required." }), {
|
|
597
|
+
status: 400,
|
|
598
|
+
headers: { "Content-Type": "application/json" },
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
const maxResults = aiConfig.maxResults ?? 5;
|
|
602
|
+
const scored = searchByQuery(lastUserMessage.content.toLowerCase(), ctx).slice(0, maxResults);
|
|
603
|
+
const contextParts = scored.map((doc) => `## ${doc.title}\nURL: ${doc.url}\n${doc.description ? `Description: ${doc.description}\n` : ""}\n${doc.content}`);
|
|
604
|
+
const ragContext = contextParts.join("\n\n---\n\n");
|
|
605
|
+
const systemPrompt = aiConfig.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
|
|
606
|
+
const systemMessage = {
|
|
607
|
+
role: "system",
|
|
608
|
+
content: ragContext
|
|
609
|
+
? `${systemPrompt}\n\n---\n\nDocumentation context:\n\n${ragContext}`
|
|
610
|
+
: systemPrompt,
|
|
611
|
+
};
|
|
612
|
+
const llmMessages = [
|
|
613
|
+
systemMessage,
|
|
614
|
+
...messages.filter((m) => m.role !== "system"),
|
|
615
|
+
];
|
|
616
|
+
const requestedModel = typeof body.model === "string" && body.model.trim().length > 0
|
|
617
|
+
? body.model.trim()
|
|
618
|
+
: undefined;
|
|
619
|
+
const resolved = resolveAIModelAndProvider(aiConfig, requestedModel);
|
|
620
|
+
const finalKey = resolved.apiKey ?? resolvedKey;
|
|
621
|
+
const llmResponse = await fetch(`${resolved.baseUrl}/chat/completions`, {
|
|
622
|
+
method: "POST",
|
|
623
|
+
headers: {
|
|
624
|
+
"Content-Type": "application/json",
|
|
625
|
+
Authorization: `Bearer ${finalKey}`,
|
|
626
|
+
},
|
|
627
|
+
body: JSON.stringify({ model: resolved.model, stream: true, messages: llmMessages }),
|
|
628
|
+
});
|
|
629
|
+
if (!llmResponse.ok) {
|
|
630
|
+
const errText = await llmResponse.text().catch(() => "Unknown error");
|
|
631
|
+
return new Response(JSON.stringify({ error: `LLM API error (${llmResponse.status}): ${errText}` }), { status: 502, headers: { "Content-Type": "application/json" } });
|
|
632
|
+
}
|
|
633
|
+
return new Response(llmResponse.body, {
|
|
634
|
+
headers: {
|
|
635
|
+
"Content-Type": "text/event-stream",
|
|
636
|
+
"Cache-Control": "no-cache",
|
|
637
|
+
Connection: "keep-alive",
|
|
638
|
+
},
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
return { load, GET, POST };
|
|
642
|
+
}
|
|
643
|
+
// ─── Nuxt event handler helper ───────────────────────────────
|
|
644
|
+
/**
|
|
645
|
+
* Create a single Nuxt event handler that serves docs pages, search, and AI chat.
|
|
646
|
+
*
|
|
647
|
+
* Pass `useStorage` from the Nitro auto-import so the handler can read
|
|
648
|
+
* docs bundled via `serverAssets`.
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```ts
|
|
652
|
+
* // server/api/docs.ts
|
|
653
|
+
* import { defineDocsHandler } from "@farming-labs/nuxt/server";
|
|
654
|
+
* import config from "../../docs.config";
|
|
655
|
+
* export default defineDocsHandler(config, useStorage);
|
|
656
|
+
* ```
|
|
657
|
+
*
|
|
658
|
+
* The handler responds to:
|
|
659
|
+
* - `GET /api/docs?pathname=/docs/page` → page load
|
|
660
|
+
* - `GET /api/docs?query=search+term` → search
|
|
661
|
+
* - `POST /api/docs` → AI chat
|
|
662
|
+
*/
|
|
663
|
+
export function defineDocsHandler(config, storage) {
|
|
664
|
+
let _server = null;
|
|
665
|
+
let _initPromise = null;
|
|
666
|
+
async function getServer() {
|
|
667
|
+
if (_server)
|
|
668
|
+
return _server;
|
|
669
|
+
if (_initPromise)
|
|
670
|
+
return _initPromise;
|
|
671
|
+
_initPromise = (async () => {
|
|
672
|
+
const entry = config.entry ?? config.contentDir ?? "docs";
|
|
673
|
+
const contentDirRel = config.contentDir ?? entry;
|
|
674
|
+
const store = storage(`assets:${contentDirRel}`);
|
|
675
|
+
const keys = await store.getKeys();
|
|
676
|
+
const contentFiles = {};
|
|
677
|
+
for (const key of keys) {
|
|
678
|
+
if (!key.endsWith(".md") && !key.endsWith(".mdx"))
|
|
679
|
+
continue;
|
|
680
|
+
const raw = await store.getItem(key);
|
|
681
|
+
if (typeof raw === "string") {
|
|
682
|
+
const filePath = `/${entry}/${key.replace(/:/g, "/")}`;
|
|
683
|
+
contentFiles[filePath] = raw;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
_server = createDocsServer({
|
|
687
|
+
...config,
|
|
688
|
+
...(Object.keys(contentFiles).length > 0 ? { _preloadedContent: contentFiles } : {}),
|
|
689
|
+
});
|
|
690
|
+
return _server;
|
|
691
|
+
})();
|
|
692
|
+
return _initPromise;
|
|
693
|
+
}
|
|
694
|
+
return async (event) => {
|
|
695
|
+
const server = await getServer();
|
|
696
|
+
const method = event.method ?? event.node?.req?.method ?? "GET";
|
|
697
|
+
const headers = event.headers ?? event.node?.req?.headers ?? {};
|
|
698
|
+
if (method === "POST") {
|
|
699
|
+
const url = new URL(event.node.req.url ?? "/", "http://localhost");
|
|
700
|
+
let body;
|
|
701
|
+
try {
|
|
702
|
+
body = await new Promise((resolve, reject) => {
|
|
703
|
+
let data = "";
|
|
704
|
+
event.node.req.on("data", (chunk) => (data += chunk));
|
|
705
|
+
event.node.req.on("end", () => resolve(data));
|
|
706
|
+
event.node.req.on("error", reject);
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
catch {
|
|
710
|
+
/* empty */
|
|
711
|
+
}
|
|
712
|
+
return server.POST({
|
|
713
|
+
request: new Request(url.href, { method: "POST", headers, body }),
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
const reqUrl = new URL(event.node.req.url ?? "/", "http://localhost");
|
|
717
|
+
const pathname = reqUrl.searchParams.get("pathname");
|
|
718
|
+
if (pathname) {
|
|
719
|
+
return server.load(pathname);
|
|
720
|
+
}
|
|
721
|
+
return server.GET({
|
|
722
|
+
request: new Request(reqUrl.href, { method: "GET", headers }),
|
|
723
|
+
});
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
//# sourceMappingURL=server.js.map
|