|
| 1 | +import { minimatch } from 'minimatch'; |
| 2 | +import { dev } from '$app/environment'; |
| 3 | +import { index } from './content'; |
| 4 | + |
| 5 | +interface GenerateLlmContentOptions { |
| 6 | + ignore?: string[]; |
| 7 | + minimize?: Partial<MinimizeOptions>; |
| 8 | + sections: Section[]; |
| 9 | +} |
| 10 | + |
| 11 | +interface MinimizeOptions { |
| 12 | + remove_legacy: boolean; |
| 13 | + remove_note_blocks: boolean; |
| 14 | + remove_details_blocks: boolean; |
| 15 | + remove_playground_links: boolean; |
| 16 | + remove_prettier_ignore: boolean; |
| 17 | + normalize_whitespace: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +interface Section { |
| 21 | + slug: string; |
| 22 | + title: string; |
| 23 | +} |
| 24 | + |
| 25 | +const defaults: MinimizeOptions = { |
| 26 | + remove_legacy: false, |
| 27 | + remove_note_blocks: false, |
| 28 | + remove_details_blocks: false, |
| 29 | + remove_playground_links: false, |
| 30 | + remove_prettier_ignore: false, |
| 31 | + normalize_whitespace: false |
| 32 | +}; |
| 33 | + |
| 34 | +export function generate_llm_content(options: GenerateLlmContentOptions): string { |
| 35 | + let content = ''; |
| 36 | + |
| 37 | + for (const section of options.sections) { |
| 38 | + if (options.sections.length > 1) { |
| 39 | + content += `# Start of ${section.title} documentation\n\n`; |
| 40 | + } |
| 41 | + |
| 42 | + for (const [path, document] of Object.entries(index)) { |
| 43 | + if (!path.startsWith(`docs/${section.slug}`)) continue; |
| 44 | + |
| 45 | + if (options.ignore?.some((pattern) => minimatch(path, pattern))) { |
| 46 | + if (dev) console.log(`❌ Ignored by pattern: ${path}`); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + const doc_content = options.minimize |
| 51 | + ? minimize_content(document.body, options.minimize) |
| 52 | + : document.body; |
| 53 | + if (doc_content.trim() === '') continue; |
| 54 | + |
| 55 | + content += `\n# ${document.metadata.title}\n\n`; |
| 56 | + content += doc_content; |
| 57 | + content += '\n'; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return content; |
| 62 | +} |
| 63 | + |
| 64 | +export const sections: Section[] = [ |
| 65 | + { slug: 'svelte', title: 'Svelte' }, |
| 66 | + { slug: 'kit', title: 'SvelteKit' }, |
| 67 | + { slug: 'cli', title: 'the Svelte CLI' } |
| 68 | +]; |
| 69 | + |
| 70 | +export function get_documentation_title(section: Section): string { |
| 71 | + return `This is the developer documentation for ${section.title}.`; |
| 72 | +} |
| 73 | + |
| 74 | +function minimize_content(content: string, options?: Partial<MinimizeOptions>): string { |
| 75 | + // Merge with defaults, but only for properties that are defined |
| 76 | + const settings: MinimizeOptions = { ...defaults, ...options }; |
| 77 | + |
| 78 | + let minimized = content; |
| 79 | + |
| 80 | + if (settings.remove_legacy) { |
| 81 | + minimized = remove_quote_blocks(minimized, 'LEGACY'); |
| 82 | + } |
| 83 | + |
| 84 | + if (settings.remove_note_blocks) { |
| 85 | + minimized = remove_quote_blocks(minimized, 'NOTE'); |
| 86 | + } |
| 87 | + |
| 88 | + if (settings.remove_details_blocks) { |
| 89 | + minimized = remove_quote_blocks(minimized, 'DETAILS'); |
| 90 | + } |
| 91 | + |
| 92 | + if (settings.remove_playground_links) { |
| 93 | + // Replace playground URLs with /[link] but keep the original link text |
| 94 | + minimized = minimized.replace(/\[([^\]]+)\]\(\/playground[^)]+\)/g, '[$1](/REMOVED)'); |
| 95 | + } |
| 96 | + |
| 97 | + if (settings.remove_prettier_ignore) { |
| 98 | + minimized = minimized |
| 99 | + .split('\n') |
| 100 | + .filter((line) => line.trim() !== '<!-- prettier-ignore -->') |
| 101 | + .join('\n'); |
| 102 | + } |
| 103 | + |
| 104 | + if (settings.normalize_whitespace) { |
| 105 | + minimized = minimized.replace(/\s+/g, ' '); |
| 106 | + } |
| 107 | + |
| 108 | + minimized = minimized.trim(); |
| 109 | + |
| 110 | + return minimized; |
| 111 | +} |
| 112 | + |
| 113 | +function remove_quote_blocks(content: string, blockType: string): string { |
| 114 | + return content |
| 115 | + .split('\n') |
| 116 | + .reduce((acc: string[], line: string, index: number, lines: string[]) => { |
| 117 | + // If we find a block (with or without additional text), skip it and all subsequent blockquote lines |
| 118 | + if (line.trim().startsWith(`> [!${blockType}]`)) { |
| 119 | + // Skip all subsequent lines that are part of the blockquote |
| 120 | + let i = index; |
| 121 | + while (i < lines.length && (lines[i].startsWith('>') || lines[i].trim() === '')) { |
| 122 | + i++; |
| 123 | + } |
| 124 | + // Update the index to skip all these lines |
| 125 | + index = i - 1; |
| 126 | + return acc; |
| 127 | + } |
| 128 | + |
| 129 | + // Only add the line if it's not being skipped |
| 130 | + acc.push(line); |
| 131 | + return acc; |
| 132 | + }, []) |
| 133 | + .join('\n'); |
| 134 | +} |
0 commit comments