|
| 1 | +#!/usr/bin/env node |
| 2 | +// Scans a folder of course HTML files and writes a sorted JSON array of all |
| 3 | +// Shiki language identifiers found in code blocks to production/data/languages.json. |
| 4 | +// |
| 5 | +// Usage: node generate_language_file.js <path-to-html-folder> |
| 6 | +// |
| 7 | +// Recognises both patterns Skilljar produces: |
| 8 | +// <pre class="language-terraform"><code data-lang="terraform"> |
| 9 | +// <pre data-lang="terraform"><code> |
| 10 | + |
| 11 | +const { readFileSync, writeFileSync, readdirSync, statSync } = require("fs"); |
| 12 | +const { join, extname, resolve } = require("path"); |
| 13 | + |
| 14 | +const htmlFolder = process.argv[2]; |
| 15 | + |
| 16 | +if (!htmlFolder) { |
| 17 | + console.error("Usage: node generate_language_file.js <path-to-html-folder>"); |
| 18 | + process.exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +function walkHtml(dir) { |
| 22 | + const files = []; |
| 23 | + for (const entry of readdirSync(dir)) { |
| 24 | + const full = join(dir, entry); |
| 25 | + if (statSync(full).isDirectory()) { |
| 26 | + files.push(...walkHtml(full)); |
| 27 | + } else if (extname(entry) === ".html") { |
| 28 | + files.push(full); |
| 29 | + } |
| 30 | + } |
| 31 | + return files; |
| 32 | +} |
| 33 | + |
| 34 | +const patterns = [ |
| 35 | + /class="language-([^"\s]+)"/g, |
| 36 | + /data-lang="([^"]+)"/g, |
| 37 | +]; |
| 38 | + |
| 39 | +const languages = new Set(); |
| 40 | + |
| 41 | +for (const file of walkHtml(resolve(htmlFolder))) { |
| 42 | + const content = readFileSync(file, "utf-8"); |
| 43 | + for (const re of patterns) { |
| 44 | + re.lastIndex = 0; |
| 45 | + let match; |
| 46 | + while ((match = re.exec(content)) !== null) { |
| 47 | + languages.add(match[1]); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const sorted = [...languages].sort(); |
| 53 | +const outputPath = join(__dirname, "production/data/languages.json"); |
| 54 | + |
| 55 | +writeFileSync(outputPath, JSON.stringify(sorted, null, 2) + "\n"); |
| 56 | + |
| 57 | +console.log(`Found ${sorted.length} language(s): ${sorted.join(", ") || "(none)"}`); |
| 58 | +console.log(`Written to ${outputPath}`); |
0 commit comments