Skip to content

Commit 87ff3a4

Browse files
staredclaude
andcommitted
Refactor HTML export to use CSS custom properties for colors
Instead of inlining hex colors throughout the HTML document, define all term colors as CSS custom properties in the :root selector and reference them with var(--term-X) throughout the document. This makes the exported HTML more maintainable and allows for easier theming. Colors are defined once at the top and referenced everywhere: :root { --term-exponential: #e6194B; --term-imaginary: #3cb44b; ... } .term-exponential { color: var(--term-exponential); } Also removes unused 'bend' variable in Beamer export. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent aa70a91 commit 87ff3a4

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/exporter.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,35 @@ export function exportToHTML(
138138
strict: false, // Allow HTML extension
139139
});
140140

141-
// Apply inline colors to description spans
142-
const descriptionHTML = applyColorsToHTML(content.description, content.termOrder, colorScheme);
141+
// Apply CSS variable references to description spans
142+
const descriptionHTML = applyColorsToHTML(content.description, content.termOrder);
143143

144144
// Generate definitions HTML with colors and render inline math
145145
const definitionsHTML = Array.from(content.definitions.entries())
146146
.map(([className, definition]) => {
147-
const color = getTermColor(className, content.termOrder, colorScheme);
147+
const cssVarName = `--term-${className}`;
148148
// Render inline math in definitions
149149
const processedDefinition = renderInlineMath(definition);
150150
return `
151151
<div class="definition">
152-
<h3 style="color: ${color};">${escapeHTML(className)}</h3>
152+
<h3 style="color: var(${cssVarName});">${escapeHTML(className)}</h3>
153153
<p>${processedDefinition}</p>
154154
</div>`;
155155
})
156156
.join('\n');
157157

158-
// Generate inline styles for term classes
159-
const termStyles = content.termOrder
158+
// Generate CSS custom properties for colors
159+
const colorVariables = content.termOrder
160160
.map((className) => {
161161
const color = getTermColor(className, content.termOrder, colorScheme);
162-
return `.term-${className} { color: ${color}; }`;
162+
return `--term-${className}: ${color};`;
163+
})
164+
.join('\n ');
165+
166+
// Generate inline styles for term classes referencing CSS variables
167+
const termStyles = content.termOrder
168+
.map((className) => {
169+
return `.term-${className} { color: var(--term-${className}); }`;
163170
})
164171
.join('\n ');
165172

@@ -178,6 +185,8 @@ export function exportToHTML(
178185
--bg-color: #ffffff;
179186
--text-color: #1f2937;
180187
--border-color: #d1d5db;
188+
/* Color definitions */
189+
${colorVariables}
181190
}
182191
183192
* {
@@ -459,18 +468,19 @@ function injectColorsIntoLatex(latex: string, termOrder: string[], colorScheme:
459468

460469
/**
461470
* Apply colors to description HTML (spans with term-X classes)
471+
* Uses CSS custom properties (variables) instead of inline hex colors
462472
*/
463-
function applyColorsToHTML(html: string, termOrder: string[], colorScheme: ColorScheme): string {
473+
function applyColorsToHTML(html: string, termOrder: string[]): string {
464474
let result = html;
465475

466476
termOrder.forEach((className) => {
467-
const color = getTermColor(className, termOrder, colorScheme);
468477
const termClass = `term-${className}`;
478+
const cssVarName = `--term-${className}`;
469479

470-
// Replace <span class="term-X"> with <span class="term-X" style="color: #hex">
480+
// Replace <span class="term-X"> with <span class="term-X" style="color: var(--term-X)">
471481
result = result.replace(
472482
new RegExp(`<span class="${termClass}">`, 'g'),
473-
`<span class="${termClass}" style="color: ${color}; font-weight: 600;">`
483+
`<span class="${termClass}" style="color: var(${cssVarName}); font-weight: 600;">`
474484
);
475485
});
476486

@@ -860,7 +870,6 @@ export function exportToBeamer(
860870
const nodeId = `def${index}`;
861871
const equationNodeId = `n${index}`;
862872
const definitionLatex = escapeLatexPreservingMath(definition);
863-
const bend = index % 2 === 0 ? 'bend left' : 'bend right';
864873

865874
return `\\begin{frame}<${index + 2}>[label=term${index}]
866875
\\frametitle{${escapeLaTeX(content.title || 'Equation')}}

0 commit comments

Comments
 (0)