@farming-labs/docs 0.1.40 → 0.1.42

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.
@@ -0,0 +1,270 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { join, resolve } from "node:path";
3
+
4
+ //#region src/cli/config.ts
5
+ const FILE_EXTS = [
6
+ "tsx",
7
+ "ts",
8
+ "jsx",
9
+ "js"
10
+ ];
11
+ function escapeRegExp(value) {
12
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
13
+ }
14
+ function createPropertyPattern(key, valuePattern) {
15
+ return new RegExp(`\\b${escapeRegExp(key)}\\b\\s*:\\s*${valuePattern}`);
16
+ }
17
+ function findBalancedBraceEnd(content, braceStart) {
18
+ let depth = 0;
19
+ let stringQuote = null;
20
+ let escaped = false;
21
+ let lineComment = false;
22
+ let blockComment = false;
23
+ for (let index = braceStart; index < content.length; index += 1) {
24
+ const char = content[index];
25
+ const next = content[index + 1];
26
+ if (lineComment) {
27
+ if (char === "\n") lineComment = false;
28
+ continue;
29
+ }
30
+ if (blockComment) {
31
+ if (char === "*" && next === "/") {
32
+ blockComment = false;
33
+ index += 1;
34
+ }
35
+ continue;
36
+ }
37
+ if (stringQuote) {
38
+ if (escaped) {
39
+ escaped = false;
40
+ continue;
41
+ }
42
+ if (char === "\\") {
43
+ escaped = true;
44
+ continue;
45
+ }
46
+ if (char === stringQuote) stringQuote = null;
47
+ continue;
48
+ }
49
+ if (char === "/" && next === "/") {
50
+ lineComment = true;
51
+ index += 1;
52
+ continue;
53
+ }
54
+ if (char === "/" && next === "*") {
55
+ blockComment = true;
56
+ index += 1;
57
+ continue;
58
+ }
59
+ if (char === "\"" || char === "'" || char === "`") {
60
+ stringQuote = char;
61
+ continue;
62
+ }
63
+ if (char === "{") {
64
+ depth += 1;
65
+ continue;
66
+ }
67
+ if (char !== "}") continue;
68
+ depth -= 1;
69
+ if (depth === 0) return index;
70
+ }
71
+ return -1;
72
+ }
73
+ function resolveDocsConfigPath(rootDir, explicitPath) {
74
+ if (explicitPath) {
75
+ const resolvedPath = resolve(rootDir, explicitPath);
76
+ if (!existsSync(resolvedPath)) throw new Error(`Could not find docs config at ${explicitPath}.`);
77
+ return resolvedPath;
78
+ }
79
+ for (const ext of FILE_EXTS) {
80
+ const configPath = join(rootDir, `docs.config.${ext}`);
81
+ if (existsSync(configPath)) return configPath;
82
+ }
83
+ throw new Error("Could not find docs.config.ts or docs.config.tsx in the current project. Use --config to point at a custom path.");
84
+ }
85
+ function readStringProperty(content, key) {
86
+ return content.match(createPropertyPattern(key, `["']([^"']+)["']`))?.[1];
87
+ }
88
+ function readEnvReferenceProperty(content, key) {
89
+ const patterns = [
90
+ createPropertyPattern(key, `process\\.env\\.([A-Za-z_$][\\w$]*)`),
91
+ createPropertyPattern(key, `process\\.env\\[['"]([^"'\\]]+)['"]\\]`),
92
+ createPropertyPattern(key, `import\\.meta\\.env\\.([A-Za-z_$][\\w$]*)`),
93
+ createPropertyPattern(key, `import\\.meta\\.env\\[['"]([^"'\\]]+)['"]\\]`)
94
+ ];
95
+ for (const pattern of patterns) {
96
+ const match = content.match(pattern);
97
+ if (match?.[1]) return match[1];
98
+ }
99
+ }
100
+ function readBooleanProperty(content, key) {
101
+ const match = content.match(createPropertyPattern(key, "(true|false)"));
102
+ return match ? match[1] === "true" : void 0;
103
+ }
104
+ function readNumberProperty(content, key) {
105
+ const match = content.match(createPropertyPattern(key, "(-?\\d+(?:\\.\\d+)?)"));
106
+ if (!match) return void 0;
107
+ const parsed = Number.parseFloat(match[1]);
108
+ return Number.isFinite(parsed) ? parsed : void 0;
109
+ }
110
+ function extractObjectLiteral(content, key) {
111
+ const keyIndex = content.search(new RegExp(`${key}\\s*:\\s*\\{`));
112
+ if (keyIndex === -1) return void 0;
113
+ const braceStart = content.indexOf("{", keyIndex);
114
+ if (braceStart === -1) return void 0;
115
+ const braceEnd = findBalancedBraceEnd(content, braceStart);
116
+ return braceEnd === -1 ? void 0 : content.slice(braceStart + 1, braceEnd);
117
+ }
118
+ function extractTopLevelConfigObject(content) {
119
+ for (const marker of ["defineDocs(", "export default"]) {
120
+ const markerIndex = content.indexOf(marker);
121
+ if (markerIndex === -1) continue;
122
+ const braceStart = content.indexOf("{", markerIndex);
123
+ if (braceStart === -1) continue;
124
+ const braceEnd = findBalancedBraceEnd(content, braceStart);
125
+ if (braceEnd !== -1) return content.slice(braceStart + 1, braceEnd);
126
+ }
127
+ }
128
+ function extractNestedObjectLiteral(content, keys) {
129
+ if (keys.length === 0) return void 0;
130
+ let current = extractTopLevelConfigObject(content) ?? content;
131
+ for (const key of keys) {
132
+ const next = extractObjectLiteral(current, key);
133
+ if (!next) return void 0;
134
+ current = next;
135
+ }
136
+ return current;
137
+ }
138
+ function splitTopLevelProperties(content) {
139
+ const properties = [];
140
+ let start = 0;
141
+ let stringQuote = null;
142
+ let escaped = false;
143
+ let braceDepth = 0;
144
+ let bracketDepth = 0;
145
+ let parenDepth = 0;
146
+ for (let index = 0; index < content.length; index += 1) {
147
+ const char = content[index];
148
+ if (stringQuote) {
149
+ if (escaped) {
150
+ escaped = false;
151
+ continue;
152
+ }
153
+ if (char === "\\") {
154
+ escaped = true;
155
+ continue;
156
+ }
157
+ if (char === stringQuote) stringQuote = null;
158
+ continue;
159
+ }
160
+ if (char === "\"" || char === "'" || char === "`") {
161
+ stringQuote = char;
162
+ continue;
163
+ }
164
+ if (char === "{") {
165
+ braceDepth += 1;
166
+ continue;
167
+ }
168
+ if (char === "}") {
169
+ braceDepth = Math.max(0, braceDepth - 1);
170
+ continue;
171
+ }
172
+ if (char === "[") {
173
+ bracketDepth += 1;
174
+ continue;
175
+ }
176
+ if (char === "]") {
177
+ bracketDepth = Math.max(0, bracketDepth - 1);
178
+ continue;
179
+ }
180
+ if (char === "(") {
181
+ parenDepth += 1;
182
+ continue;
183
+ }
184
+ if (char === ")") {
185
+ parenDepth = Math.max(0, parenDepth - 1);
186
+ continue;
187
+ }
188
+ if (char === "," && braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) {
189
+ properties.push(content.slice(start, index));
190
+ start = index + 1;
191
+ }
192
+ }
193
+ const trailing = content.slice(start);
194
+ if (trailing.trim().length > 0) properties.push(trailing);
195
+ return properties;
196
+ }
197
+ function readTopLevelStringProperty(content, key) {
198
+ const source = extractTopLevelConfigObject(content) ?? content;
199
+ const propertyPattern = new RegExp(`^\\s*${escapeRegExp(key)}\\s*:\\s*["']([^"']+)["']`);
200
+ for (const property of splitTopLevelProperties(source)) {
201
+ const match = property.trim().match(propertyPattern);
202
+ if (match) return match[1];
203
+ }
204
+ }
205
+ function readNavTitle(content) {
206
+ const block = extractObjectLiteral(extractTopLevelConfigObject(content) ?? content, "nav");
207
+ if (!block) return void 0;
208
+ return readStringProperty(block, "title");
209
+ }
210
+ function resolveDocsContentDir(rootDir, content, entry) {
211
+ const configuredContentDir = readTopLevelStringProperty(content, "contentDir");
212
+ if (configuredContentDir) return configuredContentDir;
213
+ const candidates = [
214
+ entry,
215
+ join("app", entry),
216
+ join("src", "app", entry)
217
+ ];
218
+ for (const candidate of candidates) if (existsSync(join(rootDir, candidate))) return candidate;
219
+ return entry;
220
+ }
221
+ function parseEnvValue(rawValue) {
222
+ const value = rawValue.trim();
223
+ if (value.startsWith("\"") && value.endsWith("\"") || value.startsWith("'") && value.endsWith("'")) return value.slice(1, -1);
224
+ return value;
225
+ }
226
+ function loadProjectEnv(rootDir) {
227
+ const env = {};
228
+ for (const filename of [".env", ".env.local"]) {
229
+ const fullPath = join(rootDir, filename);
230
+ if (!existsSync(fullPath)) continue;
231
+ const lines = readFileSync(fullPath, "utf-8").split(/\r?\n/);
232
+ for (const line of lines) {
233
+ const trimmed = line.trim();
234
+ if (!trimmed || trimmed.startsWith("#")) continue;
235
+ const equalsIndex = trimmed.indexOf("=");
236
+ if (equalsIndex === -1) continue;
237
+ const key = trimmed.slice(0, equalsIndex).trim();
238
+ const rawValue = trimmed.slice(equalsIndex + 1);
239
+ if (!key) continue;
240
+ env[key] = parseEnvValue(rawValue);
241
+ }
242
+ }
243
+ return env;
244
+ }
245
+ async function loadDocsConfigModule(rootDir, explicitPath) {
246
+ const configPath = resolveDocsConfigPath(rootDir, explicitPath);
247
+ try {
248
+ const { createJiti } = await import("jiti");
249
+ const loaded = await createJiti(import.meta.url, {
250
+ moduleCache: false,
251
+ fsCache: false,
252
+ interopDefault: true
253
+ }).import(configPath);
254
+ const config = loaded.default ?? loaded;
255
+ if (!config || typeof config !== "object") return null;
256
+ return {
257
+ path: configPath,
258
+ config
259
+ };
260
+ } catch (error) {
261
+ if (process.env.NODE_ENV !== "test") {
262
+ const message = error instanceof Error ? error.message : String(error);
263
+ console.warn(`[docs] Could not evaluate ${configPath} as a module; falling back to static parsing. ${message}`);
264
+ }
265
+ return null;
266
+ }
267
+ }
268
+
269
+ //#endregion
270
+ export { readBooleanProperty as a, readNumberProperty as c, resolveDocsConfigPath as d, resolveDocsContentDir as f, loadProjectEnv as i, readStringProperty as l, extractObjectLiteral as n, readEnvReferenceProperty as o, loadDocsConfigModule as r, readNavTitle as s, extractNestedObjectLiteral as t, readTopLevelStringProperty as u };
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { $ as SidebarFolderNode, A as DocsSearchQuery, B as McpDocsSearchConfig, C as DocsSearchAdapter, D as DocsSearchConfig, E as DocsSearchChunkingConfig, F as FeedbackConfig, G as OrderingItem, H as OpenDocsConfig, I as FontStyle, J as PageOpenGraph, K as PageActionsConfig, L as GithubConfig, M as DocsSearchResultType, N as DocsSearchSourcePage, O as DocsSearchDocument, P as DocsTheme, Q as SidebarConfig, R as LastUpdatedConfig, S as DocsRelatedItem, T as DocsSearchAdapterFactory, U as OpenDocsProvider, V as OGConfig, W as OpenGraphImage, X as ResolvedDocsRelatedLink, Y as PageTwitter, Z as SidebarComponentProps, _ as DocsI18nConfig, a as ApiReferenceRenderer, at as TypesenseDocsSearchConfig, b as DocsMetadata, c as ChangelogFrontmatter, d as CustomDocsSearchConfig, et as SidebarNode, f as DocsAgentFeedbackContext, g as DocsFeedbackValue, h as DocsFeedbackData, i as ApiReferenceConfig, it as ThemeToggleConfig, j as DocsSearchResult, k as DocsSearchEmbeddingsConfig, l as CodeBlockCopyData, m as DocsConfig, n as AgentFeedbackConfig, nt as SidebarTree, o as BreadcrumbConfig, ot as TypographyConfig, p as DocsAgentFeedbackData, q as PageFrontmatter, r as AlgoliaDocsSearchConfig, rt as SimpleDocsSearchConfig, s as ChangelogConfig, st as UIConfig, t as AIConfig, tt as SidebarPageNode, u as CopyMarkdownConfig, v as DocsMcpConfig, w as DocsSearchAdapterContext, x as DocsNav, y as DocsMcpToolsConfig, z as LlmsTxtConfig } from "./types-CP2NmW5c.mjs";
1
+ import { $ as SidebarFolderNode, A as DocsSearchQuery, B as McpDocsSearchConfig, C as DocsSearchAdapter, D as DocsSearchConfig, E as DocsSearchChunkingConfig, F as FeedbackConfig, G as OrderingItem, H as OpenDocsConfig, I as FontStyle, J as PageOpenGraph, K as PageActionsConfig, L as GithubConfig, M as DocsSearchResultType, N as DocsSearchSourcePage, O as DocsSearchDocument, P as DocsTheme, Q as SidebarConfig, R as LastUpdatedConfig, S as DocsRelatedItem, T as DocsSearchAdapterFactory, U as OpenDocsProvider, V as OGConfig, W as OpenGraphImage, X as ResolvedDocsRelatedLink, Y as PageTwitter, Z as SidebarComponentProps, _ as DocsI18nConfig, a as ApiReferenceRenderer, at as TypesenseDocsSearchConfig, b as DocsMetadata, c as ChangelogFrontmatter, d as CustomDocsSearchConfig, et as SidebarNode, f as DocsAgentFeedbackContext, g as DocsFeedbackValue, h as DocsFeedbackData, i as ApiReferenceConfig, it as ThemeToggleConfig, j as DocsSearchResult, k as DocsSearchEmbeddingsConfig, l as CodeBlockCopyData, m as DocsConfig, n as AgentFeedbackConfig, nt as SidebarTree, o as BreadcrumbConfig, ot as TypographyConfig, p as DocsAgentFeedbackData, q as PageFrontmatter, r as AlgoliaDocsSearchConfig, rt as SimpleDocsSearchConfig, s as ChangelogConfig, st as UIConfig, t as AIConfig, tt as SidebarPageNode, u as CopyMarkdownConfig, v as DocsMcpConfig, w as DocsSearchAdapterContext, x as DocsNav, y as DocsMcpToolsConfig, z as LlmsTxtConfig } from "./types-CKdSg7n8.mjs";
2
2
  import { DocsMcpPage, DocsMcpResolvedConfig } from "./mcp.mjs";
3
- import { a as createSimpleSearchAdapter, c as resolveSearchRequestConfig, i as createMcpSearchAdapter, n as createAlgoliaSearchAdapter, o as createTypesenseSearchAdapter, r as createCustomSearchAdapter, s as performDocsSearch, t as buildDocsSearchDocuments } from "./search-BWqFW9rt.mjs";
3
+ import { a as createSimpleSearchAdapter, c as resolveSearchRequestConfig, i as createMcpSearchAdapter, n as createAlgoliaSearchAdapter, o as createTypesenseSearchAdapter, r as createCustomSearchAdapter, s as performDocsSearch, t as buildDocsSearchDocuments } from "./search-C15yRNVE.mjs";
4
4
 
5
5
  //#region src/define-docs.d.ts
6
6
  /**