@farming-labs/svelte 0.0.38 → 0.0.44

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,276 @@
1
+ /**
2
+ * Server-side markdown rendering with Shiki syntax highlighting.
3
+ *
4
+ * Converts raw markdown content to HTML, supporting:
5
+ * - Fenced code blocks with dual-theme syntax highlighting
6
+ * - Copy-to-clipboard buttons on code blocks
7
+ * - Tabbed code blocks (`<Tabs>` / `<Tab>` syntax)
8
+ * - Callouts / admonitions (GitHub `[!NOTE]` and `**Note:**` styles)
9
+ * - Tables, lists, inline formatting, headings with anchor IDs
10
+ */
11
+ import { createHighlighter } from "shiki";
12
+ let highlighterPromise;
13
+ function getHighlighter() {
14
+ if (!highlighterPromise) {
15
+ highlighterPromise = createHighlighter({
16
+ themes: ["github-light", "github-dark"],
17
+ langs: [
18
+ "javascript",
19
+ "typescript",
20
+ "jsx",
21
+ "tsx",
22
+ "json",
23
+ "bash",
24
+ "shellscript",
25
+ "html",
26
+ "css",
27
+ "markdown",
28
+ "yaml",
29
+ "sql",
30
+ "python",
31
+ "dotenv",
32
+ ],
33
+ });
34
+ }
35
+ return highlighterPromise;
36
+ }
37
+ function slugify(text) {
38
+ return text
39
+ .toLowerCase()
40
+ .replace(/<[^>]+>/g, "")
41
+ .replace(/[^\w\s-]/g, "")
42
+ .replace(/\s+/g, "-")
43
+ .replace(/-+/g, "-")
44
+ .trim();
45
+ }
46
+ const calloutIcons = {
47
+ note: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>',
48
+ warning: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>',
49
+ tip: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 18h6"/><path d="M10 22h4"/><path d="M15.09 14c.18-.98.65-1.74 1.41-2.5A4.65 4.65 0 0018 8 6 6 0 006 8c0 1 .23 2.23 1.5 3.5A4.61 4.61 0 019 14"/></svg>',
50
+ important: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>',
51
+ caution: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>',
52
+ };
53
+ function renderCallout(type, content) {
54
+ const icon = calloutIcons[type] || calloutIcons.note;
55
+ const label = type.charAt(0).toUpperCase() + type.slice(1);
56
+ return `<div class="fd-callout fd-callout-${type}" role="note"><div class="fd-callout-indicator" role="none"></div><div class="fd-callout-icon">${icon}</div><div class="fd-callout-content"><p class="fd-callout-title">${label}</p><p>${content}</p></div></div>`;
57
+ }
58
+ function highlightCode(hl, code, lang) {
59
+ if (lang === "sh" || lang === "shell")
60
+ lang = "bash";
61
+ if (lang === "env")
62
+ lang = "dotenv";
63
+ const supported = hl.getLoadedLanguages();
64
+ if (!supported.includes(lang))
65
+ lang = "text";
66
+ const trimmedCode = code.replace(/\n$/, "");
67
+ try {
68
+ return {
69
+ html: hl.codeToHtml(trimmedCode, {
70
+ lang,
71
+ themes: { light: "github-light", dark: "github-dark" },
72
+ }),
73
+ raw: trimmedCode,
74
+ };
75
+ }
76
+ catch {
77
+ const escaped = trimmedCode.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
78
+ return {
79
+ html: `<pre class="shiki"><code>${escaped}</code></pre>`,
80
+ raw: trimmedCode,
81
+ };
82
+ }
83
+ }
84
+ function parseMeta(meta) {
85
+ const lang = (meta.split(/\s/)[0] || "text").toLowerCase();
86
+ const titleMatch = meta.match(/title=["']([^"']+)["']/);
87
+ return { lang, title: titleMatch ? titleMatch[1] : null };
88
+ }
89
+ function wrapCodeWithCopy(html, rawCode, title, language) {
90
+ const escapedRaw = rawCode
91
+ .replace(/&/g, "&amp;")
92
+ .replace(/"/g, "&quot;")
93
+ .replace(/</g, "&lt;")
94
+ .replace(/>/g, "&gt;");
95
+ const dataLang = language ? ` data-language="${String(language).replace(/"/g, "&quot;")}"` : "";
96
+ const copyBtn = `<button class="fd-copy-btn" data-code="${escapedRaw}" title="Copy code"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1"/></svg></button>`;
97
+ if (title) {
98
+ return `<div class="fd-codeblock fd-codeblock--titled"${dataLang}><div class="fd-codeblock-title"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/></svg><span class="fd-codeblock-title-text">${title}</span>${copyBtn}</div><div class="fd-codeblock-content">${html}</div></div>`;
99
+ }
100
+ return `<div class="fd-codeblock"${dataLang}>${copyBtn}<div class="fd-codeblock-content">${html}</div></div>`;
101
+ }
102
+ function dedentCode(raw) {
103
+ const lines = raw.replace(/\n$/, "").split("\n");
104
+ const indent = lines.reduce((min, l) => {
105
+ if (!l.trim())
106
+ return min;
107
+ const spaces = l.match(/^(\s*)/)?.[1].length ?? 0;
108
+ return Math.min(min, spaces);
109
+ }, Infinity);
110
+ if (indent > 0 && indent < Infinity) {
111
+ return lines.map((l) => l.slice(indent)).join("\n");
112
+ }
113
+ return raw;
114
+ }
115
+ /**
116
+ * Render a markdown string to HTML with full syntax highlighting,
117
+ * callouts, tables, tabs, and copy-to-clipboard support.
118
+ *
119
+ * Designed for server-side use in SvelteKit `+page.server` loaders.
120
+ */
121
+ export async function renderMarkdown(content) {
122
+ if (!content)
123
+ return "";
124
+ const hl = await getHighlighter();
125
+ let result = content;
126
+ // ── Tabs blocks: <Tabs items={[...]}> ... </Tabs> ──
127
+ const tabsBlocks = [];
128
+ result = result.replace(/<Tabs\s+items=\{?\[([^\]]+)\]\}?>([\s\S]*?)<\/Tabs>/g, (_, itemsStr, body) => {
129
+ const items = itemsStr.split(",").map((s) => s.trim().replace(/^["']|["']$/g, ""));
130
+ const panels = [];
131
+ const tabRegex = /<Tab\s+value=["']([^"']+)["']>([\s\S]*?)<\/Tab>/g;
132
+ let tabMatch;
133
+ while ((tabMatch = tabRegex.exec(body)) !== null) {
134
+ const tabValue = tabMatch[1];
135
+ const tabContent = tabMatch[2].trim();
136
+ const codeMatch = tabContent.match(/```([^\n]*)\n([\s\S]*?)```/);
137
+ if (codeMatch) {
138
+ const { lang, title } = parseMeta(codeMatch[1]);
139
+ const dedented = dedentCode(codeMatch[2]);
140
+ const { html, raw } = highlightCode(hl, dedented, lang);
141
+ panels.push({ value: tabValue, html: wrapCodeWithCopy(html, raw, title, lang) });
142
+ }
143
+ else {
144
+ panels.push({ value: tabValue, html: `<p>${tabContent}</p>` });
145
+ }
146
+ }
147
+ let tabsHtml = `<div class="fd-tabs" data-tabs>`;
148
+ tabsHtml += `<div class="fd-tabs-list" role="tablist">`;
149
+ for (let i = 0; i < items.length; i++) {
150
+ tabsHtml += `<button role="tab" class="fd-tab-trigger${i === 0 ? " fd-tab-active" : ""}" data-tab-value="${items[i]}" aria-selected="${i === 0}">${items[i]}</button>`;
151
+ }
152
+ tabsHtml += `</div>`;
153
+ for (let i = 0; i < panels.length; i++) {
154
+ tabsHtml += `<div class="fd-tab-panel${i === 0 ? " fd-tab-panel-active" : ""}" data-tab-panel="${panels[i].value}" role="tabpanel">${panels[i].html}</div>`;
155
+ }
156
+ tabsHtml += `</div>`;
157
+ const placeholder = `%%TABS_${tabsBlocks.length}%%`;
158
+ tabsBlocks.push(tabsHtml);
159
+ return placeholder;
160
+ });
161
+ // ── Fenced code blocks ──
162
+ const codeBlocks = [];
163
+ result = result.replace(/```([^\n]*)\n([\s\S]*?)```/g, (_, meta, code) => {
164
+ const { lang, title } = parseMeta(meta);
165
+ const { html, raw } = highlightCode(hl, code, lang);
166
+ const placeholder = `%%CODEBLOCK_${codeBlocks.length}%%`;
167
+ codeBlocks.push(wrapCodeWithCopy(html, raw, title, lang));
168
+ return placeholder;
169
+ });
170
+ // Inline code
171
+ result = result.replace(/`([^`]+)`/g, "<code>$1</code>");
172
+ // Headings (h4 → h1 order to avoid prefix collisions)
173
+ result = result.replace(/^#### (.+)$/gm, (_, text) => {
174
+ return `<h4 id="${slugify(text)}">${text}</h4>`;
175
+ });
176
+ result = result.replace(/^### (.+)$/gm, (_, text) => {
177
+ return `<h3 id="${slugify(text)}">${text}</h3>`;
178
+ });
179
+ result = result.replace(/^## (.+)$/gm, (_, text) => {
180
+ return `<h2 id="${slugify(text)}">${text}</h2>`;
181
+ });
182
+ result = result.replace(/^# (.+)$/gm, "<h1>$1</h1>");
183
+ // ── Callouts / blockquotes (before inline formatting) ──
184
+ const calloutBlocks = [];
185
+ result = result.replace(/(?:^>\s*.+\n?)+/gm, (block) => {
186
+ const lines = block.split("\n").filter(Boolean);
187
+ const inner = lines.map((l) => l.replace(/^>\s?/, "")).join("\n");
188
+ const ghMatch = inner.match(/^\[!(NOTE|WARNING|TIP|IMPORTANT|CAUTION)\]\s*\n?([\s\S]*)/i);
189
+ if (ghMatch) {
190
+ const type = ghMatch[1].toLowerCase();
191
+ const calloutContent = ghMatch[2].trim();
192
+ const placeholder = `%%CALLOUT_${calloutBlocks.length}%%`;
193
+ calloutBlocks.push(renderCallout(type, calloutContent));
194
+ return placeholder;
195
+ }
196
+ const boldMatch = inner.match(/^\*\*(Note|Warning|Tip|Important|Caution):\*\*\s*([\s\S]*)/i);
197
+ if (boldMatch) {
198
+ const type = boldMatch[1].toLowerCase();
199
+ const calloutContent = boldMatch[2].trim();
200
+ const placeholder = `%%CALLOUT_${calloutBlocks.length}%%`;
201
+ calloutBlocks.push(renderCallout(type, calloutContent));
202
+ return placeholder;
203
+ }
204
+ return `<blockquote><p>${inner}</p></blockquote>`;
205
+ });
206
+ // Inline formatting
207
+ result = result.replace(/\*\*\*(.+?)\*\*\*/g, "<strong><em>$1</em></strong>");
208
+ result = result.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
209
+ result = result.replace(/\*(.+?)\*/g, "<em>$1</em>");
210
+ // Links
211
+ result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
212
+ // Horizontal rules
213
+ result = result.replace(/^---$/gm, "<hr />");
214
+ // Tables
215
+ result = result.replace(/^\|(.+)\|\n\|[-| ]+\|\n((?:\|.+\|\n?)+)/gm, (_, headerRow, bodyRows) => {
216
+ const headers = headerRow
217
+ .split("|")
218
+ .map((h) => h.trim())
219
+ .filter(Boolean);
220
+ const rows = bodyRows
221
+ .trim()
222
+ .split("\n")
223
+ .map((row) => row
224
+ .split("|")
225
+ .map((c) => c.trim())
226
+ .filter(Boolean));
227
+ const headerHtml = headers.map((h) => `<th>${h}</th>`).join("");
228
+ const rowsHtml = rows
229
+ .map((row) => `<tr>${row.map((c) => `<td>${c}</td>`).join("")}</tr>`)
230
+ .join("");
231
+ return `<div class="fd-table-wrapper"><table><thead><tr>${headerHtml}</tr></thead><tbody>${rowsHtml}</tbody></table></div>`;
232
+ });
233
+ // Unordered lists
234
+ result = result.replace(/(?:^- .+\n?)+/gm, (block) => {
235
+ const items = block
236
+ .split("\n")
237
+ .filter((l) => l.startsWith("- "))
238
+ .map((l) => `<li>${l.slice(2)}</li>`)
239
+ .join("");
240
+ return `<ul>${items}</ul>`;
241
+ });
242
+ // Ordered lists
243
+ result = result.replace(/(?:^\d+\. .+\n?)+/gm, (block) => {
244
+ const items = block
245
+ .split("\n")
246
+ .filter((l) => /^\d+\. /.test(l))
247
+ .map((l) => `<li>${l.replace(/^\d+\. /, "")}</li>`)
248
+ .join("");
249
+ return `<ol>${items}</ol>`;
250
+ });
251
+ // Wrap remaining bare text in <p> tags
252
+ result = result
253
+ .split("\n\n")
254
+ .map((block) => {
255
+ block = block.trim();
256
+ if (!block)
257
+ return "";
258
+ if (/^<(h[1-6]|pre|ul|ol|blockquote|hr|table|div)/.test(block))
259
+ return block;
260
+ if (/^%%(CODEBLOCK|CALLOUT|TABS)_\d+%%$/.test(block))
261
+ return block;
262
+ return `<p>${block}</p>`;
263
+ })
264
+ .join("\n");
265
+ // Restore placeholders
266
+ for (let i = 0; i < codeBlocks.length; i++) {
267
+ result = result.replace(`%%CODEBLOCK_${i}%%`, codeBlocks[i]);
268
+ }
269
+ for (let i = 0; i < calloutBlocks.length; i++) {
270
+ result = result.replace(`%%CALLOUT_${i}%%`, calloutBlocks[i]);
271
+ }
272
+ for (let i = 0; i < tabsBlocks.length; i++) {
273
+ result = result.replace(`%%TABS_${i}%%`, tabsBlocks[i]);
274
+ }
275
+ return result;
276
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Server-side helpers for SvelteKit docs routes.
3
+ *
4
+ * `createDocsServer(config)` returns all the load functions and
5
+ * handlers needed for a complete docs site. Each route file becomes
6
+ * a one-line re-export.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // src/lib/docs.server.ts
11
+ * import { createDocsServer } from "@farming-labs/svelte/server";
12
+ * import config from "./docs.config";
13
+ *
14
+ * // Bundle content at build time for serverless deployments
15
+ * const contentFiles = import.meta.glob("/docs/**\/*.{md,mdx,svx}", {
16
+ * query: "?raw", import: "default", eager: true,
17
+ * }) as Record<string, string>;
18
+ *
19
+ * export const { load, GET, POST } = createDocsServer({
20
+ * ...config,
21
+ * _preloadedContent: contentFiles,
22
+ * });
23
+ * ```
24
+ *
25
+ * ```ts
26
+ * // routes/docs/+layout.server.js
27
+ * export { load } from "$lib/docs.server";
28
+ * ```
29
+ */
30
+ import { loadDocsNavTree } from "./content.js";
31
+ import type { PageNode } from "./content.js";
32
+ export { createSvelteApiReference } from "./api-reference.js";
33
+ interface UnifiedLoadEvent {
34
+ url: URL;
35
+ }
36
+ interface RequestEvent {
37
+ url: URL;
38
+ request: Request;
39
+ }
40
+ export interface DocsServer {
41
+ load: (event: UnifiedLoadEvent) => Promise<{
42
+ tree: ReturnType<typeof loadDocsNavTree>;
43
+ flatPages: PageNode[];
44
+ title: string;
45
+ description?: string;
46
+ html: string;
47
+ entry?: string;
48
+ locale?: string;
49
+ slug?: string;
50
+ previousPage: PageNode | null;
51
+ nextPage: PageNode | null;
52
+ editOnGithub?: string;
53
+ lastModified: string;
54
+ }>;
55
+ GET: (event: RequestEvent) => Response;
56
+ POST: (event: RequestEvent) => Promise<Response>;
57
+ }
58
+ /**
59
+ * Create all server-side functions needed for a SvelteKit docs site.
60
+ *
61
+ * @param config - The `DocsConfig` object (from `defineDocs()` in `docs.config.ts`).
62
+ *
63
+ * Pass `_preloadedContent` (from `import.meta.glob`) to bundle markdown files
64
+ * at build time — required for serverless deployments (Vercel, Netlify, etc.)
65
+ * where the filesystem is not available at runtime.
66
+ */
67
+ export declare function createDocsServer(config?: Record<string, any>): DocsServer;