-
-
Notifications
You must be signed in to change notification settings - Fork 79k
Expand file tree
/
Copy pathUtilityReferenceTable.astro
More file actions
176 lines (145 loc) · 4.85 KB
/
UtilityReferenceTable.astro
File metadata and controls
176 lines (145 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
interface Props {
utility: string | string[]; // The utility key(s) from the metadata (e.g., "font-size" or ["font-size", "text-size"])
className?: string;
}
const {
utility,
className = "table reference-table"
} = Astro.props;
// Normalize to array
const utilities = Array.isArray(utility) ? utility : [utility];
// Parse CSS variables from _root.scss at build time
function parseCSSVariables(): Record<string, string> {
try {
const projectRoot = process.cwd();
const rootScssPath = join(projectRoot, 'scss/_root.scss');
const scssContent = readFileSync(rootScssPath, 'utf-8');
const cssVarValues: Record<string, string> = {};
const varRegex = /--#\{\$prefix\}([a-z0-9-]+):\s*([^;]+);/gi;
let match;
while ((match = varRegex.exec(scssContent)) !== null) {
const varName = `--bs-${match[1]}`;
let value = match[2].trim();
value = value.replace(/#\{[^}]+\}/g, '').trim();
value = value.replace(/\/\/.*$/gm, '').trim();
if (value) {
cssVarValues[varName] = value;
}
}
return cssVarValues;
} catch (error) {
console.warn('Could not parse CSS variables from _root.scss:', error);
return {};
}
}
// Load utilities metadata
function loadUtilitiesMetadata(): any {
try {
const projectRoot = process.cwd();
const metadataPath = join(projectRoot, 'dist/css/bootstrap-utilities.metadata.json');
const metadataContent = readFileSync(metadataPath, 'utf-8');
return JSON.parse(metadataContent);
} catch (error) {
console.warn('Could not load utilities metadata:', error);
return { utilities: {} };
}
}
// Parse compiled CSS to extract styles for given class selectors
function parseCompiledCSS(classNames: string[]): Record<string, string[]> {
try {
const projectRoot = process.cwd();
const bootstrapCssPath = join(projectRoot, 'dist/css/bootstrap.css');
const cssContent = readFileSync(bootstrapCssPath, 'utf-8');
const classStyles: Record<string, string[]> = {};
classNames.forEach(className => {
// Match class selectors, including :where() wrapped child-selector utilities
const escapedClass = className.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const selectorRegex = new RegExp(
`(?:^|\\n)\\s*(?::where\\()?\\s*\\.${escapedClass}(?:\\s[^{]*)?\\)?\\s*\\{([^}]+)\\}`,
'gm'
);
let match;
let foundDeclarations: string[] = [];
while ((match = selectorRegex.exec(cssContent)) !== null) {
const declarations = match[1]
.split(';')
.map(decl => decl.trim())
.filter(decl => decl.length > 0)
.map(decl => `${decl};`);
if (declarations.length > 0) {
foundDeclarations = declarations;
break;
}
}
classStyles[className] = foundDeclarations;
});
return classStyles;
} catch (error) {
console.warn('Could not parse compiled CSS:', error);
return {};
}
}
const cssVarValues = parseCSSVariables();
const metadata = loadUtilitiesMetadata();
// Collect classes from all specified utilities
let allClasses: string[] = [];
utilities.forEach(util => {
const utilityMeta = metadata.utilities[util];
if (!utilityMeta) {
console.warn(`Utility "${util}" not found in metadata. Available utilities: ${Object.keys(metadata.utilities).join(', ')}`);
return;
}
const classes = utilityMeta.classes || [];
allClasses = allClasses.concat(classes);
});
if (allClasses.length === 0) {
throw new Error(`No classes found for utilities: ${utilities.join(', ')}`);
}
const classStyles = parseCompiledCSS(allClasses);
// Function to add CSS variable value comments
function addVarComments(cssValue: string): string {
const comments: string[] = [];
cssValue.replace(/var\((--[a-z0-9-]+)\)/gi, (match, varName) => {
const resolvedValue = cssVarValues[varName];
if (resolvedValue) {
comments.push(`<span class="fg-3">/* ${resolvedValue} */</span>`);
}
return match;
});
if (comments.length > 0) {
const hasSemicolon = cssValue.trimEnd().endsWith(';');
return `${cssValue}${hasSemicolon ? '' : ';'} ${comments.join(' ')}`;
}
return cssValue;
}
// Build table data
const tableData = allClasses.map(cls => {
const styles = classStyles[cls] || [];
const formattedStyles = styles.map(style => addVarComments(style)).join('<br/>');
return {
class: `.${cls}`,
styles: formattedStyles || '<em>Not found</em>'
};
});
---
<div class="table-responsive bd-reference-table">
<table class={className}>
<thead>
<tr>
<th scope="col">Class</th>
<th scope="col">Styles</th>
</tr>
</thead>
<tbody>
{tableData.map((row) => (
<tr>
<td>{row.class}</td>
<td><Fragment set:html={row.styles} /></td>
</tr>
))}
</tbody>
</table>
</div>