@farming-labs/docs 0.1.49 → 0.1.51

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.
@@ -1,311 +0,0 @@
1
- import matter from "gray-matter";
2
-
3
- //#region src/define-docs.ts
4
- /**
5
- * Define docs configuration. Validates and returns the config.
6
- */
7
- function defineDocs(config) {
8
- return {
9
- entry: config.entry ?? "docs",
10
- contentDir: config.contentDir,
11
- i18n: config.i18n,
12
- theme: config.theme,
13
- nav: config.nav,
14
- github: config.github,
15
- themeToggle: config.themeToggle,
16
- breadcrumb: config.breadcrumb,
17
- sidebar: config.sidebar,
18
- components: config.components,
19
- onCopyClick: config.onCopyClick,
20
- feedback: config.feedback,
21
- search: config.search,
22
- mcp: config.mcp,
23
- icons: config.icons,
24
- pageActions: config.pageActions,
25
- lastUpdated: config.lastUpdated,
26
- readingTime: config.readingTime,
27
- llmsTxt: config.llmsTxt,
28
- ai: config.ai,
29
- ordering: config.ordering,
30
- metadata: config.metadata,
31
- og: config.og,
32
- changelog: config.changelog,
33
- apiReference: config.apiReference,
34
- agent: config.agent
35
- };
36
- }
37
-
38
- //#endregion
39
- //#region src/changelog.ts
40
- function normalizePathSegment(value, fallback) {
41
- return (value ?? fallback).trim().replace(/^\/+|\/+$/g, "") || fallback;
42
- }
43
- function normalizeContentDir(value) {
44
- const trimmed = value?.trim();
45
- if (!trimmed) return "changelog";
46
- return trimmed.replace(/\/+$/, "") || "changelog";
47
- }
48
- function resolveChangelogConfig(value) {
49
- if (value === false || value === void 0) return {
50
- enabled: false,
51
- path: "changelog",
52
- contentDir: "changelog",
53
- title: "Changelog",
54
- description: void 0,
55
- search: true
56
- };
57
- if (value === true) return {
58
- enabled: true,
59
- path: "changelog",
60
- contentDir: "changelog",
61
- title: "Changelog",
62
- description: void 0,
63
- search: true
64
- };
65
- return {
66
- enabled: value.enabled !== false,
67
- path: normalizePathSegment(value.path, "changelog"),
68
- contentDir: normalizeContentDir(value.contentDir),
69
- title: value.title?.trim() || "Changelog",
70
- description: value.description?.trim() || void 0,
71
- search: value.search !== false,
72
- actionsComponent: value.actionsComponent
73
- };
74
- }
75
-
76
- //#endregion
77
- //#region src/utils.ts
78
- /**
79
- * Deep merge utility for theme overrides.
80
- * Merges objects recursively; later values override earlier ones.
81
- */
82
- function deepMerge(target, ...sources) {
83
- if (!sources.length) return target;
84
- const source = sources.shift();
85
- if (!source) return target;
86
- const result = { ...target };
87
- for (const key of Object.keys(source)) {
88
- const sourceVal = source[key];
89
- const targetVal = result[key];
90
- if (sourceVal && typeof sourceVal === "object" && !Array.isArray(sourceVal) && targetVal && typeof targetVal === "object" && !Array.isArray(targetVal)) result[key] = deepMerge(targetVal, sourceVal);
91
- else if (sourceVal !== void 0) result[key] = sourceVal;
92
- }
93
- if (sources.length) return deepMerge(result, ...sources);
94
- return result;
95
- }
96
-
97
- //#endregion
98
- //#region src/create-theme.ts
99
- /**
100
- * Create a theme preset factory.
101
- *
102
- * Returns a function that accepts optional overrides and deep-merges them
103
- * with the base theme defaults. This is the same pattern used by the
104
- * built-in `fumadocs()`, `darksharp()`, and `pixelBorder()` presets.
105
- *
106
- * @param baseTheme - The default theme configuration
107
- * @returns A factory function `(overrides?) => DocsTheme`
108
- *
109
- * @example
110
- * ```ts
111
- * import { createTheme } from "@farming-labs/docs";
112
- *
113
- * export const myTheme = createTheme({
114
- * name: "my-theme",
115
- * ui: {
116
- * colors: { primary: "#6366f1" },
117
- * layout: { contentWidth: 800 },
118
- * },
119
- * });
120
- * ```
121
- */
122
- function createTheme(baseTheme) {
123
- return function themeFactory(overrides = {}) {
124
- const merged = deepMerge(baseTheme, overrides);
125
- if (overrides.ui?.colors) merged._userColorOverrides = { ...overrides.ui.colors };
126
- return merged;
127
- };
128
- }
129
- /**
130
- * Extend an existing theme preset with additional defaults.
131
- *
132
- * Useful when you want to build on top of an existing theme (e.g. fumadocs)
133
- * rather than starting from scratch.
134
- *
135
- * @example
136
- * ```ts
137
- * import { extendTheme } from "@farming-labs/docs";
138
- * import { fumadocs } from "@farming-labs/theme/default";
139
- *
140
- * // Start with fumadocs defaults, override some values
141
- * export const myTheme = extendTheme(fumadocs(), {
142
- * name: "my-custom-fumadocs",
143
- * ui: { colors: { primary: "#22c55e" } },
144
- * });
145
- * ```
146
- */
147
- function extendTheme(baseTheme, extensions) {
148
- return deepMerge(baseTheme, extensions);
149
- }
150
-
151
- //#endregion
152
- //#region src/i18n.ts
153
- function normalizeSegment(value) {
154
- return value.replace(/^\/+|\/+$/g, "");
155
- }
156
- function splitSegments(value) {
157
- const cleaned = normalizeSegment(value);
158
- return cleaned ? cleaned.split("/").filter(Boolean) : [];
159
- }
160
- function resolveDocsI18n(config) {
161
- if (!config || !Array.isArray(config.locales)) return null;
162
- const locales = Array.from(new Set(config.locales.map((l) => l.trim()).filter(Boolean)));
163
- if (locales.length === 0) return null;
164
- return {
165
- locales,
166
- defaultLocale: config.defaultLocale && locales.includes(config.defaultLocale) ? config.defaultLocale : locales[0]
167
- };
168
- }
169
- function resolveDocsLocale(searchParams, i18n) {
170
- if (!i18n) return void 0;
171
- const raw = searchParams.get("lang") ?? searchParams.get("locale");
172
- if (!raw) return void 0;
173
- if (i18n.locales.includes(raw)) return raw;
174
- return i18n.defaultLocale;
175
- }
176
- function resolveDocsPath(pathname, entry) {
177
- const entryBase = normalizeSegment(entry || "docs") || "docs";
178
- const entryParts = splitSegments(entryBase);
179
- const pathParts = splitSegments(pathname);
180
- let rest = pathParts;
181
- if (entryParts.length > 0) {
182
- if (pathParts.slice(0, entryParts.length).join("/") === entryParts.join("/")) rest = pathParts.slice(entryParts.length);
183
- }
184
- return {
185
- slug: rest.join("/"),
186
- entryPath: entryBase
187
- };
188
- }
189
-
190
- //#endregion
191
- //#region src/metadata.ts
192
- /**
193
- * Resolve page title using metadata titleTemplate.
194
- * %s is replaced with page title.
195
- */
196
- function resolveTitle(pageTitle, metadata) {
197
- return (metadata?.titleTemplate ?? "%s").replace("%s", pageTitle);
198
- }
199
- /**
200
- * Resolve OG image URL for a page.
201
- * Prefers page.openGraph.images[0], then page.ogImage, then config endpoint/default.
202
- */
203
- function resolveOGImage(page, ogConfig, baseUrl) {
204
- if (page.openGraph?.images?.length) return resolveImageUrl(page.openGraph.images[0].url, baseUrl);
205
- if (!ogConfig?.enabled) return void 0;
206
- if (page.ogImage) return resolveImageUrl(page.ogImage, baseUrl);
207
- if (ogConfig.type === "dynamic" && ogConfig.endpoint) return `${baseUrl ?? ""}${ogConfig.endpoint}`;
208
- return ogConfig.defaultImage;
209
- }
210
- function resolveImageUrl(url, baseUrl) {
211
- if (url.startsWith("/") || url.startsWith("http")) return url;
212
- const base = baseUrl ?? "";
213
- return `${base}${base.length > 0 && !base.endsWith("/") ? "/" : ""}${url}`;
214
- }
215
- /**
216
- * Build the Open Graph metadata object for a page.
217
- * When the page has openGraph in frontmatter, uses it (with title/description filled from page if omitted).
218
- * Otherwise uses ogImage or config (dynamic endpoint / defaultImage).
219
- */
220
- function buildPageOpenGraph(page, ogConfig, baseUrl) {
221
- if (page.openGraph) {
222
- const images = page.openGraph.images?.length ? page.openGraph.images.map((img) => ({
223
- url: resolveImageUrl(img.url, baseUrl),
224
- width: img.width ?? 1200,
225
- height: img.height ?? 630
226
- })) : void 0;
227
- return {
228
- title: page.openGraph.title ?? page.title,
229
- description: page.openGraph.description ?? page.description,
230
- ...images && { images }
231
- };
232
- }
233
- const url = resolveOGImage(page, ogConfig, baseUrl);
234
- if (!url) return void 0;
235
- return {
236
- title: page.title,
237
- ...page.description && { description: page.description },
238
- images: [{
239
- url,
240
- width: 1200,
241
- height: 630
242
- }]
243
- };
244
- }
245
- /**
246
- * Build the Twitter card metadata object for a page.
247
- * When the page has twitter in frontmatter, uses it.
248
- * Otherwise builds from ogImage or config (dynamic endpoint).
249
- */
250
- function buildPageTwitter(page, ogConfig, baseUrl) {
251
- if (page.twitter) {
252
- const images = page.twitter.images?.length ? page.twitter.images.map((url) => resolveImageUrl(url, baseUrl)) : void 0;
253
- return {
254
- ...page.twitter.card && { card: page.twitter.card },
255
- ...page.twitter.title !== void 0 && { title: page.twitter.title },
256
- ...page.twitter.description !== void 0 && { description: page.twitter.description },
257
- ...images && { images }
258
- };
259
- }
260
- const url = resolveOGImage(page, ogConfig, baseUrl);
261
- if (!url) return void 0;
262
- return {
263
- card: "summary_large_image",
264
- title: page.title,
265
- ...page.description && { description: page.description },
266
- images: [url]
267
- };
268
- }
269
-
270
- //#endregion
271
- //#region src/reading-time.ts
272
- function hasExplicitReadingTime(frontmatter) {
273
- return Object.prototype.hasOwnProperty.call(frontmatter ?? {}, "readingTime");
274
- }
275
- function normalizeWordsPerMinute(wordsPerMinute) {
276
- if (typeof wordsPerMinute !== "number" || !Number.isFinite(wordsPerMinute)) return 220;
277
- return Math.max(1, Math.floor(wordsPerMinute));
278
- }
279
- function stripNonReadingContent(content) {
280
- return content.replace(/(`{3,})[^\n]*\n[\s\S]*?\1/g, " ").replace(/(~{3,})[^\n]*\n[\s\S]*?\1/g, " ").replace(/`[^`\n]+`/g, " ").replace(/!\[[^\]]*\]\([^)]+\)/g, " ").replace(/\[([^\]]+)\]\([^)]+\)/g, " $1 ").replace(/<[^>]+>/g, " ").replace(/\{[^{}]*\}/g, " ").replace(/https?:\/\/\S+/g, " ").replace(/[#>*_~|]/g, " ");
281
- }
282
- function estimateReadingTimeMinutes(content, wordsPerMinute) {
283
- const wordCount = stripNonReadingContent(content).match(/\b[\p{L}\p{N}][\p{L}\p{N}'’-]*\b/gu)?.length ?? 0;
284
- return Math.max(1, Math.ceil(wordCount / normalizeWordsPerMinute(wordsPerMinute)));
285
- }
286
- function resolveReadingTimeOptions(readingTime) {
287
- if (readingTime === true) return { enabled: true };
288
- if (readingTime === false || readingTime === void 0 || readingTime === null) return { enabled: false };
289
- if (typeof readingTime !== "object") return { enabled: false };
290
- return {
291
- enabled: readingTime.enabled !== false,
292
- wordsPerMinute: typeof readingTime.wordsPerMinute === "number" && Number.isFinite(readingTime.wordsPerMinute) ? readingTime.wordsPerMinute : void 0
293
- };
294
- }
295
- function resolveReadingTimeFromContent(frontmatter, content, wordsPerMinute) {
296
- const pageData = frontmatter ?? {};
297
- if (pageData.readingTime === false) return null;
298
- if (typeof pageData.readingTime === "number" && Number.isFinite(pageData.readingTime)) return Math.max(1, Math.ceil(pageData.readingTime));
299
- return estimateReadingTimeMinutes(content, wordsPerMinute);
300
- }
301
- function resolvePageReadingTime(frontmatter, content, options) {
302
- if (!(options?.enabledByDefault ?? false) && !hasExplicitReadingTime(frontmatter)) return;
303
- return resolveReadingTimeFromContent(frontmatter, content, options?.wordsPerMinute);
304
- }
305
- function resolveReadingTimeFromSource(source, wordsPerMinute) {
306
- const { data, content } = matter(source);
307
- return resolveReadingTimeFromContent(data, content, wordsPerMinute);
308
- }
309
-
310
- //#endregion
311
- export { defineDocs as _, resolveReadingTimeOptions as a, resolveOGImage as c, resolveDocsLocale as d, resolveDocsPath as f, resolveChangelogConfig as g, deepMerge as h, resolveReadingTimeFromSource as i, resolveTitle as l, extendTheme as m, resolvePageReadingTime as n, buildPageOpenGraph as o, createTheme as p, resolveReadingTimeFromContent as r, buildPageTwitter as s, estimateReadingTimeMinutes as t, resolveDocsI18n as u };
File without changes