|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const dappsSidebar = require('../docs/dapps/sidebar.js'); |
| 4 | +const oevSearchersSidebar = require('../docs/oev-searchers/sidebar.js'); |
| 5 | + |
| 6 | +const docsDir = path.join(__dirname, '..', 'docs'); |
| 7 | +const staticDir = path.join(__dirname, '..', 'docs', 'public'); |
| 8 | +const llmsTxtPath = path.join(staticDir, 'llms.txt'); |
| 9 | +const llmsFullTxtPath = path.join(staticDir, 'llms-full.txt'); |
| 10 | + |
| 11 | +function getMarkdownFiles(items) { |
| 12 | + let files = []; |
| 13 | + for (const item of items) { |
| 14 | + if (item.link) { |
| 15 | + files.push(item.link); |
| 16 | + } |
| 17 | + if (item.items) { |
| 18 | + files = files.concat(getMarkdownFiles(item.items)); |
| 19 | + } |
| 20 | + } |
| 21 | + return files; |
| 22 | +} |
| 23 | + |
| 24 | +function generateLlmsTxt() { |
| 25 | + let content = '# Api3 Docs\n\n'; |
| 26 | + content += `> Api3 Docs helps developers build dApps using Api3 data feeds and searchers recapture oracle extractable value (OEV).\n\n`; |
| 27 | + |
| 28 | + const sidebars = { |
| 29 | + dapps: dappsSidebar, |
| 30 | + 'oev-searchers': oevSearchersSidebar, |
| 31 | + }; |
| 32 | + |
| 33 | + for (const key in sidebars) { |
| 34 | + const sidebar = sidebars[key]; |
| 35 | + if (sidebar) { |
| 36 | + content += `## ${key}\n\n`; |
| 37 | + const files = getMarkdownFiles(sidebar); |
| 38 | + for (const file of files) { |
| 39 | + const filePath = path.join(docsDir, `${file}.md`); |
| 40 | + if (fs.existsSync(filePath)) { |
| 41 | + const fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 42 | + const lines = fileContent.split('\n'); |
| 43 | + let title = path.basename(file, '.md'); |
| 44 | + for (const line of lines) { |
| 45 | + if (line.startsWith('title: ')) { |
| 46 | + title = line.substring('title: '.length); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + const url = `https://docs.api3.org${file}`; |
| 51 | + content += `- [${title}](${url.replace(/\/$/, '/index')}.html)\n`; |
| 52 | + } else { |
| 53 | + const indexPath = path.join(docsDir, file, 'index.md'); |
| 54 | + if (fs.existsSync(indexPath)) { |
| 55 | + const fileContent = fs.readFileSync(indexPath, 'utf-8'); |
| 56 | + const lines = fileContent.split('\n'); |
| 57 | + let title = path.basename(file); |
| 58 | + for (const line of lines) { |
| 59 | + if (line.startsWith('title: ')) { |
| 60 | + title = line.substring('title: '.length); |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + const url = `https://docs.api3.org${file}`; |
| 65 | + content += `- [${title}](${url.replace(/\/$/, '/index.html')})\n`; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + content += '\n'; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fs.writeFileSync(llmsTxtPath, content); |
| 74 | + console.log(`Successfully created ${llmsTxtPath}`); |
| 75 | +} |
| 76 | + |
| 77 | +function generateLlmsFullTxt() { |
| 78 | + const llmsTxtContent = fs.readFileSync(llmsTxtPath, 'utf-8'); |
| 79 | + const links = llmsTxtContent.match(/- \[(.*?)\]\((.*?)\)/g); |
| 80 | + let fullContent = ''; |
| 81 | + const pageHeader = '<PageHeader/>\n\n'; |
| 82 | + |
| 83 | + if (links) { |
| 84 | + for (const link of links) { |
| 85 | + const match = link.match(/- \[(.*?)\]\((.*?)\)/); |
| 86 | + if (match) { |
| 87 | + const url = match[2] |
| 88 | + .replace('https://docs.api3.org', '') |
| 89 | + .replace('.html', '.md'); |
| 90 | + const filePath = path.join(docsDir, url); |
| 91 | + if (fs.existsSync(filePath)) { |
| 92 | + let fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 93 | + const lines = fileContent.split('\n'); |
| 94 | + let pageHeaderValue = ''; |
| 95 | + for (const line of lines) { |
| 96 | + if (line.startsWith('pageHeader: ')) { |
| 97 | + pageHeaderValue = line.substring('pageHeader: '.length); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + const pageHeaderIndex = fileContent.indexOf(pageHeader); |
| 103 | + if (pageHeaderIndex === -1) { |
| 104 | + throw new Error(`Could not find PageHeader in ${filePath}`); |
| 105 | + } |
| 106 | + fileContent = fileContent.substring( |
| 107 | + pageHeaderIndex + pageHeader.length |
| 108 | + ); |
| 109 | + const titleMatch = fileContent.match(/# (.*)/); |
| 110 | + if (titleMatch && pageHeaderValue) { |
| 111 | + fileContent = fileContent.replace( |
| 112 | + /# (.*)/, |
| 113 | + `# ${titleMatch[1]} (${pageHeaderValue})` |
| 114 | + ); |
| 115 | + } |
| 116 | + fullContent += fileContent + '\n\n'; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + fs.writeFileSync(llmsFullTxtPath, fullContent); |
| 123 | + console.log(`Successfully created ${llmsFullTxtPath}`); |
| 124 | +} |
| 125 | + |
| 126 | +function main() { |
| 127 | + try { |
| 128 | + if (!fs.existsSync(staticDir)) { |
| 129 | + fs.mkdirSync(staticDir, { recursive: true }); |
| 130 | + } |
| 131 | + generateLlmsTxt(); |
| 132 | + generateLlmsFullTxt(); |
| 133 | + } catch (err) { |
| 134 | + console.error('Error creating llms files:', err); |
| 135 | + process.exit(1); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +main(); |
0 commit comments