winzik/tools/bootstrap-catalog.mjs
David Nurdin 4a8b785cac IDE Bootstrap editor: components palette, utilities inspector, checks with quick fixes, class completion, AI help
Bootstrap mode for .html / .twig files of a project that uses Bootstrap (detected from the page, package.json, importmap.php,
the base template): the page drawn with Bootstrap's CSS (click to select, drop a component), a palette of 30 components with ids / aria /
data-bs wired, a utilities inspector (spacing box, grid per breakpoint, display, flex, text, colours, borders...) that rewrites the class
attribute only (one undo), problems with quick fixes and 'Convert the whole page to Bootstrap 5', and 'Ask the AI' (Claude / OpenCode /
chat model, the answer shown as a diff to accept). In every mode the code gets the checks as markers, class completion and hover docs.
Class lists come from the real 5.3 and 4.6 stylesheets (tools/bootstrap-catalog.mjs). Tests: node --test bootstrap.test.mjs, phpunit
BootstrapAssistantTest, games/ide/tools/ui-bootstrap-check.sh. 14 languages. docs/ide/bootstrap.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JkYTqsvYu6AYr9yQ3Q4Nt6
2026-11-16 00:47:07 +01:00

42 lines
2.4 KiB
JavaScript

// Builds ide/ui/js/bootstrap-classes.js: every class of Bootstrap's real stylesheets (the IDE's Bootstrap editor checks class names
// against it). Run in Docker, with the two bootstrap.min.css files of the npm packages:
// docker run --rm -v "$PWD:/w" -w /w node:22-alpine sh -c 'cd /tmp && npm pack [email protected] [email protected] --silent && \
// for f in bootstrap-*.tgz; do mkdir ${f%.tgz} && tar xzf $f -C ${f%.tgz} package/dist/css/bootstrap.min.css; done; \
// cd /w && node tools/bootstrap-catalog.mjs 5.3.3=/tmp/bootstrap-5.3.3/package/dist/css/bootstrap.min.css 4.6.2=/tmp/bootstrap-4.6.2/package/dist/css/bootstrap.min.css'
import { readFileSync, writeFileSync } from 'node:fs';
function classesOf(css) {
const s = css.replace(/\/\*[\s\S]*?\*\//g, '').replace(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g, '""').replace(/url\([^)]*\)/g, '');
const out = new Set();
let buffer = '';
let depth = 0;
const declarations = []; // depth at which a { opened a declaration block (no selectors inside it)
for (const ch of s) {
if (ch === '{') {
const prelude = buffer.trim();
// a rule's prelude is its selector list (an at-rule's, like @media, has none); inside a declaration block nothing is one
if (!declarations.length && prelude && !prelude.startsWith('@')) {
for (const m of prelude.matchAll(/(?<![\w\d)\]-])\.(-?[_a-zA-Z](?:[\w-]|\\.)*)/g)) out.add(m[1].replace(/\\(.)/g, '$1'));
declarations.push(depth);
} else if (declarations.length) declarations.push(depth);
depth++;
buffer = '';
} else if (ch === '}') {
depth--;
if (declarations.length && declarations[declarations.length - 1] === depth) declarations.pop();
buffer = '';
} else if (ch === ';' && !declarations.length) buffer = '';
else buffer += ch;
}
return [...out].sort();
}
const args = process.argv.slice(2).map((a) => a.split('='));
const lines = ['// GENERATED by tools/bootstrap-catalog.mjs from the real bootstrap.min.css files: do not edit.', '// version -> every class of its stylesheet (space separated)', 'export const BOOTSTRAP_CLASSES = {'];
for (const [version, file] of args) {
const list = classesOf(readFileSync(file, 'utf8'));
lines.push(` '${version.split('.')[0]}': '${list.join(' ')}',`);
console.log(version, list.length);
}
lines.push('};', '');
writeFileSync(new URL('../ide/ui/js/bootstrap-classes.js', import.meta.url), lines.join('\n'));