@farming-labs/svelte 0.0.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/config.d.ts +30 -0
- package/dist/config.js +26 -0
- package/dist/content.d.ts +52 -0
- package/dist/content.js +158 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/markdown.d.ts +17 -0
- package/dist/markdown.js +280 -0
- package/dist/server.d.ts +53 -0
- package/dist/server.js +250 -0
- package/package.json +63 -0
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mdsvex configuration helper for @farming-labs/docs.
|
|
3
|
+
*
|
|
4
|
+
* Returns a pre-configured mdsvex options object that handles
|
|
5
|
+
* frontmatter extraction and syntax highlighting.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```js
|
|
9
|
+
* // svelte.config.js
|
|
10
|
+
* import { createMdsvexConfig } from "@farming-labs/svelte/config";
|
|
11
|
+
*
|
|
12
|
+
* const mdsvexConfig = createMdsvexConfig();
|
|
13
|
+
*
|
|
14
|
+
* export default {
|
|
15
|
+
* extensions: [".svelte", ".svx"],
|
|
16
|
+
* preprocess: [mdsvex(mdsvexConfig)],
|
|
17
|
+
* };
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface MdsvexConfigOptions {
|
|
21
|
+
extensions?: string[];
|
|
22
|
+
highlighter?: unknown;
|
|
23
|
+
}
|
|
24
|
+
export declare function createMdsvexConfig(options?: MdsvexConfigOptions): {
|
|
25
|
+
extensions: string[];
|
|
26
|
+
smartypants: {
|
|
27
|
+
dashes: "oldschool";
|
|
28
|
+
};
|
|
29
|
+
layout: undefined;
|
|
30
|
+
};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mdsvex configuration helper for @farming-labs/docs.
|
|
3
|
+
*
|
|
4
|
+
* Returns a pre-configured mdsvex options object that handles
|
|
5
|
+
* frontmatter extraction and syntax highlighting.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```js
|
|
9
|
+
* // svelte.config.js
|
|
10
|
+
* import { createMdsvexConfig } from "@farming-labs/svelte/config";
|
|
11
|
+
*
|
|
12
|
+
* const mdsvexConfig = createMdsvexConfig();
|
|
13
|
+
*
|
|
14
|
+
* export default {
|
|
15
|
+
* extensions: [".svelte", ".svx"],
|
|
16
|
+
* preprocess: [mdsvex(mdsvexConfig)],
|
|
17
|
+
* };
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function createMdsvexConfig(options) {
|
|
21
|
+
return {
|
|
22
|
+
extensions: options?.extensions ?? [".svx", ".md"],
|
|
23
|
+
smartypants: { dashes: "oldschool" },
|
|
24
|
+
layout: undefined,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content loading utilities for SvelteKit docs.
|
|
3
|
+
*
|
|
4
|
+
* Scans the filesystem for `.md` / `.svx` content files,
|
|
5
|
+
* extracts frontmatter, and builds a navigation tree compatible
|
|
6
|
+
* with @farming-labs/docs DocsConfig.
|
|
7
|
+
*/
|
|
8
|
+
export interface PageNode {
|
|
9
|
+
type: "page";
|
|
10
|
+
name: string;
|
|
11
|
+
url: string;
|
|
12
|
+
icon?: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface FolderNode {
|
|
16
|
+
type: "folder";
|
|
17
|
+
name: string;
|
|
18
|
+
icon?: string;
|
|
19
|
+
index?: PageNode;
|
|
20
|
+
children: NavNode[];
|
|
21
|
+
}
|
|
22
|
+
export type NavNode = PageNode | FolderNode;
|
|
23
|
+
export interface NavTree {
|
|
24
|
+
name: string;
|
|
25
|
+
children: NavNode[];
|
|
26
|
+
}
|
|
27
|
+
export interface ContentPage {
|
|
28
|
+
slug: string;
|
|
29
|
+
url: string;
|
|
30
|
+
title: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
icon?: string;
|
|
33
|
+
content: string;
|
|
34
|
+
rawContent: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Scan a content directory and return all docs pages.
|
|
38
|
+
* Expects a flat or nested structure of `.md` files:
|
|
39
|
+
* content/docs/index.md → /docs
|
|
40
|
+
* content/docs/installation.md → /docs/installation
|
|
41
|
+
* content/docs/guides/auth.md → /docs/guides/auth
|
|
42
|
+
*/
|
|
43
|
+
export declare function loadDocsContent(contentDir: string, entry?: string): ContentPage[];
|
|
44
|
+
/**
|
|
45
|
+
* Build a navigation tree from a content directory.
|
|
46
|
+
*/
|
|
47
|
+
export declare function loadDocsNavTree(contentDir: string, entry?: string): NavTree;
|
|
48
|
+
/**
|
|
49
|
+
* Flatten a navigation tree into an ordered list of pages.
|
|
50
|
+
* Useful for computing previous/next page links.
|
|
51
|
+
*/
|
|
52
|
+
export declare function flattenNavTree(tree: NavTree): PageNode[];
|
package/dist/content.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content loading utilities for SvelteKit docs.
|
|
3
|
+
*
|
|
4
|
+
* Scans the filesystem for `.md` / `.svx` content files,
|
|
5
|
+
* extracts frontmatter, and builds a navigation tree compatible
|
|
6
|
+
* with @farming-labs/docs DocsConfig.
|
|
7
|
+
*/
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import matter from "gray-matter";
|
|
11
|
+
/**
|
|
12
|
+
* Scan a content directory and return all docs pages.
|
|
13
|
+
* Expects a flat or nested structure of `.md` files:
|
|
14
|
+
* content/docs/index.md → /docs
|
|
15
|
+
* content/docs/installation.md → /docs/installation
|
|
16
|
+
* content/docs/guides/auth.md → /docs/guides/auth
|
|
17
|
+
*/
|
|
18
|
+
export function loadDocsContent(contentDir, entry = "docs") {
|
|
19
|
+
const pages = [];
|
|
20
|
+
const absDir = path.resolve(contentDir);
|
|
21
|
+
function scan(dir, slugParts) {
|
|
22
|
+
if (!fs.existsSync(dir))
|
|
23
|
+
return;
|
|
24
|
+
const entries = fs.readdirSync(dir).sort();
|
|
25
|
+
for (const name of entries) {
|
|
26
|
+
const full = path.join(dir, name);
|
|
27
|
+
const stat = fs.statSync(full);
|
|
28
|
+
if (stat.isDirectory()) {
|
|
29
|
+
scan(full, [...slugParts, name]);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (!name.endsWith(".md") && !name.endsWith(".mdx") && !name.endsWith(".svx"))
|
|
33
|
+
continue;
|
|
34
|
+
const raw = fs.readFileSync(full, "utf-8");
|
|
35
|
+
const { data, content } = matter(raw);
|
|
36
|
+
const baseName = name.replace(/\.(md|mdx|svx)$/, "");
|
|
37
|
+
const isIndex = baseName === "index" || baseName === "page" || baseName === "+page";
|
|
38
|
+
const slug = isIndex
|
|
39
|
+
? slugParts.join("/")
|
|
40
|
+
: [...slugParts, baseName].join("/");
|
|
41
|
+
const url = slug ? `/${entry}/${slug}` : `/${entry}`;
|
|
42
|
+
const title = data.title ??
|
|
43
|
+
baseName.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
|
44
|
+
pages.push({
|
|
45
|
+
slug,
|
|
46
|
+
url,
|
|
47
|
+
title,
|
|
48
|
+
description: data.description,
|
|
49
|
+
icon: data.icon,
|
|
50
|
+
content: stripMarkdown(content),
|
|
51
|
+
rawContent: content,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
scan(absDir, []);
|
|
56
|
+
return pages;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Build a navigation tree from a content directory.
|
|
60
|
+
*/
|
|
61
|
+
export function loadDocsNavTree(contentDir, entry = "docs") {
|
|
62
|
+
const absDir = path.resolve(contentDir);
|
|
63
|
+
const children = [];
|
|
64
|
+
const indexPath = findIndex(absDir);
|
|
65
|
+
if (indexPath) {
|
|
66
|
+
const { data } = matter(fs.readFileSync(indexPath, "utf-8"));
|
|
67
|
+
children.push({
|
|
68
|
+
type: "page",
|
|
69
|
+
name: data.title ?? "Documentation",
|
|
70
|
+
url: `/${entry}`,
|
|
71
|
+
icon: data.icon,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
children.push(...scanDir(absDir, [], entry));
|
|
75
|
+
return { name: "Docs", children };
|
|
76
|
+
}
|
|
77
|
+
function scanDir(dir, slugParts, entry) {
|
|
78
|
+
if (!fs.existsSync(dir))
|
|
79
|
+
return [];
|
|
80
|
+
const nodes = [];
|
|
81
|
+
const entries = fs.readdirSync(dir).sort();
|
|
82
|
+
for (const name of entries) {
|
|
83
|
+
const full = path.join(dir, name);
|
|
84
|
+
if (!fs.statSync(full).isDirectory())
|
|
85
|
+
continue;
|
|
86
|
+
const indexPath = findIndex(full);
|
|
87
|
+
if (!indexPath)
|
|
88
|
+
continue;
|
|
89
|
+
const { data } = matter(fs.readFileSync(indexPath, "utf-8"));
|
|
90
|
+
const slug = [...slugParts, name];
|
|
91
|
+
const url = `/${entry}/${slug.join("/")}`;
|
|
92
|
+
const displayName = data.title ?? name.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
|
93
|
+
const icon = data.icon;
|
|
94
|
+
const childDirs = fs.readdirSync(full).filter((n) => {
|
|
95
|
+
const p = path.join(full, n);
|
|
96
|
+
return fs.statSync(p).isDirectory() && findIndex(p) !== null;
|
|
97
|
+
});
|
|
98
|
+
if (childDirs.length > 0) {
|
|
99
|
+
nodes.push({
|
|
100
|
+
type: "folder",
|
|
101
|
+
name: displayName,
|
|
102
|
+
icon,
|
|
103
|
+
index: { type: "page", name: displayName, url, icon },
|
|
104
|
+
children: scanDir(full, slug, entry),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
nodes.push({ type: "page", name: displayName, url, icon });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return nodes;
|
|
112
|
+
}
|
|
113
|
+
function findIndex(dir) {
|
|
114
|
+
for (const name of ["page.md", "page.mdx", "index.md", "index.svx", "+page.md", "+page.svx"]) {
|
|
115
|
+
const p = path.join(dir, name);
|
|
116
|
+
if (fs.existsSync(p))
|
|
117
|
+
return p;
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Flatten a navigation tree into an ordered list of pages.
|
|
123
|
+
* Useful for computing previous/next page links.
|
|
124
|
+
*/
|
|
125
|
+
export function flattenNavTree(tree) {
|
|
126
|
+
const pages = [];
|
|
127
|
+
function walk(nodes) {
|
|
128
|
+
for (const node of nodes) {
|
|
129
|
+
if (node.type === "page") {
|
|
130
|
+
pages.push(node);
|
|
131
|
+
}
|
|
132
|
+
else if (node.type === "folder") {
|
|
133
|
+
if (node.index)
|
|
134
|
+
pages.push(node.index);
|
|
135
|
+
walk(node.children);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
walk(tree.children);
|
|
140
|
+
return pages;
|
|
141
|
+
}
|
|
142
|
+
function stripMarkdown(content) {
|
|
143
|
+
return content
|
|
144
|
+
.replace(/^(import|export)\s.*$/gm, "")
|
|
145
|
+
.replace(/<[^>]+\/>/g, "")
|
|
146
|
+
.replace(/<\/?[A-Z][^>]*>/g, "")
|
|
147
|
+
.replace(/<\/?[a-z][^>]*>/g, "")
|
|
148
|
+
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
|
|
149
|
+
.replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1")
|
|
150
|
+
.replace(/^#{1,6}\s+/gm, "")
|
|
151
|
+
.replace(/(\*{1,3}|_{1,3})(.*?)\1/g, "$2")
|
|
152
|
+
.replace(/```[\s\S]*?```/g, "")
|
|
153
|
+
.replace(/`([^`]+)`/g, "$1")
|
|
154
|
+
.replace(/^>\s+/gm, "")
|
|
155
|
+
.replace(/^[-*_]{3,}\s*$/gm, "")
|
|
156
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
157
|
+
.trim();
|
|
158
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @farming-labs/svelte
|
|
3
|
+
*
|
|
4
|
+
* SvelteKit adapter for the @farming-labs/docs framework.
|
|
5
|
+
* Provides content loading, navigation tree building, search,
|
|
6
|
+
* and server-side markdown rendering for SvelteKit-based documentation sites.
|
|
7
|
+
*/
|
|
8
|
+
export { loadDocsContent, loadDocsNavTree, flattenNavTree, type NavNode, type NavTree, type PageNode, type FolderNode } from "./content.js";
|
|
9
|
+
export { createMdsvexConfig } from "./config.js";
|
|
10
|
+
export { renderMarkdown } from "./markdown.js";
|
|
11
|
+
export { createDocsServer, type DocsServer } from "./server.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @farming-labs/svelte
|
|
3
|
+
*
|
|
4
|
+
* SvelteKit adapter for the @farming-labs/docs framework.
|
|
5
|
+
* Provides content loading, navigation tree building, search,
|
|
6
|
+
* and server-side markdown rendering for SvelteKit-based documentation sites.
|
|
7
|
+
*/
|
|
8
|
+
export { loadDocsContent, loadDocsNavTree, flattenNavTree } from "./content.js";
|
|
9
|
+
export { createMdsvexConfig } from "./config.js";
|
|
10
|
+
export { renderMarkdown } from "./markdown.js";
|
|
11
|
+
export { createDocsServer } from "./server.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
/**
|
|
12
|
+
* Render a markdown string to HTML with full syntax highlighting,
|
|
13
|
+
* callouts, tables, tabs, and copy-to-clipboard support.
|
|
14
|
+
*
|
|
15
|
+
* Designed for server-side use in SvelteKit `+page.server` loaders.
|
|
16
|
+
*/
|
|
17
|
+
export declare function renderMarkdown(content: string): Promise<string>;
|
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
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
|
|
78
|
+
.replace(/&/g, "&")
|
|
79
|
+
.replace(/</g, "<")
|
|
80
|
+
.replace(/>/g, ">");
|
|
81
|
+
return {
|
|
82
|
+
html: `<pre class="shiki"><code>${escaped}</code></pre>`,
|
|
83
|
+
raw: trimmedCode,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function parseMeta(meta) {
|
|
88
|
+
const lang = (meta.split(/\s/)[0] || "text").toLowerCase();
|
|
89
|
+
const titleMatch = meta.match(/title=["']([^"']+)["']/);
|
|
90
|
+
return { lang, title: titleMatch ? titleMatch[1] : null };
|
|
91
|
+
}
|
|
92
|
+
function wrapCodeWithCopy(html, rawCode, title) {
|
|
93
|
+
const escapedRaw = rawCode
|
|
94
|
+
.replace(/&/g, "&")
|
|
95
|
+
.replace(/"/g, """)
|
|
96
|
+
.replace(/</g, "<")
|
|
97
|
+
.replace(/>/g, ">");
|
|
98
|
+
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>`;
|
|
99
|
+
if (title) {
|
|
100
|
+
return `<div class="fd-codeblock fd-codeblock--titled"><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>`;
|
|
101
|
+
}
|
|
102
|
+
return `<div class="fd-codeblock">${copyBtn}<div class="fd-codeblock-content">${html}</div></div>`;
|
|
103
|
+
}
|
|
104
|
+
function dedentCode(raw) {
|
|
105
|
+
const lines = raw.replace(/\n$/, "").split("\n");
|
|
106
|
+
const indent = lines.reduce((min, l) => {
|
|
107
|
+
if (!l.trim())
|
|
108
|
+
return min;
|
|
109
|
+
const spaces = l.match(/^(\s*)/)?.[1].length ?? 0;
|
|
110
|
+
return Math.min(min, spaces);
|
|
111
|
+
}, Infinity);
|
|
112
|
+
if (indent > 0 && indent < Infinity) {
|
|
113
|
+
return lines.map((l) => l.slice(indent)).join("\n");
|
|
114
|
+
}
|
|
115
|
+
return raw;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Render a markdown string to HTML with full syntax highlighting,
|
|
119
|
+
* callouts, tables, tabs, and copy-to-clipboard support.
|
|
120
|
+
*
|
|
121
|
+
* Designed for server-side use in SvelteKit `+page.server` loaders.
|
|
122
|
+
*/
|
|
123
|
+
export async function renderMarkdown(content) {
|
|
124
|
+
if (!content)
|
|
125
|
+
return "";
|
|
126
|
+
const hl = await getHighlighter();
|
|
127
|
+
let result = content;
|
|
128
|
+
// ── Tabs blocks: <Tabs items={[...]}> ... </Tabs> ──
|
|
129
|
+
const tabsBlocks = [];
|
|
130
|
+
result = result.replace(/<Tabs\s+items=\{?\[([^\]]+)\]\}?>([\s\S]*?)<\/Tabs>/g, (_, itemsStr, body) => {
|
|
131
|
+
const items = itemsStr
|
|
132
|
+
.split(",")
|
|
133
|
+
.map((s) => s.trim().replace(/^["']|["']$/g, ""));
|
|
134
|
+
const panels = [];
|
|
135
|
+
const tabRegex = /<Tab\s+value=["']([^"']+)["']>([\s\S]*?)<\/Tab>/g;
|
|
136
|
+
let tabMatch;
|
|
137
|
+
while ((tabMatch = tabRegex.exec(body)) !== null) {
|
|
138
|
+
const tabValue = tabMatch[1];
|
|
139
|
+
const tabContent = tabMatch[2].trim();
|
|
140
|
+
const codeMatch = tabContent.match(/```([^\n]*)\n([\s\S]*?)```/);
|
|
141
|
+
if (codeMatch) {
|
|
142
|
+
const { lang, title } = parseMeta(codeMatch[1]);
|
|
143
|
+
const dedented = dedentCode(codeMatch[2]);
|
|
144
|
+
const { html, raw } = highlightCode(hl, dedented, lang);
|
|
145
|
+
panels.push({ value: tabValue, html: wrapCodeWithCopy(html, raw, title) });
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
panels.push({ value: tabValue, html: `<p>${tabContent}</p>` });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
let tabsHtml = `<div class="fd-tabs" data-tabs>`;
|
|
152
|
+
tabsHtml += `<div class="fd-tabs-list" role="tablist">`;
|
|
153
|
+
for (let i = 0; i < items.length; i++) {
|
|
154
|
+
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>`;
|
|
155
|
+
}
|
|
156
|
+
tabsHtml += `</div>`;
|
|
157
|
+
for (let i = 0; i < panels.length; i++) {
|
|
158
|
+
tabsHtml += `<div class="fd-tab-panel${i === 0 ? " fd-tab-panel-active" : ""}" data-tab-panel="${panels[i].value}" role="tabpanel">${panels[i].html}</div>`;
|
|
159
|
+
}
|
|
160
|
+
tabsHtml += `</div>`;
|
|
161
|
+
const placeholder = `%%TABS_${tabsBlocks.length}%%`;
|
|
162
|
+
tabsBlocks.push(tabsHtml);
|
|
163
|
+
return placeholder;
|
|
164
|
+
});
|
|
165
|
+
// ── Fenced code blocks ──
|
|
166
|
+
const codeBlocks = [];
|
|
167
|
+
result = result.replace(/```([^\n]*)\n([\s\S]*?)```/g, (_, meta, code) => {
|
|
168
|
+
const { lang, title } = parseMeta(meta);
|
|
169
|
+
const { html, raw } = highlightCode(hl, code, lang);
|
|
170
|
+
const placeholder = `%%CODEBLOCK_${codeBlocks.length}%%`;
|
|
171
|
+
codeBlocks.push(wrapCodeWithCopy(html, raw, title));
|
|
172
|
+
return placeholder;
|
|
173
|
+
});
|
|
174
|
+
// Inline code
|
|
175
|
+
result = result.replace(/`([^`]+)`/g, "<code>$1</code>");
|
|
176
|
+
// Headings (h4 → h1 order to avoid prefix collisions)
|
|
177
|
+
result = result.replace(/^#### (.+)$/gm, (_, text) => {
|
|
178
|
+
return `<h4 id="${slugify(text)}">${text}</h4>`;
|
|
179
|
+
});
|
|
180
|
+
result = result.replace(/^### (.+)$/gm, (_, text) => {
|
|
181
|
+
return `<h3 id="${slugify(text)}">${text}</h3>`;
|
|
182
|
+
});
|
|
183
|
+
result = result.replace(/^## (.+)$/gm, (_, text) => {
|
|
184
|
+
return `<h2 id="${slugify(text)}">${text}</h2>`;
|
|
185
|
+
});
|
|
186
|
+
result = result.replace(/^# (.+)$/gm, "<h1>$1</h1>");
|
|
187
|
+
// ── Callouts / blockquotes (before inline formatting) ──
|
|
188
|
+
const calloutBlocks = [];
|
|
189
|
+
result = result.replace(/(?:^>\s*.+\n?)+/gm, (block) => {
|
|
190
|
+
const lines = block.split("\n").filter(Boolean);
|
|
191
|
+
const inner = lines.map((l) => l.replace(/^>\s?/, "")).join("\n");
|
|
192
|
+
const ghMatch = inner.match(/^\[!(NOTE|WARNING|TIP|IMPORTANT|CAUTION)\]\s*\n?([\s\S]*)/i);
|
|
193
|
+
if (ghMatch) {
|
|
194
|
+
const type = ghMatch[1].toLowerCase();
|
|
195
|
+
const calloutContent = ghMatch[2].trim();
|
|
196
|
+
const placeholder = `%%CALLOUT_${calloutBlocks.length}%%`;
|
|
197
|
+
calloutBlocks.push(renderCallout(type, calloutContent));
|
|
198
|
+
return placeholder;
|
|
199
|
+
}
|
|
200
|
+
const boldMatch = inner.match(/^\*\*(Note|Warning|Tip|Important|Caution):\*\*\s*([\s\S]*)/i);
|
|
201
|
+
if (boldMatch) {
|
|
202
|
+
const type = boldMatch[1].toLowerCase();
|
|
203
|
+
const calloutContent = boldMatch[2].trim();
|
|
204
|
+
const placeholder = `%%CALLOUT_${calloutBlocks.length}%%`;
|
|
205
|
+
calloutBlocks.push(renderCallout(type, calloutContent));
|
|
206
|
+
return placeholder;
|
|
207
|
+
}
|
|
208
|
+
return `<blockquote><p>${inner}</p></blockquote>`;
|
|
209
|
+
});
|
|
210
|
+
// Inline formatting
|
|
211
|
+
result = result.replace(/\*\*\*(.+?)\*\*\*/g, "<strong><em>$1</em></strong>");
|
|
212
|
+
result = result.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
|
|
213
|
+
result = result.replace(/\*(.+?)\*/g, "<em>$1</em>");
|
|
214
|
+
// Links
|
|
215
|
+
result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
|
|
216
|
+
// Horizontal rules
|
|
217
|
+
result = result.replace(/^---$/gm, "<hr />");
|
|
218
|
+
// Tables
|
|
219
|
+
result = result.replace(/^\|(.+)\|\n\|[-| ]+\|\n((?:\|.+\|\n?)+)/gm, (_, headerRow, bodyRows) => {
|
|
220
|
+
const headers = headerRow
|
|
221
|
+
.split("|")
|
|
222
|
+
.map((h) => h.trim())
|
|
223
|
+
.filter(Boolean);
|
|
224
|
+
const rows = bodyRows
|
|
225
|
+
.trim()
|
|
226
|
+
.split("\n")
|
|
227
|
+
.map((row) => row
|
|
228
|
+
.split("|")
|
|
229
|
+
.map((c) => c.trim())
|
|
230
|
+
.filter(Boolean));
|
|
231
|
+
const headerHtml = headers.map((h) => `<th>${h}</th>`).join("");
|
|
232
|
+
const rowsHtml = rows
|
|
233
|
+
.map((row) => `<tr>${row.map((c) => `<td>${c}</td>`).join("")}</tr>`)
|
|
234
|
+
.join("");
|
|
235
|
+
return `<div class="fd-table-wrapper"><table><thead><tr>${headerHtml}</tr></thead><tbody>${rowsHtml}</tbody></table></div>`;
|
|
236
|
+
});
|
|
237
|
+
// Unordered lists
|
|
238
|
+
result = result.replace(/(?:^- .+\n?)+/gm, (block) => {
|
|
239
|
+
const items = block
|
|
240
|
+
.split("\n")
|
|
241
|
+
.filter((l) => l.startsWith("- "))
|
|
242
|
+
.map((l) => `<li>${l.slice(2)}</li>`)
|
|
243
|
+
.join("");
|
|
244
|
+
return `<ul>${items}</ul>`;
|
|
245
|
+
});
|
|
246
|
+
// Ordered lists
|
|
247
|
+
result = result.replace(/(?:^\d+\. .+\n?)+/gm, (block) => {
|
|
248
|
+
const items = block
|
|
249
|
+
.split("\n")
|
|
250
|
+
.filter((l) => /^\d+\. /.test(l))
|
|
251
|
+
.map((l) => `<li>${l.replace(/^\d+\. /, "")}</li>`)
|
|
252
|
+
.join("");
|
|
253
|
+
return `<ol>${items}</ol>`;
|
|
254
|
+
});
|
|
255
|
+
// Wrap remaining bare text in <p> tags
|
|
256
|
+
result = result
|
|
257
|
+
.split("\n\n")
|
|
258
|
+
.map((block) => {
|
|
259
|
+
block = block.trim();
|
|
260
|
+
if (!block)
|
|
261
|
+
return "";
|
|
262
|
+
if (/^<(h[1-6]|pre|ul|ol|blockquote|hr|table|div)/.test(block))
|
|
263
|
+
return block;
|
|
264
|
+
if (/^%%(CODEBLOCK|CALLOUT|TABS)_\d+%%$/.test(block))
|
|
265
|
+
return block;
|
|
266
|
+
return `<p>${block}</p>`;
|
|
267
|
+
})
|
|
268
|
+
.join("\n");
|
|
269
|
+
// Restore placeholders
|
|
270
|
+
for (let i = 0; i < codeBlocks.length; i++) {
|
|
271
|
+
result = result.replace(`%%CODEBLOCK_${i}%%`, codeBlocks[i]);
|
|
272
|
+
}
|
|
273
|
+
for (let i = 0; i < calloutBlocks.length; i++) {
|
|
274
|
+
result = result.replace(`%%CALLOUT_${i}%%`, calloutBlocks[i]);
|
|
275
|
+
}
|
|
276
|
+
for (let i = 0; i < tabsBlocks.length; i++) {
|
|
277
|
+
result = result.replace(`%%TABS_${i}%%`, tabsBlocks[i]);
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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.js";
|
|
13
|
+
*
|
|
14
|
+
* export const { load, GET, POST } = createDocsServer(config);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* // routes/docs/+layout.server.js
|
|
19
|
+
* export { load } from "$lib/docs.server";
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import { loadDocsNavTree } from "./content.js";
|
|
23
|
+
import type { PageNode } from "./content.js";
|
|
24
|
+
interface UnifiedLoadEvent {
|
|
25
|
+
url: URL;
|
|
26
|
+
}
|
|
27
|
+
interface RequestEvent {
|
|
28
|
+
url: URL;
|
|
29
|
+
request: Request;
|
|
30
|
+
}
|
|
31
|
+
export interface DocsServer {
|
|
32
|
+
load: (event: UnifiedLoadEvent) => Promise<{
|
|
33
|
+
tree: ReturnType<typeof loadDocsNavTree>;
|
|
34
|
+
flatPages: PageNode[];
|
|
35
|
+
title: string;
|
|
36
|
+
description?: string;
|
|
37
|
+
html: string;
|
|
38
|
+
slug?: string;
|
|
39
|
+
previousPage: PageNode | null;
|
|
40
|
+
nextPage: PageNode | null;
|
|
41
|
+
editOnGithub?: string;
|
|
42
|
+
lastModified: string;
|
|
43
|
+
}>;
|
|
44
|
+
GET: (event: RequestEvent) => Response;
|
|
45
|
+
POST: (event: RequestEvent) => Promise<Response>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create all server-side functions needed for a SvelteKit docs site.
|
|
49
|
+
*
|
|
50
|
+
* @param config - The `DocsConfig` object (from `defineDocs()` in `docs.config.ts`).
|
|
51
|
+
*/
|
|
52
|
+
export declare function createDocsServer(config?: Record<string, any>): DocsServer;
|
|
53
|
+
export {};
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
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.js";
|
|
13
|
+
*
|
|
14
|
+
* export const { load, GET, POST } = createDocsServer(config);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* // routes/docs/+layout.server.js
|
|
19
|
+
* export { load } from "$lib/docs.server";
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import fs from "node:fs";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
import matter from "gray-matter";
|
|
25
|
+
import { loadDocsNavTree, loadDocsContent, flattenNavTree } from "./content.js";
|
|
26
|
+
import { renderMarkdown } from "./markdown.js";
|
|
27
|
+
/**
|
|
28
|
+
* Create all server-side functions needed for a SvelteKit docs site.
|
|
29
|
+
*
|
|
30
|
+
* @param config - The `DocsConfig` object (from `defineDocs()` in `docs.config.ts`).
|
|
31
|
+
*/
|
|
32
|
+
export function createDocsServer(config = {}) {
|
|
33
|
+
const entry = config.entry ?? "docs";
|
|
34
|
+
const githubRaw = config.github;
|
|
35
|
+
const github = typeof githubRaw === "string"
|
|
36
|
+
? { url: githubRaw }
|
|
37
|
+
: githubRaw ?? null;
|
|
38
|
+
const githubRepo = github?.url;
|
|
39
|
+
const githubBranch = github?.branch ?? "main";
|
|
40
|
+
const githubContentPath = github?.directory;
|
|
41
|
+
const contentDir = path.resolve(config.contentDir ?? entry);
|
|
42
|
+
const aiConfig = config.ai ?? {};
|
|
43
|
+
// Allow top-level apiKey as a shorthand
|
|
44
|
+
if (config.apiKey && !aiConfig.apiKey) {
|
|
45
|
+
aiConfig.apiKey = config.apiKey;
|
|
46
|
+
}
|
|
47
|
+
// ─── Unified load (tree + page content in one call) ────────
|
|
48
|
+
async function load(event) {
|
|
49
|
+
const tree = loadDocsNavTree(contentDir, entry);
|
|
50
|
+
const flatPages = flattenNavTree(tree);
|
|
51
|
+
const prefix = new RegExp(`^/${entry}/?`);
|
|
52
|
+
const slug = event.url.pathname.replace(prefix, "");
|
|
53
|
+
const isIndex = slug === "";
|
|
54
|
+
let filePath = null;
|
|
55
|
+
let relPath = "";
|
|
56
|
+
if (isIndex) {
|
|
57
|
+
for (const name of ["page.md", "page.mdx", "index.md"]) {
|
|
58
|
+
const candidate = path.join(contentDir, name);
|
|
59
|
+
if (fs.existsSync(candidate)) {
|
|
60
|
+
filePath = candidate;
|
|
61
|
+
relPath = name;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const candidates = [
|
|
68
|
+
path.join(contentDir, slug, "page.md"),
|
|
69
|
+
path.join(contentDir, slug, "page.mdx"),
|
|
70
|
+
path.join(contentDir, slug, "index.md"),
|
|
71
|
+
path.join(contentDir, slug, "index.svx"),
|
|
72
|
+
path.join(contentDir, `${slug}.md`),
|
|
73
|
+
path.join(contentDir, `${slug}.svx`),
|
|
74
|
+
];
|
|
75
|
+
for (const candidate of candidates) {
|
|
76
|
+
if (fs.existsSync(candidate)) {
|
|
77
|
+
filePath = candidate;
|
|
78
|
+
relPath = path.relative(contentDir, candidate);
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!filePath) {
|
|
84
|
+
const err = new Error(`Page not found: /${entry}/${slug}`);
|
|
85
|
+
err.status = 404;
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
const raw = fs.readFileSync(filePath, "utf-8");
|
|
89
|
+
const { data, content } = matter(raw);
|
|
90
|
+
const html = await renderMarkdown(content);
|
|
91
|
+
const currentUrl = isIndex ? `/${entry}` : `/${entry}/${slug}`;
|
|
92
|
+
const currentIndex = flatPages.findIndex((p) => p.url === currentUrl);
|
|
93
|
+
const previousPage = currentIndex > 0 ? flatPages[currentIndex - 1] : null;
|
|
94
|
+
const nextPage = currentIndex < flatPages.length - 1 ? flatPages[currentIndex + 1] : null;
|
|
95
|
+
let editOnGithub;
|
|
96
|
+
if (githubRepo && githubContentPath) {
|
|
97
|
+
editOnGithub = `${githubRepo}/blob/${githubBranch}/${githubContentPath}/${relPath}`;
|
|
98
|
+
}
|
|
99
|
+
const stat = fs.statSync(filePath);
|
|
100
|
+
const lastModified = stat.mtime.toLocaleDateString("en-US", {
|
|
101
|
+
year: "numeric",
|
|
102
|
+
month: "long",
|
|
103
|
+
day: "numeric",
|
|
104
|
+
});
|
|
105
|
+
const fallbackTitle = isIndex
|
|
106
|
+
? "Documentation"
|
|
107
|
+
: slug.split("/").pop()?.replace(/-/g, " ") ?? "Documentation";
|
|
108
|
+
return {
|
|
109
|
+
tree,
|
|
110
|
+
flatPages,
|
|
111
|
+
title: data.title ?? fallbackTitle,
|
|
112
|
+
description: data.description,
|
|
113
|
+
html,
|
|
114
|
+
...(isIndex ? {} : { slug }),
|
|
115
|
+
previousPage,
|
|
116
|
+
nextPage,
|
|
117
|
+
editOnGithub,
|
|
118
|
+
lastModified,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// ─── Search index ──────────────────────────────────────────
|
|
122
|
+
let searchIndex = null;
|
|
123
|
+
function getSearchIndex() {
|
|
124
|
+
if (!searchIndex) {
|
|
125
|
+
searchIndex = loadDocsContent(contentDir, entry);
|
|
126
|
+
}
|
|
127
|
+
return searchIndex;
|
|
128
|
+
}
|
|
129
|
+
function searchByQuery(query) {
|
|
130
|
+
const index = getSearchIndex();
|
|
131
|
+
return index
|
|
132
|
+
.map((page) => {
|
|
133
|
+
const titleMatch = page.title.toLowerCase().includes(query) ? 10 : 0;
|
|
134
|
+
const words = query.split(/\s+/);
|
|
135
|
+
const contentMatch = words.reduce((score, word) => {
|
|
136
|
+
return score + (page.content.toLowerCase().includes(word) ? 1 : 0);
|
|
137
|
+
}, 0);
|
|
138
|
+
return { ...page, score: titleMatch + contentMatch };
|
|
139
|
+
})
|
|
140
|
+
.filter((r) => r.score > 0)
|
|
141
|
+
.sort((a, b) => b.score - a.score);
|
|
142
|
+
}
|
|
143
|
+
// ─── GET /api/docs?query=… — full-text search ────────────
|
|
144
|
+
function GET(event) {
|
|
145
|
+
const query = event.url.searchParams.get("query")?.toLowerCase().trim();
|
|
146
|
+
if (!query) {
|
|
147
|
+
return new Response(JSON.stringify([]), {
|
|
148
|
+
headers: { "Content-Type": "application/json" },
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const results = searchByQuery(query)
|
|
152
|
+
.slice(0, 10)
|
|
153
|
+
.map(({ title, url, description }) => ({
|
|
154
|
+
content: title,
|
|
155
|
+
url,
|
|
156
|
+
description,
|
|
157
|
+
}));
|
|
158
|
+
return new Response(JSON.stringify(results), {
|
|
159
|
+
headers: { "Content-Type": "application/json" },
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
// ─── POST /api/docs — AI chat with RAG ────────────────────
|
|
163
|
+
const projectName = (typeof config.nav?.title === "string"
|
|
164
|
+
? config.nav.title
|
|
165
|
+
: null);
|
|
166
|
+
const packageName = aiConfig.packageName;
|
|
167
|
+
const docsUrl = aiConfig.docsUrl;
|
|
168
|
+
function buildDefaultSystemPrompt() {
|
|
169
|
+
const lines = [
|
|
170
|
+
`You are a helpful documentation assistant${projectName ? ` for ${projectName}` : ""}.`,
|
|
171
|
+
"Answer questions based on the provided documentation context.",
|
|
172
|
+
"Be concise and accurate. If the answer is not in the context, say so honestly.",
|
|
173
|
+
"Use markdown formatting for code examples and links.",
|
|
174
|
+
];
|
|
175
|
+
if (packageName) {
|
|
176
|
+
lines.push(`When showing import examples, always use "${packageName}" as the package name.`);
|
|
177
|
+
}
|
|
178
|
+
if (docsUrl) {
|
|
179
|
+
lines.push(`When linking to documentation pages, use "${docsUrl}" as the base URL (e.g. ${docsUrl}/docs/get-started).`);
|
|
180
|
+
}
|
|
181
|
+
return lines.join(" ");
|
|
182
|
+
}
|
|
183
|
+
const DEFAULT_SYSTEM_PROMPT = buildDefaultSystemPrompt();
|
|
184
|
+
async function POST(event) {
|
|
185
|
+
if (!aiConfig.enabled) {
|
|
186
|
+
return new Response(JSON.stringify({
|
|
187
|
+
error: "AI is not enabled. Set `ai: { enabled: true }` in your docs config to enable it.",
|
|
188
|
+
}), { status: 404, headers: { "Content-Type": "application/json" } });
|
|
189
|
+
}
|
|
190
|
+
const resolvedKey = aiConfig.apiKey ??
|
|
191
|
+
(typeof process !== "undefined" ? process.env?.OPENAI_API_KEY : undefined);
|
|
192
|
+
if (!resolvedKey) {
|
|
193
|
+
return new Response(JSON.stringify({
|
|
194
|
+
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.",
|
|
195
|
+
}), { status: 500, headers: { "Content-Type": "application/json" } });
|
|
196
|
+
}
|
|
197
|
+
let body;
|
|
198
|
+
try {
|
|
199
|
+
body = await event.request.json();
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return new Response(JSON.stringify({ error: "Invalid JSON body. Expected { messages: [...] }" }), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
203
|
+
}
|
|
204
|
+
const messages = body.messages;
|
|
205
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
206
|
+
return new Response(JSON.stringify({ error: "messages array is required and must not be empty." }), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
207
|
+
}
|
|
208
|
+
const lastUserMessage = [...messages].reverse().find((m) => m.role === "user");
|
|
209
|
+
if (!lastUserMessage) {
|
|
210
|
+
return new Response(JSON.stringify({ error: "At least one user message is required." }), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
211
|
+
}
|
|
212
|
+
const maxResults = aiConfig.maxResults ?? 5;
|
|
213
|
+
const scored = searchByQuery(lastUserMessage.content.toLowerCase()).slice(0, maxResults);
|
|
214
|
+
const contextParts = scored.map((doc) => `## ${doc.title}\nURL: ${doc.url}\n${doc.description ? `Description: ${doc.description}\n` : ""}\n${doc.content}`);
|
|
215
|
+
const context = contextParts.join("\n\n---\n\n");
|
|
216
|
+
const systemPrompt = aiConfig.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
|
|
217
|
+
const systemMessage = {
|
|
218
|
+
role: "system",
|
|
219
|
+
content: context
|
|
220
|
+
? `${systemPrompt}\n\n---\n\nDocumentation context:\n\n${context}`
|
|
221
|
+
: systemPrompt,
|
|
222
|
+
};
|
|
223
|
+
const llmMessages = [
|
|
224
|
+
systemMessage,
|
|
225
|
+
...messages.filter((m) => m.role !== "system"),
|
|
226
|
+
];
|
|
227
|
+
const baseUrl = (aiConfig.baseUrl ?? "https://api.openai.com/v1").replace(/\/$/, "");
|
|
228
|
+
const model = aiConfig.model ?? "gpt-4o-mini";
|
|
229
|
+
const llmResponse = await fetch(`${baseUrl}/chat/completions`, {
|
|
230
|
+
method: "POST",
|
|
231
|
+
headers: {
|
|
232
|
+
"Content-Type": "application/json",
|
|
233
|
+
Authorization: `Bearer ${resolvedKey}`,
|
|
234
|
+
},
|
|
235
|
+
body: JSON.stringify({ model, stream: true, messages: llmMessages }),
|
|
236
|
+
});
|
|
237
|
+
if (!llmResponse.ok) {
|
|
238
|
+
const errText = await llmResponse.text().catch(() => "Unknown error");
|
|
239
|
+
return new Response(JSON.stringify({ error: `LLM API error (${llmResponse.status}): ${errText}` }), { status: 502, headers: { "Content-Type": "application/json" } });
|
|
240
|
+
}
|
|
241
|
+
return new Response(llmResponse.body, {
|
|
242
|
+
headers: {
|
|
243
|
+
"Content-Type": "text/event-stream",
|
|
244
|
+
"Cache-Control": "no-cache",
|
|
245
|
+
Connection: "keep-alive",
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
return { load, GET, POST };
|
|
250
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@farming-labs/svelte",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "SvelteKit adapter for @farming-labs/docs — content loading and navigation utilities",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./config": {
|
|
15
|
+
"types": "./dist/config.d.ts",
|
|
16
|
+
"import": "./dist/config.js",
|
|
17
|
+
"default": "./dist/config.js"
|
|
18
|
+
},
|
|
19
|
+
"./content": {
|
|
20
|
+
"types": "./dist/content.d.ts",
|
|
21
|
+
"import": "./dist/content.js",
|
|
22
|
+
"default": "./dist/content.js"
|
|
23
|
+
},
|
|
24
|
+
"./markdown": {
|
|
25
|
+
"types": "./dist/markdown.d.ts",
|
|
26
|
+
"import": "./dist/markdown.js",
|
|
27
|
+
"default": "./dist/markdown.js"
|
|
28
|
+
},
|
|
29
|
+
"./server": {
|
|
30
|
+
"types": "./dist/server.d.ts",
|
|
31
|
+
"import": "./dist/server.js",
|
|
32
|
+
"default": "./dist/server.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"keywords": [
|
|
39
|
+
"docs",
|
|
40
|
+
"svelte",
|
|
41
|
+
"sveltekit",
|
|
42
|
+
"documentation"
|
|
43
|
+
],
|
|
44
|
+
"author": "Farming Labs",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"gray-matter": "^4.0.3",
|
|
48
|
+
"shiki": "^3.2.1"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^22.10.0",
|
|
52
|
+
"typescript": "^5.9.3",
|
|
53
|
+
"@farming-labs/docs": "0.0.2-beta.4"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@farming-labs/docs": "*"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsc",
|
|
60
|
+
"dev": "tsc --watch",
|
|
61
|
+
"typecheck": "tsc --noEmit"
|
|
62
|
+
}
|
|
63
|
+
}
|