|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Generate docs/all-anchors.adoc and website/public/llms.txt |
| 4 | + * |
| 5 | + * all-anchors.adoc: AsciiDoc include-based full reference document |
| 6 | + * llms.txt: Clean Markdown for LLM consumption |
| 7 | + * |
| 8 | + * Usage: node scripts/generate-llms-txt.js |
| 9 | + */ |
| 10 | + |
| 11 | +const fs = require('fs') |
| 12 | +const path = require('path') |
| 13 | + |
| 14 | +const ROOT = path.join(__dirname, '..') |
| 15 | + |
| 16 | +const categories = JSON.parse( |
| 17 | + fs.readFileSync(path.join(ROOT, 'website/public/data/categories.json'), 'utf-8') |
| 18 | +) |
| 19 | + |
| 20 | +// ─── AsciiDoc → Markdown converter ────────────────────────────────────────── |
| 21 | + |
| 22 | +function adocToMarkdown(adoc) { |
| 23 | + let md = adoc |
| 24 | + |
| 25 | + // Remove document attributes (:key: value) |
| 26 | + md = md.replace(/^:[a-z][a-z0-9-]*:.*$/gm, '') |
| 27 | + |
| 28 | + // Headings: = → #, == → ##, etc. |
| 29 | + md = md.replace(/^(=+) (.+)$/gm, (_, eq, title) => '#'.repeat(eq.length) + ' ' + title) |
| 30 | + |
| 31 | + // [source,lang] + ---- → ```lang / ``` |
| 32 | + md = md.replace(/\[source(?:,([^\]]*))?\]\s*\n----/g, (_, lang) => '```' + (lang ? lang.trim() : '')) |
| 33 | + md = md.replace(/^----\s*$/gm, '```') |
| 34 | + |
| 35 | + // [quote] block: [quote]\n____\ntext\n____ → > text |
| 36 | + md = md.replace(/\[quote[^\]]*\]\s*\n_{4}\s*\n([\s\S]*?)\n_{4}/g, (_, body) => |
| 37 | + body.trim().split('\n').map((l) => '> ' + l).join('\n') |
| 38 | + ) |
| 39 | + |
| 40 | + // Sidebar blocks **** → remove delimiters |
| 41 | + md = md.replace(/^\*{4}\s*$/gm, '') |
| 42 | + |
| 43 | + // Collapsible: [%collapsible] + ==== delimiters → remove markers, keep content |
| 44 | + md = md.replace(/^\[%collapsible\]\s*$/gm, '') |
| 45 | + md = md.replace(/^====\s*$/gm, '') |
| 46 | + |
| 47 | + // Tables |=== → remove delimiters |
| 48 | + md = md.replace(/^\|===\s*$/gm, '') |
| 49 | + |
| 50 | + // Table rows: |cell content → keep, clean up leading pipe |
| 51 | + md = md.replace(/^\|(.+)$/gm, (_, row) => { |
| 52 | + const cells = row.split('|').map((c) => c.trim()).filter(Boolean) |
| 53 | + return '| ' + cells.join(' | ') + ' |' |
| 54 | + }) |
| 55 | + |
| 56 | + // Remove block attribute lines |
| 57 | + md = md.replace(/^\[(?:horizontal|sidebar|cols[^\]]*|options[^\]]*|%\w+[^\]]*)\]\s*$/gm, '') |
| 58 | + |
| 59 | + // Definition lists: term:: description → **term**: description |
| 60 | + md = md.replace(/^([^:\n|#`>]+)::\s*(.*)$/gm, (_, term, desc) => |
| 61 | + desc.trim() ? `**${term.trim()}**: ${desc.trim()}` : `**${term.trim()}**` |
| 62 | + ) |
| 63 | + |
| 64 | + // Links: link:url[text] → [text](url) |
| 65 | + md = md.replace(/link:([^\[]+)\[([^\]]*)\]/g, '[$2]($1)') |
| 66 | + |
| 67 | + // Cross-references: <<id,text>> → text, <<id>> → `id` |
| 68 | + md = md.replace(/<<([^,>]+),([^>]+)>>/g, '$2') |
| 69 | + md = md.replace(/<<([^>]+)>>/g, '`$1`') |
| 70 | + |
| 71 | + // Bold: **text** stays, *text* → **text** |
| 72 | + md = md.replace(/(?<![*\w])\*([^*\n]+)\*(?![*\w])/g, '**$1**') |
| 73 | + |
| 74 | + // Ordered list items: ". item" → "1. item" |
| 75 | + md = md.replace(/^\. /gm, '1. ') |
| 76 | + |
| 77 | + // Trailing whitespace and normalize blank lines |
| 78 | + md = md.replace(/[ \t]+$/gm, '') |
| 79 | + md = md.replace(/\n{3,}/g, '\n\n') |
| 80 | + |
| 81 | + return md.trim() |
| 82 | +} |
| 83 | + |
| 84 | +// ─── Generate docs/all-anchors.adoc ───────────────────────────────────────── |
| 85 | + |
| 86 | +function generateAllAnchorsAdoc() { |
| 87 | + const lines = [ |
| 88 | + '= Semantic Anchors — Complete Reference', |
| 89 | + ':toc:', |
| 90 | + ':toc-placement: preamble', |
| 91 | + ':toclevels: 2', |
| 92 | + '', |
| 93 | + 'include::about.adoc[leveloffset=+1]', |
| 94 | + '', |
| 95 | + '<<<', |
| 96 | + '', |
| 97 | + ] |
| 98 | + |
| 99 | + for (const category of categories) { |
| 100 | + lines.push(`== ${category.name}`) |
| 101 | + lines.push('') |
| 102 | + for (const anchorId of category.anchors) { |
| 103 | + const filepath = path.join(ROOT, 'docs/anchors', `${anchorId}.adoc`) |
| 104 | + if (fs.existsSync(filepath)) { |
| 105 | + lines.push(`include::anchors/${anchorId}.adoc[leveloffset=+2]`) |
| 106 | + lines.push('') |
| 107 | + } |
| 108 | + } |
| 109 | + lines.push('<<<') |
| 110 | + lines.push('') |
| 111 | + } |
| 112 | + |
| 113 | + const output = lines.join('\n') |
| 114 | + fs.writeFileSync(path.join(ROOT, 'docs/all-anchors.adoc'), output, 'utf-8') |
| 115 | + console.warn(`Generated: docs/all-anchors.adoc (${categories.length} categories)`) |
| 116 | +} |
| 117 | + |
| 118 | +// ─── Generate website/public/llms.txt ─────────────────────────────────────── |
| 119 | + |
| 120 | +function generateLlmsTxt() { |
| 121 | + const totalAnchors = categories.reduce((n, c) => n + c.anchors.length, 0) |
| 122 | + const lines = [ |
| 123 | + '# Semantic Anchors — Complete Reference', |
| 124 | + '', |
| 125 | + `> ${totalAnchors} well-defined terms, methodologies, and frameworks`, |
| 126 | + '> that serve as precision reference points when communicating with LLMs.', |
| 127 | + '> Source: https://github.com/LLM-Coding/Semantic-Anchors', |
| 128 | + '> Website: https://llm-coding.github.io/Semantic-Anchors/', |
| 129 | + '', |
| 130 | + '---', |
| 131 | + '', |
| 132 | + ] |
| 133 | + |
| 134 | + // Introductory content from about.adoc |
| 135 | + const aboutAdoc = fs.readFileSync(path.join(ROOT, 'docs/about.adoc'), 'utf-8') |
| 136 | + lines.push(adocToMarkdown(aboutAdoc)) |
| 137 | + lines.push('') |
| 138 | + lines.push('---') |
| 139 | + lines.push('') |
| 140 | + |
| 141 | + // Anchors by category |
| 142 | + for (const category of categories) { |
| 143 | + lines.push(`## ${category.name}`) |
| 144 | + lines.push('') |
| 145 | + |
| 146 | + for (const anchorId of category.anchors) { |
| 147 | + const filepath = path.join(ROOT, 'docs/anchors', `${anchorId}.adoc`) |
| 148 | + if (!fs.existsSync(filepath)) continue |
| 149 | + |
| 150 | + const raw = fs.readFileSync(filepath, 'utf-8') |
| 151 | + const titleMatch = raw.match(/^= (.+)$/m) |
| 152 | + const title = titleMatch ? titleMatch[1] : anchorId |
| 153 | + |
| 154 | + lines.push(`### ${title}`) |
| 155 | + lines.push('') |
| 156 | + |
| 157 | + const body = raw.replace(/^= .+\n/, '') |
| 158 | + lines.push(adocToMarkdown(body)) |
| 159 | + lines.push('') |
| 160 | + } |
| 161 | + |
| 162 | + lines.push('---') |
| 163 | + lines.push('') |
| 164 | + } |
| 165 | + |
| 166 | + const output = lines.join('\n') |
| 167 | + fs.writeFileSync(path.join(ROOT, 'website/public/llms.txt'), output, 'utf-8') |
| 168 | + const kb = Math.round(Buffer.byteLength(output, 'utf-8') / 1024) |
| 169 | + console.warn(`Generated: website/public/llms.txt (${totalAnchors} anchors, ~${kb} KB)`) |
| 170 | +} |
| 171 | + |
| 172 | +// ─── Main ──────────────────────────────────────────────────────────────────── |
| 173 | + |
| 174 | +generateAllAnchorsAdoc() |
| 175 | +generateLlmsTxt() |
0 commit comments