Skip to content

Commit 23270ed

Browse files
authored
Fix support for latest GitHub styles (#34)
1 parent a656734 commit 23270ed

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

index.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ function extractColors(colors, name, ast) {
3939
}
4040
}
4141
}
42-
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');
5342
}
5443

5544
// https://github.com/gjtorikian/html-pipeline/blob/main/lib/html_pipeline/sanitization_filter.rb
@@ -200,9 +189,9 @@ function extractStyles(rules, ast) {
200189
}
201190

202191
function fixDeclaration(declaration) {
203-
// 'var(--fgColor-default, var(--color-fg-default))' -> 'var(--color-fg-default)'
192+
// 'var(--fgColor-default, var(--color-fg-default))' -> 'var(--fgColor-default)'
204193
if (declaration.value.includes('Color')) {
205-
declaration.value = declaration.value.replace(/var\([^,]+,\s*(var\(--color-.+?\))\)/, '$1');
194+
declaration.value = declaration.value.replace(/var\(([^,]+),\s*(var\(--color-.+?\))\)/, 'var($1)');
206195
}
207196
}
208197

@@ -319,9 +308,6 @@ function applyColors(colors, rules) {
319308
if (declaration.value.includes('var(')) {
320309
declaration.value = declaration.value.replaceAll(/var\((.+?)\)/g, (match, name) => {
321310
name = name.split(',')[0];
322-
if (name === '--color-text-primary') {
323-
name = '--color-fg-default';
324-
}
325311

326312
if (name in colors) {
327313
return colors[name];
@@ -387,6 +373,7 @@ In "single" mode, the output will apply the values of all variables to the rules
387373
@param {boolean} [options.onlyStyles=false] - Only output the style part of the CSS without any variables. Forces `preserveVariables` to be `true` and ignores the theme values. Useful to get the base styles to use multiple themes.
388374
@param {string} [options.rootSelector=.markdown-body] - Set the root selector of the rendered Markdown body as it should appear in the output CSS. Defaults to `.markdown-body`.
389375
*/
376+
// eslint-disable-next-line complexity
390377
export default async function getCSS({
391378
light = 'light',
392379
dark = 'dark',
@@ -414,12 +401,13 @@ export default async function getCSS({
414401
const links = unique(body.match(/(?<=href=").+?\.css/g));
415402
const contents = await Promise.all(links.map(url => cachedGot(url)));
416403

404+
const shared = [];
417405
const colors = [];
418406
let rules = [];
419407

420408
for (const [url, cssText] of zip(links, contents)) {
421409
// Get the name of a css file without the cache prevention number
422-
const match = url.match(/(?<=\/)\w+(?=-\w+\.css$)/);
410+
const match = url.match(/(?<=\/)[-\w]+(?=-\w+\.css$)/);
423411
if (!match) {
424412
continue;
425413
}
@@ -428,6 +416,11 @@ export default async function getCSS({
428416
const patched = patchCSSText(cssText);
429417
const ast = css.parse(patched);
430418

419+
// Primer*.css contains styles and variables that apply to all themes
420+
if (name.startsWith('primer')) {
421+
extractColors(shared, name, ast);
422+
}
423+
431424
// If it's a theme variable file extract colors, otherwise extract style
432425
if (/^(light|dark)/.test(name)) {
433426
extractColors(colors, name, ast);
@@ -441,6 +434,19 @@ export default async function getCSS({
441434
return colors.map(({name}) => name).join('\n');
442435
}
443436

437+
// Merge shared variables into every color theme
438+
for (const declarations of shared) {
439+
for (const declaration of declarations) {
440+
const {property, value} = declaration;
441+
for (const theme of colors) {
442+
if (!(property in theme)) {
443+
theme[property] = value;
444+
theme.unshift(declaration);
445+
}
446+
}
447+
}
448+
}
449+
444450
rules = reverseUnique(rules, rule => {
445451
const selector = rule.selectors.join(',');
446452
const body = rule.declarations.map(({property, value}) => `${property}: ${value}`).join(';');
@@ -529,10 +535,9 @@ export default async function getCSS({
529535

530536
let string = css.stringify({type: 'stylesheet', stylesheet: {rules}});
531537

532-
const rootBegin = string.indexOf('\n.markdown-body {');
533-
const rootEnd = string.indexOf('}', rootBegin) + 2;
534-
535538
if (!onlyVariables) {
539+
const rootBegin = string.indexOf('\n.markdown-body {');
540+
const rootEnd = string.indexOf('}', rootBegin) + 2;
536541
string = string.slice(0, rootEnd) + manuallyAddedStyle + string.slice(rootEnd);
537542
}
538543

0 commit comments

Comments
 (0)