Skip to content

Commit 580af17

Browse files
authored
Fix unresolved variables wrongly replaced with v (#30)
1 parent f6b00ba commit 580af17

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ function extractColors(colors, name, ast) {
4040
}
4141
}
4242

43-
colors[name]['--base-size-8'] = '8px';
44-
colors[name]['--base-size-16'] = '16px';
45-
colors[name]['--base-text-weight-normal'] = '400';
46-
colors[name]['--base-text-weight-medium'] = '500';
47-
colors[name]['--base-text-weight-semibold'] = '600';
43+
function addDeclaration(property, value) {
44+
colors[name].push({type: 'declaration', property, value});
45+
colors[name][property] = value;
46+
}
47+
48+
addDeclaration('--base-size-8', '8px');
49+
addDeclaration('--base-size-16', '16px');
50+
addDeclaration('--base-text-weight-normal', '400');
51+
addDeclaration('--base-text-weight-medium', '500');
52+
addDeclaration('--base-text-weight-semibold', '600');
4853
}
4954

5055
// https://github.com/gjtorikian/html-pipeline/blob/main/lib/html_pipeline/sanitization_filter.rb
@@ -322,7 +327,7 @@ function applyColors(colors, rules) {
322327
return colors[name];
323328
}
324329

325-
return match[0];
330+
return match;
326331
});
327332
}
328333
}
@@ -446,12 +451,17 @@ export default async function getCSS({
446451

447452
// Find all variables used across all styles
448453
const usedVariables = new Set(rules.flatMap(rule => rule.declarations.flatMap(({value}) => {
449-
let match = /var\((?<name>[-\w]+?)\)/.exec(value)?.groups.name;
450-
if (match === '--color-text-primary') {
451-
match = '--color-fg-default';
452-
}
454+
const matches = [];
455+
const re = /var\((?<name>[-\w]+?)[,)]/g;
456+
let match = null;
457+
do {
458+
match = re.exec(value);
459+
if (match) {
460+
matches.push(match.groups.name);
461+
}
462+
} while (match);
453463

454-
return match ? [match] : [];
464+
return matches;
455465
})));
456466

457467
const colorSchemeLight = {type: 'declaration', property: 'color-scheme', value: 'light'};

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const html = `<!DOCTYPE html>
3636
<body class="markdown-body">
3737
<article class="markdown-body" style="padding: 1em; max-width: 42em; margin: 0px auto;">
3838
${fixture}
39+
40+
<div class="markdown-alert markdown-alert-note" dir="auto">
41+
<p class="markdown-alert-title" dir="auto"><svg class="octicon octicon-info mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>Note</p>
42+
<p dir="auto">Highlights information that users should take into account, even when skimming.</p>
43+
</div>
44+
3945
</article>
4046
<select style="position: fixed; top: 1em; right: 1em; font-size: 16px;"
4147
onchange="theme.href=this.value">
@@ -71,6 +77,12 @@ const htmlVars = `<!DOCTYPE html>
7177
<body class="markdown-body">
7278
<article class="markdown-body" style="padding: 1em; max-width: 42em; margin: 0px auto;">
7379
${fixture}
80+
81+
<div class="markdown-alert markdown-alert-note" dir="auto">
82+
<p class="markdown-alert-title" dir="auto"><svg class="octicon octicon-info mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>Note</p>
83+
<p dir="auto">Highlights information that users should take into account, even when skimming.</p>
84+
</div>
85+
7486
</article>
7587
<select style="position: fixed; top: 1em; right: 1em; font-size: 16px;"
7688
onchange="theme.href=this.value">

0 commit comments

Comments
 (0)