@farming-labs/theme 0.1.1-beta.2 → 0.1.1
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/docs-api.d.mts +22 -2
- package/dist/docs-api.mjs +220 -1
- package/package.json +2 -2
package/dist/docs-api.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DocsI18nConfig } from "@farming-labs/docs";
|
|
1
|
+
import { DocsI18nConfig, DocsMcpConfig, OrderingItem } from "@farming-labs/docs";
|
|
2
2
|
|
|
3
3
|
//#region src/docs-api.d.ts
|
|
4
4
|
interface AIProviderConfig {
|
|
@@ -35,6 +35,16 @@ interface DocsAPIOptions {
|
|
|
35
35
|
/** i18n config (optional) */
|
|
36
36
|
i18n?: DocsI18nConfig;
|
|
37
37
|
}
|
|
38
|
+
interface DocsMCPAPIOptions {
|
|
39
|
+
rootDir?: string;
|
|
40
|
+
entry?: string;
|
|
41
|
+
contentDir?: string;
|
|
42
|
+
nav?: {
|
|
43
|
+
title?: unknown;
|
|
44
|
+
};
|
|
45
|
+
ordering?: "alphabetical" | "numeric" | OrderingItem[];
|
|
46
|
+
mcp?: boolean | DocsMcpConfig;
|
|
47
|
+
}
|
|
38
48
|
/**
|
|
39
49
|
* Create a unified docs API route handler.
|
|
40
50
|
*
|
|
@@ -66,5 +76,15 @@ declare function createDocsAPI(options?: DocsAPIOptions): {
|
|
|
66
76
|
*/
|
|
67
77
|
POST(request: Request): Promise<Response>;
|
|
68
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* Create MCP route handlers for `/api/docs/mcp`.
|
|
81
|
+
*
|
|
82
|
+
* Returns `{ GET, POST, DELETE }` for use in a Next.js route handler.
|
|
83
|
+
*/
|
|
84
|
+
declare function createDocsMCPAPI(options?: DocsMCPAPIOptions): {
|
|
85
|
+
GET(request: Request): Promise<Response>;
|
|
86
|
+
POST(request: Request): Promise<Response>;
|
|
87
|
+
DELETE(request: Request): Promise<Response>;
|
|
88
|
+
};
|
|
69
89
|
//#endregion
|
|
70
|
-
export { createDocsAPI };
|
|
90
|
+
export { createDocsAPI, createDocsMCPAPI };
|
package/dist/docs-api.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import fs from "node:fs";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import matter from "gray-matter";
|
|
7
7
|
import { createSearchAPI } from "fumadocs-core/search/server";
|
|
8
|
+
import { createDocsMcpHttpHandler, createFilesystemDocsMcpSource } from "@farming-labs/docs/server";
|
|
8
9
|
|
|
9
10
|
//#region src/docs-api.ts
|
|
10
11
|
/**
|
|
@@ -90,6 +91,190 @@ function readAIConfig(root) {
|
|
|
90
91
|
}
|
|
91
92
|
return {};
|
|
92
93
|
}
|
|
94
|
+
function readMcpConfig(root) {
|
|
95
|
+
for (const ext of FILE_EXTS) {
|
|
96
|
+
const configPath = path.join(root, `docs.config.${ext}`);
|
|
97
|
+
if (!fs.existsSync(configPath)) continue;
|
|
98
|
+
try {
|
|
99
|
+
const content = fs.readFileSync(configPath, "utf-8");
|
|
100
|
+
const sanitized = stripCommentsAndStrings(content);
|
|
101
|
+
const configObject = extractRootConfigObject(content, sanitized);
|
|
102
|
+
const scopedContent = configObject?.content ?? content;
|
|
103
|
+
const scopedSanitized = configObject?.sanitized ?? sanitized;
|
|
104
|
+
const booleanValue = readTopLevelBoolean(scopedSanitized, "mcp");
|
|
105
|
+
if (booleanValue !== void 0) return booleanValue;
|
|
106
|
+
const block = extractObjectLiteral(scopedContent, scopedSanitized, "mcp");
|
|
107
|
+
if (!block) continue;
|
|
108
|
+
return {
|
|
109
|
+
enabled: readBooleanFromBlock(block, "enabled"),
|
|
110
|
+
route: readStringFromBlock(block, "route"),
|
|
111
|
+
name: readStringFromBlock(block, "name"),
|
|
112
|
+
version: readStringFromBlock(block, "version"),
|
|
113
|
+
tools: {
|
|
114
|
+
listPages: readBooleanFromBlock(block, "listPages"),
|
|
115
|
+
readPage: readBooleanFromBlock(block, "readPage"),
|
|
116
|
+
searchDocs: readBooleanFromBlock(block, "searchDocs"),
|
|
117
|
+
getNavigation: readBooleanFromBlock(block, "getNavigation")
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
} catch {}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function readStringFromBlock(block, key) {
|
|
124
|
+
return block.match(new RegExp(`${key}\\s*:\\s*["']([^"']+)["']`))?.[1];
|
|
125
|
+
}
|
|
126
|
+
function readBooleanFromBlock(block, key) {
|
|
127
|
+
const match = block.match(new RegExp(`${key}\\s*:\\s*(true|false)`));
|
|
128
|
+
return match ? match[1] === "true" : void 0;
|
|
129
|
+
}
|
|
130
|
+
function extractObjectLiteral(content, sanitized, key) {
|
|
131
|
+
const keyIndex = findTopLevelPropertyIndex(sanitized, key);
|
|
132
|
+
if (keyIndex === -1) return void 0;
|
|
133
|
+
const colonIndex = sanitized.indexOf(":", keyIndex + key.length);
|
|
134
|
+
if (colonIndex === -1) return void 0;
|
|
135
|
+
const braceStart = sanitized.indexOf("{", colonIndex);
|
|
136
|
+
if (braceStart === -1) return void 0;
|
|
137
|
+
let depth = 0;
|
|
138
|
+
for (let index = braceStart; index < sanitized.length; index += 1) {
|
|
139
|
+
const char = sanitized[index];
|
|
140
|
+
if (char === "{") {
|
|
141
|
+
depth += 1;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (char !== "}") continue;
|
|
145
|
+
depth -= 1;
|
|
146
|
+
if (depth === 0) return content.slice(braceStart + 1, index);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function readTopLevelBoolean(content, key) {
|
|
150
|
+
const keyIndex = findTopLevelPropertyIndex(content, key);
|
|
151
|
+
if (keyIndex === -1) return void 0;
|
|
152
|
+
const match = content.slice(keyIndex + key.length).match(/^\s*:\s*(true|false)\b/);
|
|
153
|
+
if (!match) return void 0;
|
|
154
|
+
return match[1] === "true";
|
|
155
|
+
}
|
|
156
|
+
function findTopLevelPropertyIndex(content, key) {
|
|
157
|
+
let depth = 0;
|
|
158
|
+
for (let index = 0; index < content.length; index += 1) {
|
|
159
|
+
const char = content[index];
|
|
160
|
+
if (char === "{") {
|
|
161
|
+
depth += 1;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (char === "}") {
|
|
165
|
+
depth = Math.max(0, depth - 1);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (depth !== 0) continue;
|
|
169
|
+
if (!content.startsWith(key, index)) continue;
|
|
170
|
+
const previous = index === 0 ? "" : content[index - 1];
|
|
171
|
+
if (previous && /[\w$]/.test(previous)) continue;
|
|
172
|
+
const next = content[index + key.length] ?? "";
|
|
173
|
+
if (next && /[\w$]/.test(next)) continue;
|
|
174
|
+
if (!content.slice(index + key.length).match(/^\s*:/)) continue;
|
|
175
|
+
return index;
|
|
176
|
+
}
|
|
177
|
+
return -1;
|
|
178
|
+
}
|
|
179
|
+
function extractRootConfigObject(content, sanitized) {
|
|
180
|
+
const defineDocsIndex = sanitized.indexOf("defineDocs");
|
|
181
|
+
const exportDefaultIndex = sanitized.indexOf("export default");
|
|
182
|
+
let braceStart = -1;
|
|
183
|
+
if (defineDocsIndex !== -1) {
|
|
184
|
+
const parenIndex = sanitized.indexOf("(", defineDocsIndex);
|
|
185
|
+
if (parenIndex !== -1) braceStart = sanitized.indexOf("{", parenIndex);
|
|
186
|
+
}
|
|
187
|
+
if (braceStart === -1 && exportDefaultIndex !== -1) braceStart = sanitized.indexOf("{", exportDefaultIndex);
|
|
188
|
+
if (braceStart === -1) return void 0;
|
|
189
|
+
let depth = 0;
|
|
190
|
+
for (let index = braceStart; index < sanitized.length; index += 1) {
|
|
191
|
+
const char = sanitized[index];
|
|
192
|
+
if (char === "{") {
|
|
193
|
+
depth += 1;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (char !== "}") continue;
|
|
197
|
+
depth -= 1;
|
|
198
|
+
if (depth === 0) return {
|
|
199
|
+
content: content.slice(braceStart + 1, index),
|
|
200
|
+
sanitized: sanitized.slice(braceStart + 1, index)
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function stripCommentsAndStrings(content) {
|
|
205
|
+
let result = "";
|
|
206
|
+
let index = 0;
|
|
207
|
+
let state = "normal";
|
|
208
|
+
while (index < content.length) {
|
|
209
|
+
const char = content[index];
|
|
210
|
+
const next = content[index + 1];
|
|
211
|
+
if (state === "lineComment") {
|
|
212
|
+
if (char === "\n") {
|
|
213
|
+
state = "normal";
|
|
214
|
+
result += "\n";
|
|
215
|
+
} else result += " ";
|
|
216
|
+
index += 1;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (state === "blockComment") {
|
|
220
|
+
if (char === "*" && next === "/") {
|
|
221
|
+
result += " ";
|
|
222
|
+
index += 2;
|
|
223
|
+
state = "normal";
|
|
224
|
+
} else {
|
|
225
|
+
result += char === "\n" ? "\n" : " ";
|
|
226
|
+
index += 1;
|
|
227
|
+
}
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (state === "singleQuote" || state === "doubleQuote" || state === "template") {
|
|
231
|
+
const quote = state === "singleQuote" ? "'" : state === "doubleQuote" ? "\"" : "`";
|
|
232
|
+
if (char === "\\") {
|
|
233
|
+
result += " ";
|
|
234
|
+
if (next) result += next === "\n" ? "\n" : " ";
|
|
235
|
+
index += 2;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
result += char === "\n" ? "\n" : " ";
|
|
239
|
+
if (char === quote) state = "normal";
|
|
240
|
+
index += 1;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (char === "/" && next === "/") {
|
|
244
|
+
result += " ";
|
|
245
|
+
index += 2;
|
|
246
|
+
state = "lineComment";
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (char === "/" && next === "*") {
|
|
250
|
+
result += " ";
|
|
251
|
+
index += 2;
|
|
252
|
+
state = "blockComment";
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (char === "'") {
|
|
256
|
+
result += " ";
|
|
257
|
+
index += 1;
|
|
258
|
+
state = "singleQuote";
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (char === "\"") {
|
|
262
|
+
result += " ";
|
|
263
|
+
index += 1;
|
|
264
|
+
state = "doubleQuote";
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (char === "`") {
|
|
268
|
+
result += " ";
|
|
269
|
+
index += 1;
|
|
270
|
+
state = "template";
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
result += char;
|
|
274
|
+
index += 1;
|
|
275
|
+
}
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
93
278
|
function stripMdx(raw) {
|
|
94
279
|
const { content } = matter(raw);
|
|
95
280
|
return content.replace(/^(import|export)\s.*$/gm, "").replace(/<[^>]+\/>/g, "").replace(/<\/?[A-Z][^>]*>/g, "").replace(/<\/?[a-z][^>]*>/g, "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1").replace(/^#{1,6}\s+/gm, "").replace(/(\*{1,3}|_{1,3})(.*?)\1/g, "$2").replace(/```[\s\S]*?```/g, "").replace(/`([^`]+)`/g, "$1").replace(/^>\s+/gm, "").replace(/^[-*_]{3,}\s*$/gm, "").replace(/\n{3,}/g, "\n\n").trim();
|
|
@@ -353,6 +538,40 @@ function createDocsAPI(options) {
|
|
|
353
538
|
}
|
|
354
539
|
};
|
|
355
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
* Create MCP route handlers for `/api/docs/mcp`.
|
|
543
|
+
*
|
|
544
|
+
* Returns `{ GET, POST, DELETE }` for use in a Next.js route handler.
|
|
545
|
+
*/
|
|
546
|
+
function createDocsMCPAPI(options = {}) {
|
|
547
|
+
const rootDir = options.rootDir ?? process.cwd();
|
|
548
|
+
const entry = options.entry ?? readEntry(rootDir);
|
|
549
|
+
const appDir = getNextAppDir(rootDir);
|
|
550
|
+
const contentDir = options.contentDir ?? path.join(appDir, entry);
|
|
551
|
+
const navTitle = typeof options.nav?.title === "string" && options.nav.title.trim().length > 0 ? options.nav.title : "Documentation";
|
|
552
|
+
const handlers = createDocsMcpHttpHandler({
|
|
553
|
+
source: createFilesystemDocsMcpSource({
|
|
554
|
+
rootDir,
|
|
555
|
+
entry,
|
|
556
|
+
contentDir,
|
|
557
|
+
siteTitle: navTitle,
|
|
558
|
+
ordering: options.ordering
|
|
559
|
+
}),
|
|
560
|
+
mcp: options.mcp ?? readMcpConfig(rootDir),
|
|
561
|
+
defaultName: navTitle
|
|
562
|
+
});
|
|
563
|
+
return {
|
|
564
|
+
GET(request) {
|
|
565
|
+
return handlers.GET({ request });
|
|
566
|
+
},
|
|
567
|
+
POST(request) {
|
|
568
|
+
return handlers.POST({ request });
|
|
569
|
+
},
|
|
570
|
+
DELETE(request) {
|
|
571
|
+
return handlers.DELETE({ request });
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
}
|
|
356
575
|
|
|
357
576
|
//#endregion
|
|
358
|
-
export { createDocsAPI };
|
|
577
|
+
export { createDocsAPI, createDocsMCPAPI };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farming-labs/theme",
|
|
3
|
-
"version": "0.1.1
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Theme package for @farming-labs/docs — layout, provider, MDX components, and styles",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"docs",
|
|
@@ -127,7 +127,7 @@
|
|
|
127
127
|
"tsdown": "^0.20.3",
|
|
128
128
|
"typescript": "^5.9.3",
|
|
129
129
|
"vitest": "^3.2.4",
|
|
130
|
-
"@farming-labs/docs": "0.1.1
|
|
130
|
+
"@farming-labs/docs": "0.1.1"
|
|
131
131
|
},
|
|
132
132
|
"peerDependencies": {
|
|
133
133
|
"@farming-labs/docs": ">=0.0.1",
|