|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const log = require('fancy-log') |
| 6 | +const https = require('https') |
| 7 | + |
| 8 | +const ANTORA_YML_URL = 'https://raw.githubusercontent.com/redpanda-data/rp-connect-docs/main/antora.yml' |
| 9 | +const CONNECT_JSON_BASE = 'https://docs.redpanda.com/redpanda-connect/components/_attachments' |
| 10 | +const FALLBACK_VERSIONS = ['4.79.0', '4.78.0', '4.77.0', '4.76.0', '4.75.0'] |
| 11 | + |
| 12 | +function fetchText (url) { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + const options = { |
| 15 | + headers: { 'User-Agent': 'docs-ui-build' }, |
| 16 | + } |
| 17 | + https.get(url, options, (res) => { |
| 18 | + if (res.statusCode !== 200) { |
| 19 | + reject(new Error(`HTTP ${res.statusCode}`)) |
| 20 | + res.resume() |
| 21 | + return |
| 22 | + } |
| 23 | + let data = '' |
| 24 | + res.on('data', (chunk) => { data += chunk }) |
| 25 | + res.on('end', () => resolve(data)) |
| 26 | + }).on('error', reject) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +function fetchJSON (url) { |
| 31 | + return fetchText(url).then((text) => JSON.parse(text)) |
| 32 | +} |
| 33 | + |
| 34 | +async function getLatestVersion () { |
| 35 | + try { |
| 36 | + const yaml = await fetchText(ANTORA_YML_URL) |
| 37 | + const match = yaml.match(/latest-connect-version:\s*['"]?(\d+\.\d+\.\d+)/) |
| 38 | + if (match) { |
| 39 | + return match[1] |
| 40 | + } |
| 41 | + log.warn('Could not parse version from antora.yml') |
| 42 | + return null |
| 43 | + } catch (err) { |
| 44 | + log.warn('Could not fetch Connect version from antora.yml:', err.message) |
| 45 | + return null |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +async function fetchConnectJSON (version) { |
| 50 | + const url = `${CONNECT_JSON_BASE}/connect-${version}.json` |
| 51 | + try { |
| 52 | + return await fetchJSON(url) |
| 53 | + } catch (err) { |
| 54 | + log.warn(`Could not fetch connect-${version}.json:`, err.message) |
| 55 | + return null |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function extractNames (data) { |
| 60 | + const functions = [] |
| 61 | + const methods = [] |
| 62 | + |
| 63 | + if (Array.isArray(data['bloblang-functions'])) { |
| 64 | + for (const fn of data['bloblang-functions']) { |
| 65 | + if (fn.name) functions.push(fn.name) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (Array.isArray(data['bloblang-methods'])) { |
| 70 | + for (const method of data['bloblang-methods']) { |
| 71 | + if (method.name) methods.push(method.name) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return { functions: functions.sort(), methods: methods.sort() } |
| 76 | +} |
| 77 | + |
| 78 | +function generateGrammar (functions, methods) { |
| 79 | + return `/** |
| 80 | + * Prism syntax highlighting for Bloblang (blobl) |
| 81 | + * |
| 82 | + * Bloblang is Redpanda Connect's native mapping language for transforming data. |
| 83 | + * @see https://docs.redpanda.com/redpanda-connect/guides/bloblang/about/ |
| 84 | + * |
| 85 | + * AUTO-GENERATED from Connect JSON - do not edit manually. |
| 86 | + * Run: gulp generate:bloblang-grammar |
| 87 | + */ |
| 88 | +
|
| 89 | +(function (Prism) { |
| 90 | + var functions = ${JSON.stringify(functions)}.join('|'); |
| 91 | + var methods = ${JSON.stringify(methods)}.join('|'); |
| 92 | +
|
| 93 | + Prism.languages.bloblang = { |
| 94 | + 'comment': { |
| 95 | + pattern: /#.*/, |
| 96 | + greedy: true |
| 97 | + }, |
| 98 | +
|
| 99 | + 'string': [ |
| 100 | + { |
| 101 | + pattern: /"""[\\s\\S]*?"""/, |
| 102 | + greedy: true, |
| 103 | + alias: 'multiline-string' |
| 104 | + }, |
| 105 | + { |
| 106 | + pattern: /(["'])(?:\\\\(?:\\r\\n|[\\s\\S])|(?!\\1)[^\\\\\\r\\n])*\\1/, |
| 107 | + greedy: true |
| 108 | + } |
| 109 | + ], |
| 110 | +
|
| 111 | + 'spread': { |
| 112 | + pattern: /\\.\\*/, |
| 113 | + alias: 'operator' |
| 114 | + }, |
| 115 | +
|
| 116 | + 'keyword': { |
| 117 | + pattern: /\\b(?:root|this|let|meta|if|else|match|case|_)\\b/, |
| 118 | + lookbehind: false |
| 119 | + }, |
| 120 | +
|
| 121 | + 'function': { |
| 122 | + pattern: new RegExp('\\\\b(?:' + functions + ')(?=\\\\s*\\\\()', 'i') |
| 123 | + }, |
| 124 | +
|
| 125 | + 'method': { |
| 126 | + pattern: new RegExp('\\\\.\\\\s*(?:' + methods + ')(?=\\\\s*\\\\()', 'i'), |
| 127 | + inside: { |
| 128 | + 'punctuation': /^\\./ |
| 129 | + } |
| 130 | + }, |
| 131 | +
|
| 132 | + 'boolean': /\\b(?:true|false)\\b/, |
| 133 | +
|
| 134 | + 'null': { |
| 135 | + pattern: /\\bnull\\b/, |
| 136 | + alias: 'keyword' |
| 137 | + }, |
| 138 | +
|
| 139 | + 'number': /\\b-?(?:0x[\\da-f]+|\\d+(?:\\.\\d*)?(?:e[+-]?\\d+)?)\\b/i, |
| 140 | +
|
| 141 | + 'metadata': { |
| 142 | + pattern: /@(?:[\\w-]+|\\.[\\w-]+|\\()/, |
| 143 | + alias: 'variable' |
| 144 | + }, |
| 145 | +
|
| 146 | + 'variable': { |
| 147 | + pattern: /\\$[\\w-]+/ |
| 148 | + }, |
| 149 | +
|
| 150 | + 'operator': [ |
| 151 | + /->/, |
| 152 | + { |
| 153 | + pattern: /\\|/, |
| 154 | + alias: 'coalesce' |
| 155 | + }, |
| 156 | + /[<>]=?|[!=]=?|&&|\\|\\||!(?!=)/, |
| 157 | + /=/, |
| 158 | + /[+\\-*\\/%]/ |
| 159 | + ], |
| 160 | +
|
| 161 | + 'punctuation': /[{}[\\]();:,]|\\.(?!\\*)|->|\\.\\.\\.?/, |
| 162 | +
|
| 163 | + 'property': { |
| 164 | + pattern: /\\.[\\w-]+/, |
| 165 | + inside: { |
| 166 | + 'punctuation': /^\\./ |
| 167 | + } |
| 168 | + } |
| 169 | + }; |
| 170 | +
|
| 171 | + Prism.languages.blobl = Prism.languages.bloblang; |
| 172 | +
|
| 173 | +}(Prism)); |
| 174 | +` |
| 175 | +} |
| 176 | + |
| 177 | +module.exports = (destPath) => async (done) => { |
| 178 | + try { |
| 179 | + log('Fetching Connect JSON for Bloblang grammar generation...') |
| 180 | + |
| 181 | + let data = null |
| 182 | + |
| 183 | + // Try latest version first |
| 184 | + const latestVersion = await getLatestVersion() |
| 185 | + if (latestVersion) { |
| 186 | + log(`Trying latest version: ${latestVersion}`) |
| 187 | + data = await fetchConnectJSON(latestVersion) |
| 188 | + } |
| 189 | + |
| 190 | + // Fall back through known versions |
| 191 | + if (!data) { |
| 192 | + for (const version of FALLBACK_VERSIONS) { |
| 193 | + log(`Trying fallback version: ${version}`) |
| 194 | + data = await fetchConnectJSON(version) |
| 195 | + if (data) break |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + if (!data) { |
| 200 | + log.warn('Could not fetch Connect JSON, keeping existing grammar') |
| 201 | + done() |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + const { functions, methods } = extractNames(data) |
| 206 | + log(`Found ${functions.length} functions and ${methods.length} methods`) |
| 207 | + |
| 208 | + const grammar = generateGrammar(functions, methods) |
| 209 | + |
| 210 | + fs.mkdirSync(path.dirname(destPath), { recursive: true }) |
| 211 | + fs.writeFileSync(destPath, grammar) |
| 212 | + log(`Generated: ${destPath}`) |
| 213 | + |
| 214 | + done() |
| 215 | + } catch (err) { |
| 216 | + log.error('Failed to generate Bloblang grammar:', err) |
| 217 | + done(err) |
| 218 | + } |
| 219 | +} |
0 commit comments