@nuasite/llm-enhancements 0.64.3 → 0.65.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.
Files changed (65) hide show
  1. package/dist/{types/build-processor.d.ts → build-processor.d.ts} +1 -1
  2. package/dist/build-processor.d.ts.map +1 -0
  3. package/dist/build-processor.js +135 -0
  4. package/dist/{types/cms-marker.d.ts → cms-marker.d.ts} +1 -1
  5. package/dist/cms-marker.d.ts.map +1 -0
  6. package/dist/cms-marker.js +59 -0
  7. package/dist/cms.d.ts.map +1 -0
  8. package/dist/cms.js +21 -0
  9. package/dist/{types/dev-middleware.d.ts → dev-middleware.d.ts} +1 -1
  10. package/dist/dev-middleware.d.ts.map +1 -0
  11. package/dist/dev-middleware.js +216 -0
  12. package/dist/html-to-markdown.d.ts.map +1 -0
  13. package/dist/html-to-markdown.js +298 -0
  14. package/dist/{types/index.d.ts → index.d.ts} +2 -2
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +23 -0
  17. package/dist/{types/llm-endpoint.d.ts → llm-endpoint.d.ts} +1 -1
  18. package/dist/llm-endpoint.d.ts.map +1 -0
  19. package/dist/llm-endpoint.js +52 -0
  20. package/dist/{types/llms-txt-endpoint.d.ts → llms-txt-endpoint.d.ts} +1 -1
  21. package/dist/llms-txt-endpoint.d.ts.map +1 -0
  22. package/dist/llms-txt-endpoint.js +92 -0
  23. package/dist/{types/markdown-generator.d.ts → markdown-generator.d.ts} +1 -1
  24. package/dist/markdown-generator.d.ts.map +1 -0
  25. package/dist/markdown-generator.js +106 -0
  26. package/dist/mdx.d.ts.map +1 -0
  27. package/dist/mdx.js +90 -0
  28. package/dist/{types/page-markdown.d.ts → page-markdown.d.ts} +4 -4
  29. package/dist/page-markdown.d.ts.map +1 -0
  30. package/dist/page-markdown.js +34 -0
  31. package/dist/paths.d.ts.map +1 -0
  32. package/dist/paths.js +79 -0
  33. package/dist/tsconfig.tsbuildinfo +1 -0
  34. package/dist/types/tests/tsconfig.tsbuildinfo +1 -1
  35. package/dist/types.d.ts.map +1 -0
  36. package/dist/types.js +11 -0
  37. package/package.json +10 -10
  38. package/src/build-processor.ts +10 -10
  39. package/src/cms-marker.ts +3 -3
  40. package/src/dev-middleware.ts +10 -10
  41. package/src/index.ts +4 -4
  42. package/src/llm-endpoint.ts +2 -2
  43. package/src/llms-txt-endpoint.ts +2 -2
  44. package/src/markdown-generator.ts +1 -1
  45. package/src/page-markdown.ts +5 -5
  46. package/src/tsconfig.json +5 -1
  47. package/dist/types/build-processor.d.ts.map +0 -1
  48. package/dist/types/cms-marker.d.ts.map +0 -1
  49. package/dist/types/cms.d.ts.map +0 -1
  50. package/dist/types/dev-middleware.d.ts.map +0 -1
  51. package/dist/types/html-to-markdown.d.ts.map +0 -1
  52. package/dist/types/index.d.ts.map +0 -1
  53. package/dist/types/llm-endpoint.d.ts.map +0 -1
  54. package/dist/types/llms-txt-endpoint.d.ts.map +0 -1
  55. package/dist/types/markdown-generator.d.ts.map +0 -1
  56. package/dist/types/mdx.d.ts.map +0 -1
  57. package/dist/types/page-markdown.d.ts.map +0 -1
  58. package/dist/types/paths.d.ts.map +0 -1
  59. package/dist/types/tsconfig.tsbuildinfo +0 -1
  60. package/dist/types/types.d.ts.map +0 -1
  61. /package/dist/{types/cms.d.ts → cms.d.ts} +0 -0
  62. /package/dist/{types/html-to-markdown.d.ts → html-to-markdown.d.ts} +0 -0
  63. /package/dist/{types/mdx.d.ts → mdx.d.ts} +0 -0
  64. /package/dist/{types/paths.d.ts → paths.d.ts} +0 -0
  65. /package/dist/{types/types.d.ts → types.d.ts} +0 -0
@@ -0,0 +1,298 @@
1
+ import { NodeType, parse } from 'node-html-parser';
2
+ /** Elements to exclude from markdown conversion */
3
+ const EXCLUDED_TAGS = new Set([
4
+ 'nav',
5
+ 'footer',
6
+ 'header',
7
+ 'script',
8
+ 'style',
9
+ 'noscript',
10
+ 'svg',
11
+ 'iframe',
12
+ 'form',
13
+ 'button',
14
+ 'input',
15
+ 'select',
16
+ 'textarea',
17
+ ]);
18
+ /** Block-level elements that need newlines around them */
19
+ const BLOCK_ELEMENTS = new Set([
20
+ 'p',
21
+ 'div',
22
+ 'section',
23
+ 'article',
24
+ 'main',
25
+ 'aside',
26
+ 'h1',
27
+ 'h2',
28
+ 'h3',
29
+ 'h4',
30
+ 'h5',
31
+ 'h6',
32
+ 'ul',
33
+ 'ol',
34
+ 'li',
35
+ 'blockquote',
36
+ 'pre',
37
+ 'table',
38
+ 'tr',
39
+ 'thead',
40
+ 'tbody',
41
+ 'figure',
42
+ 'figcaption',
43
+ 'hr',
44
+ 'br',
45
+ ]);
46
+ /**
47
+ * Extract the main content area from HTML
48
+ */
49
+ function extractMainContent(root) {
50
+ // Try to find main content container in order of preference
51
+ const selectors = ['main', 'article', '[role="main"]', '.content', '#content'];
52
+ for (const selector of selectors) {
53
+ const element = root.querySelector(selector);
54
+ if (element) {
55
+ return element;
56
+ }
57
+ }
58
+ // Fall back to body content
59
+ const body = root.querySelector('body');
60
+ return body ?? root;
61
+ }
62
+ /**
63
+ * Check if an element should be excluded from markdown output
64
+ */
65
+ function shouldExclude(element) {
66
+ const tagName = element.tagName?.toLowerCase();
67
+ if (!tagName)
68
+ return false;
69
+ return EXCLUDED_TAGS.has(tagName);
70
+ }
71
+ /**
72
+ * Convert an HTML element to markdown
73
+ */
74
+ function elementToMarkdown(element, depth = 0) {
75
+ const tagName = element.tagName?.toLowerCase();
76
+ if (!tagName) {
77
+ return '';
78
+ }
79
+ if (shouldExclude(element)) {
80
+ return '';
81
+ }
82
+ // Process children for most elements
83
+ const childrenMd = () => childNodesToMarkdown(element, depth);
84
+ switch (tagName) {
85
+ // Headings
86
+ case 'h1':
87
+ return `\n# ${childrenMd().trim()}\n`;
88
+ case 'h2':
89
+ return `\n## ${childrenMd().trim()}\n`;
90
+ case 'h3':
91
+ return `\n### ${childrenMd().trim()}\n`;
92
+ case 'h4':
93
+ return `\n#### ${childrenMd().trim()}\n`;
94
+ case 'h5':
95
+ return `\n##### ${childrenMd().trim()}\n`;
96
+ case 'h6':
97
+ return `\n###### ${childrenMd().trim()}\n`;
98
+ // Paragraphs
99
+ case 'p':
100
+ return `\n${childrenMd().trim()}\n`;
101
+ // Inline formatting
102
+ case 'strong':
103
+ case 'b':
104
+ return `**${childrenMd()}**`;
105
+ case 'em':
106
+ case 'i':
107
+ return `*${childrenMd()}*`;
108
+ case 'code':
109
+ return `\`${childrenMd()}\``;
110
+ case 's':
111
+ case 'del':
112
+ case 'strike':
113
+ return `~~${childrenMd()}~~`;
114
+ // Links
115
+ case 'a': {
116
+ const href = element.getAttribute('href') ?? '';
117
+ const text = childrenMd().trim();
118
+ if (!text)
119
+ return '';
120
+ return `[${text}](${href})`;
121
+ }
122
+ // Images
123
+ case 'img': {
124
+ const src = element.getAttribute('src') ?? '';
125
+ const alt = element.getAttribute('alt') ?? '';
126
+ return `![${alt}](${src})`;
127
+ }
128
+ // Lists
129
+ case 'ul':
130
+ case 'ol':
131
+ return `\n${childrenMd()}`;
132
+ case 'li': {
133
+ const parent = element.parentNode;
134
+ const isOrdered = parent?.tagName?.toLowerCase() === 'ol';
135
+ const prefix = isOrdered ? '1. ' : '- ';
136
+ const content = childrenMd().trim();
137
+ return `${prefix}${content}\n`;
138
+ }
139
+ // Code blocks
140
+ case 'pre': {
141
+ // node-html-parser treats pre content as text, so we need to re-parse innerHTML
142
+ const innerParsed = parse(element.innerHTML);
143
+ const codeElement = innerParsed.querySelector('code');
144
+ const code = codeElement ? codeElement.textContent : element.textContent;
145
+ const lang = codeElement?.getAttribute('class')?.match(/language-(\w+)/)?.[1] ?? '';
146
+ return `\n\`\`\`${lang}\n${code?.trim()}\n\`\`\`\n`;
147
+ }
148
+ // Blockquotes
149
+ case 'blockquote': {
150
+ const content = childrenMd().trim();
151
+ const lines = content.split('\n');
152
+ return '\n' + lines.map((line) => `> ${line}`).join('\n') + '\n';
153
+ }
154
+ // Horizontal rule
155
+ case 'hr':
156
+ return '\n---\n';
157
+ // Line break
158
+ case 'br':
159
+ return '\n';
160
+ // Tables
161
+ case 'table':
162
+ return `\n${convertTable(element)}\n`;
163
+ // Figures
164
+ case 'figure':
165
+ return `\n${childrenMd()}`;
166
+ case 'figcaption':
167
+ return `\n*${childrenMd().trim()}*\n`;
168
+ // Container elements - just process children
169
+ case 'div':
170
+ case 'section':
171
+ case 'article':
172
+ case 'main':
173
+ case 'aside':
174
+ case 'span':
175
+ case 'thead':
176
+ case 'tbody':
177
+ case 'tfoot':
178
+ return childrenMd();
179
+ default:
180
+ return childrenMd();
181
+ }
182
+ }
183
+ /**
184
+ * Convert child nodes to markdown
185
+ */
186
+ function childNodesToMarkdown(element, depth) {
187
+ let result = '';
188
+ for (const child of element.childNodes) {
189
+ if (child.nodeType === NodeType.TEXT_NODE) {
190
+ // Normalize whitespace in text nodes
191
+ const text = child.textContent?.replace(/\s+/g, ' ') ?? '';
192
+ result += text;
193
+ }
194
+ else if (child.nodeType === NodeType.ELEMENT_NODE) {
195
+ result += elementToMarkdown(child, depth + 1);
196
+ }
197
+ }
198
+ return result;
199
+ }
200
+ /**
201
+ * Convert HTML table to markdown table
202
+ */
203
+ function convertTable(table) {
204
+ const rows = table.querySelectorAll('tr');
205
+ if (rows.length === 0)
206
+ return '';
207
+ const result = [];
208
+ let headerDone = false;
209
+ for (const row of rows) {
210
+ const cells = row.querySelectorAll('th, td');
211
+ const cellContents = cells.map((cell) => {
212
+ const content = childNodesToMarkdown(cell, 0).trim();
213
+ return content.replace(/\|/g, '\\|'); // Escape pipe characters
214
+ });
215
+ result.push(`| ${cellContents.join(' | ')} |`);
216
+ // Add header separator after first row with th elements
217
+ if (!headerDone && row.querySelector('th')) {
218
+ result.push(`| ${cellContents.map(() => '---').join(' | ')} |`);
219
+ headerDone = true;
220
+ }
221
+ }
222
+ // If no header row with th, add separator after first row
223
+ if (!headerDone && result.length > 0) {
224
+ const firstRow = rows[0];
225
+ if (firstRow) {
226
+ const cellCount = firstRow.querySelectorAll('td').length;
227
+ if (cellCount > 0) {
228
+ result.splice(1, 0, `| ${Array(cellCount).fill('---').join(' | ')} |`);
229
+ }
230
+ }
231
+ }
232
+ return result.join('\n');
233
+ }
234
+ /**
235
+ * Extract page title from HTML
236
+ */
237
+ function extractTitle(root) {
238
+ // Try meta title first
239
+ const title = root.querySelector('title');
240
+ if (title?.textContent) {
241
+ return title.textContent.trim();
242
+ }
243
+ // Try og:title
244
+ const ogTitle = root.querySelector('meta[property="og:title"]');
245
+ if (ogTitle) {
246
+ return ogTitle.getAttribute('content') ?? undefined;
247
+ }
248
+ // Try first h1
249
+ const h1 = root.querySelector('h1');
250
+ if (h1?.textContent) {
251
+ return h1.textContent.trim();
252
+ }
253
+ return undefined;
254
+ }
255
+ /**
256
+ * Extract meta description from HTML
257
+ */
258
+ function extractDescription(root) {
259
+ const meta = root.querySelector('meta[name="description"]');
260
+ if (meta) {
261
+ return meta.getAttribute('content') ?? undefined;
262
+ }
263
+ const ogDesc = root.querySelector('meta[property="og:description"]');
264
+ if (ogDesc) {
265
+ return ogDesc.getAttribute('content') ?? undefined;
266
+ }
267
+ return undefined;
268
+ }
269
+ /**
270
+ * Convert HTML string to markdown
271
+ */
272
+ export function htmlToMarkdown(html) {
273
+ const root = parse(html);
274
+ // Extract metadata
275
+ const title = extractTitle(root);
276
+ const description = extractDescription(root);
277
+ // Get main content
278
+ const mainContent = extractMainContent(root);
279
+ // Convert to markdown
280
+ let body = elementToMarkdown(mainContent);
281
+ // Clean up the output
282
+ body = body
283
+ // Remove excessive newlines
284
+ .replace(/\n{3,}/g, '\n\n')
285
+ // Trim whitespace from each line
286
+ .split('\n')
287
+ .map((line) => line.trimEnd())
288
+ .join('\n')
289
+ // Trim start and end
290
+ .trim();
291
+ return {
292
+ metadata: {
293
+ title,
294
+ description,
295
+ },
296
+ body,
297
+ };
298
+ }
@@ -1,5 +1,5 @@
1
1
  import type { AstroIntegration } from 'astro';
2
- import { type PageMarkdownOptions } from './types';
2
+ import { type PageMarkdownOptions } from './types.ts';
3
3
  export default function pageMarkdown(options?: PageMarkdownOptions): AstroIntegration;
4
- export type { LlmEndpointOptions, LlmsTxtOptions, MarkdownOutput, PageMarkdownOptions } from './types';
4
+ export type { LlmEndpointOptions, LlmsTxtOptions, MarkdownOutput, PageMarkdownOptions } from './types.ts';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,gBAAgB,EAAE,MAAM,OAAO,CAAA;AAG1D,OAAO,EAAE,KAAK,mBAAmB,EAAkB,MAAM,YAAY,CAAA;AAErE,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,gBAAgB,CAsBxF;AAED,YAAY,EAAE,kBAAkB,EAAE,cAAc,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import { processBuildOutput } from "./build-processor.js";
2
+ import { createDevMiddleware } from "./dev-middleware.js";
3
+ import { resolveOptions } from "./types.js";
4
+ export default function pageMarkdown(options = {}) {
5
+ const resolvedOptions = resolveOptions(options);
6
+ let config;
7
+ return {
8
+ name: '@nuasite/llm-enhancements',
9
+ hooks: {
10
+ 'astro:config:done': ({ config: cfg }) => {
11
+ config = cfg;
12
+ },
13
+ 'astro:server:setup': ({ server, logger }) => {
14
+ // Cast needed due to Astro's bundled Vite types differing from root vite
15
+ createDevMiddleware(server, resolvedOptions, config);
16
+ logger.info('Markdown endpoints enabled');
17
+ },
18
+ 'astro:build:done': async ({ dir, pages, logger }) => {
19
+ await processBuildOutput(dir, pages, resolvedOptions, logger, config);
20
+ },
21
+ },
22
+ };
23
+ }
@@ -1,4 +1,4 @@
1
- import type { LlmEndpointOptions } from './types';
1
+ import type { LlmEndpointOptions } from './types.ts';
2
2
  export interface PageEntry {
3
3
  pathname: string;
4
4
  title?: string;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"llm-endpoint.d.ts","sourceRoot":"","sources":["../src/llm-endpoint.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAEpD,MAAM,WAAW,SAAS;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,YAAY,GAAG,QAAQ,CAAA;CAC7B;AAED,MAAM,WAAW,YAAY;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,SAAS,EAAE,EAClB,YAAY,EAAE,YAAY,EAC1B,OAAO,EAAE,kBAAkB,GACzB,MAAM,CAyDR"}
@@ -0,0 +1,52 @@
1
+ import { getMarkdownUrl } from "./paths.js";
2
+ /**
3
+ * Generate the content for /.well-known/llm.md
4
+ */
5
+ export function generateLlmMarkdown(pages, siteMetadata, options) {
6
+ const siteName = options.siteName ?? siteMetadata.title ?? 'Site';
7
+ const description = options.description ?? siteMetadata.description;
8
+ const baseUrl = options.baseUrl ?? siteMetadata.baseUrl ?? '';
9
+ const lines = [];
10
+ // Frontmatter
11
+ lines.push('---');
12
+ lines.push(`generatedAt: ${new Date().toISOString()}`);
13
+ lines.push('---');
14
+ lines.push('');
15
+ // Title
16
+ lines.push(`# ${siteName}`);
17
+ lines.push('');
18
+ // Description
19
+ if (description) {
20
+ lines.push(description);
21
+ lines.push('');
22
+ }
23
+ // Markdown endpoints section
24
+ lines.push('## Markdown Endpoints');
25
+ lines.push('');
26
+ lines.push('This site exposes page content as markdown at `.md` URLs.');
27
+ lines.push('');
28
+ // Pages section
29
+ if (pages.length > 0) {
30
+ lines.push('### Pages');
31
+ lines.push('');
32
+ // List all pages sorted by pathname
33
+ const sortedPages = [...pages].sort((a, b) => a.pathname.localeCompare(b.pathname));
34
+ for (const page of sortedPages) {
35
+ const mdUrl = getMarkdownUrl(page.pathname);
36
+ lines.push(`- [${baseUrl}${mdUrl}](${baseUrl}${mdUrl})${page.title ? ` - ${page.title}` : ''}`);
37
+ }
38
+ lines.push('');
39
+ }
40
+ // Usage section
41
+ lines.push('## Usage');
42
+ lines.push('');
43
+ lines.push('Append `.md` to any page URL to get the markdown version:');
44
+ lines.push(`- \`${baseUrl}/about\` → \`${baseUrl}/about.md\``);
45
+ lines.push(`- \`${baseUrl}/blog/hello\` → \`${baseUrl}/blog/hello.md\``);
46
+ // Additional content
47
+ if (options.additionalContent) {
48
+ lines.push('');
49
+ lines.push(options.additionalContent);
50
+ }
51
+ return lines.join('\n');
52
+ }
@@ -1,4 +1,4 @@
1
- import type { LlmsTxtOptions } from './types';
1
+ import type { LlmsTxtOptions } from './types.ts';
2
2
  export interface PageEntry {
3
3
  pathname: string;
4
4
  title?: string;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"llms-txt-endpoint.d.ts","sourceRoot":"","sources":["../src/llms-txt-endpoint.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD,MAAM,WAAW,SAAS;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,YAAY,GAAG,QAAQ,CAAA;CAC7B;AAED,MAAM,WAAW,YAAY;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,SAAS,EAAE,EAClB,YAAY,EAAE,YAAY,EAC1B,OAAO,EAAE,cAAc,GACrB,MAAM,CAiGR"}
@@ -0,0 +1,92 @@
1
+ import { getMarkdownUrl } from "./paths.js";
2
+ /**
3
+ * Generate the content for /llms.txt
4
+ *
5
+ * The llms.txt format follows the convention at https://llmstxt.org/
6
+ * providing a standardized way to communicate site structure to LLMs and crawlers.
7
+ */
8
+ export function generateLlmsTxt(pages, siteMetadata, options) {
9
+ const siteName = options.siteName ?? siteMetadata.title ?? 'Site';
10
+ const description = options.description ?? siteMetadata.description;
11
+ const baseUrl = options.baseUrl ?? siteMetadata.baseUrl ?? '';
12
+ const lines = [];
13
+ // Title
14
+ lines.push(`# ${siteName}`);
15
+ lines.push('');
16
+ // Description as blockquote (llms.txt convention)
17
+ if (description) {
18
+ lines.push(`> ${description}`);
19
+ lines.push('');
20
+ }
21
+ // LLM-specific guidance
22
+ if (options.allowCrawling !== false) {
23
+ lines.push('This site provides markdown versions of all pages for LLM consumption.');
24
+ lines.push('');
25
+ }
26
+ // LLM Discovery endpoint
27
+ lines.push('## LLM Discovery');
28
+ lines.push('');
29
+ lines.push(`- [LLM Discovery Endpoint](${baseUrl}/.well-known/llm.md): Full site map with all available markdown endpoints`);
30
+ lines.push('');
31
+ // Markdown endpoints section
32
+ lines.push('## Markdown Endpoints');
33
+ lines.push('');
34
+ lines.push('All pages are available as markdown by appending `.md` to the URL.');
35
+ lines.push('');
36
+ // Separate collection and static pages
37
+ const collectionPages = pages.filter((p) => p.type === 'collection');
38
+ const staticPages = pages.filter((p) => p.type === 'static');
39
+ // Content pages (from collections)
40
+ if (collectionPages.length > 0) {
41
+ lines.push('### Content');
42
+ lines.push('');
43
+ const sortedCollection = [...collectionPages].sort((a, b) => a.pathname.localeCompare(b.pathname));
44
+ for (const page of sortedCollection) {
45
+ const mdUrl = getMarkdownUrl(page.pathname);
46
+ const title = page.title ?? page.pathname;
47
+ lines.push(`- [${title}](${baseUrl}${mdUrl}): ${page.pathname}`);
48
+ }
49
+ lines.push('');
50
+ }
51
+ // Static pages
52
+ if (staticPages.length > 0) {
53
+ lines.push('### Pages');
54
+ lines.push('');
55
+ const sortedStatic = [...staticPages].sort((a, b) => a.pathname.localeCompare(b.pathname));
56
+ for (const page of sortedStatic) {
57
+ const mdUrl = getMarkdownUrl(page.pathname);
58
+ const title = page.title ?? page.pathname;
59
+ lines.push(`- [${title}](${baseUrl}${mdUrl}): ${page.pathname}`);
60
+ }
61
+ lines.push('');
62
+ }
63
+ // If both are empty, show a generic message
64
+ if (collectionPages.length === 0 && staticPages.length === 0) {
65
+ lines.push('Append `.md` to any page URL to get the markdown version.');
66
+ lines.push('');
67
+ lines.push('Examples:');
68
+ lines.push('- `/about` → `/about.md`');
69
+ lines.push('- `/blog/post` → `/blog/post.md`');
70
+ lines.push('');
71
+ }
72
+ // Crawling permissions
73
+ lines.push('## Permissions');
74
+ lines.push('');
75
+ if (options.allowCrawling !== false) {
76
+ lines.push('LLMs and crawlers are welcome to access markdown endpoints.');
77
+ }
78
+ else {
79
+ lines.push('Please respect rate limits when crawling.');
80
+ }
81
+ // Additional instructions
82
+ if (options.instructions) {
83
+ lines.push('');
84
+ lines.push(options.instructions);
85
+ }
86
+ // Additional content
87
+ if (options.additionalContent) {
88
+ lines.push('');
89
+ lines.push(options.additionalContent);
90
+ }
91
+ return lines.join('\n');
92
+ }
@@ -1,4 +1,4 @@
1
- import type { MarkdownOutput } from './types';
1
+ import type { MarkdownOutput } from './types.ts';
2
2
  export interface GenerateOptions {
3
3
  /** URL path of the page */
4
4
  url: string;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown-generator.d.ts","sourceRoot":"","sources":["../src/markdown-generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAyDhD,MAAM,WAAW,eAAe;IAC/B,2BAA2B;IAC3B,GAAG,EAAE,MAAM,CAAA;IACX,6CAA6C;IAC7C,IAAI,EAAE,YAAY,GAAG,QAAQ,CAAA;IAC7B,4CAA4C;IAC5C,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,eAAe,EACxB,kBAAkB,UAAO,GACvB,MAAM,CAsBR;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CACrC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,EAC5D,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GAChB,cAAc,CAYhB;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CACjC,QAAQ,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,EAClD,IAAI,EAAE,MAAM,GACV,cAAc,CAchB"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Serialize a value to YAML format
3
+ */
4
+ function yamlValue(value) {
5
+ if (value === null || value === undefined) {
6
+ return 'null';
7
+ }
8
+ if (typeof value === 'string') {
9
+ // Check if string needs quoting
10
+ if (value.includes('\n')
11
+ || value.includes(':')
12
+ || value.includes('#')
13
+ || value.startsWith(' ')
14
+ || value.endsWith(' ')
15
+ || value === '') {
16
+ // Use double quotes and escape
17
+ return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;
18
+ }
19
+ return value;
20
+ }
21
+ if (typeof value === 'number' || typeof value === 'boolean') {
22
+ return String(value);
23
+ }
24
+ if (Array.isArray(value)) {
25
+ if (value.length === 0)
26
+ return '[]';
27
+ return '\n' + value.map((v) => ` - ${yamlValue(v)}`).join('\n');
28
+ }
29
+ if (typeof value === 'object') {
30
+ const entries = Object.entries(value);
31
+ if (entries.length === 0)
32
+ return '{}';
33
+ return '\n' + entries.map(([k, v]) => ` ${k}: ${yamlValue(v)}`).join('\n');
34
+ }
35
+ return String(value);
36
+ }
37
+ /**
38
+ * Convert frontmatter object to YAML string
39
+ */
40
+ function frontmatterToYaml(frontmatter) {
41
+ const lines = [];
42
+ for (const [key, value] of Object.entries(frontmatter)) {
43
+ const yamlVal = yamlValue(value);
44
+ if (yamlVal.startsWith('\n')) {
45
+ lines.push(`${key}:${yamlVal}`);
46
+ }
47
+ else {
48
+ lines.push(`${key}: ${yamlVal}`);
49
+ }
50
+ }
51
+ return lines.join('\n');
52
+ }
53
+ /**
54
+ * Generate complete markdown output with frontmatter
55
+ */
56
+ export function generateMarkdown(output, options, includeFrontmatter = true) {
57
+ if (!includeFrontmatter) {
58
+ return output.body;
59
+ }
60
+ // Build frontmatter with metadata
61
+ // Use absolute URL if baseUrl is provided, otherwise use relative path
62
+ const absoluteUrl = options.baseUrl ? `${options.baseUrl}${options.url}` : options.url;
63
+ const frontmatter = {
64
+ ...output.frontmatter,
65
+ url: absoluteUrl,
66
+ type: options.type,
67
+ generatedAt: new Date().toISOString(),
68
+ };
69
+ if (options.sourcePath) {
70
+ frontmatter.source = options.sourcePath;
71
+ }
72
+ const yamlContent = frontmatterToYaml(frontmatter);
73
+ return `---\n${yamlContent}\n---\n\n${output.body}`;
74
+ }
75
+ /**
76
+ * Create a simple markdown output for collection content
77
+ * that already has markdown body
78
+ */
79
+ export function createCollectionOutput(frontmatter, body, sourcePath) {
80
+ // Convert frontmatter to simple key-value
81
+ const simpleFrontmatter = {};
82
+ for (const [key, { value }] of Object.entries(frontmatter)) {
83
+ simpleFrontmatter[key] = value;
84
+ }
85
+ return {
86
+ frontmatter: simpleFrontmatter,
87
+ body,
88
+ sourcePath,
89
+ };
90
+ }
91
+ /**
92
+ * Create markdown output from HTML conversion result
93
+ */
94
+ export function createStaticOutput(metadata, body) {
95
+ const frontmatter = {};
96
+ if (metadata.title) {
97
+ frontmatter.title = metadata.title;
98
+ }
99
+ if (metadata.description) {
100
+ frontmatter.description = metadata.description;
101
+ }
102
+ return {
103
+ frontmatter,
104
+ body,
105
+ };
106
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mdx.d.ts","sourceRoot":"","sources":["../src/mdx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AASxC,MAAM,WAAW,cAAc;IAC9B;;;OAGG;IACH,UAAU,EAAE,OAAO,CAAA;CACnB;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,GAAG,cAAc,CASpE;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,MAAM,GAAG,SAAS,CAmC1F"}