@@ -39,17 +39,6 @@ function extractColors(colors, name, ast) {
39
39
}
40
40
}
41
41
}
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' ) ;
53
42
}
54
43
55
44
// https://github.com/gjtorikian/html-pipeline/blob/main/lib/html_pipeline/sanitization_filter.rb
@@ -200,9 +189,9 @@ function extractStyles(rules, ast) {
200
189
}
201
190
202
191
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)'
204
193
if ( declaration . value . includes ( 'Color' ) ) {
205
- declaration . value = declaration . value . replace ( / v a r \( [ ^ , ] + , \s * ( v a r \( - - c o l o r - .+ ?\) ) \) / , '$1 ' ) ;
194
+ declaration . value = declaration . value . replace ( / v a r \( ( [ ^ , ] + ) , \s * ( v a r \( - - c o l o r - .+ ?\) ) \) / , 'var($1) ' ) ;
206
195
}
207
196
}
208
197
@@ -319,9 +308,6 @@ function applyColors(colors, rules) {
319
308
if ( declaration . value . includes ( 'var(' ) ) {
320
309
declaration . value = declaration . value . replaceAll ( / v a r \( ( .+ ?) \) / g, ( match , name ) => {
321
310
name = name . split ( ',' ) [ 0 ] ;
322
- if ( name === '--color-text-primary' ) {
323
- name = '--color-fg-default' ;
324
- }
325
311
326
312
if ( name in colors ) {
327
313
return colors [ name ] ;
@@ -387,6 +373,7 @@ In "single" mode, the output will apply the values of all variables to the rules
387
373
@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.
388
374
@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`.
389
375
*/
376
+ // eslint-disable-next-line complexity
390
377
export default async function getCSS ( {
391
378
light = 'light' ,
392
379
dark = 'dark' ,
@@ -414,12 +401,13 @@ export default async function getCSS({
414
401
const links = unique ( body . match ( / (?< = h r e f = " ) .+ ?\. c s s / g) ) ;
415
402
const contents = await Promise . all ( links . map ( url => cachedGot ( url ) ) ) ;
416
403
404
+ const shared = [ ] ;
417
405
const colors = [ ] ;
418
406
let rules = [ ] ;
419
407
420
408
for ( const [ url , cssText ] of zip ( links , contents ) ) {
421
409
// Get the name of a css file without the cache prevention number
422
- const match = url . match ( / (?< = \/ ) \w + (? = - \w + \. c s s $ ) / ) ;
410
+ const match = url . match ( / (?< = \/ ) [ - \w ] + (? = - \w + \. c s s $ ) / ) ;
423
411
if ( ! match ) {
424
412
continue ;
425
413
}
@@ -428,6 +416,11 @@ export default async function getCSS({
428
416
const patched = patchCSSText ( cssText ) ;
429
417
const ast = css . parse ( patched ) ;
430
418
419
+ // Primer*.css contains styles and variables that apply to all themes
420
+ if ( name . startsWith ( 'primer' ) ) {
421
+ extractColors ( shared , name , ast ) ;
422
+ }
423
+
431
424
// If it's a theme variable file extract colors, otherwise extract style
432
425
if ( / ^ ( l i g h t | d a r k ) / . test ( name ) ) {
433
426
extractColors ( colors , name , ast ) ;
@@ -441,6 +434,19 @@ export default async function getCSS({
441
434
return colors . map ( ( { name} ) => name ) . join ( '\n' ) ;
442
435
}
443
436
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
+
444
450
rules = reverseUnique ( rules , rule => {
445
451
const selector = rule . selectors . join ( ',' ) ;
446
452
const body = rule . declarations . map ( ( { property, value} ) => `${ property } : ${ value } ` ) . join ( ';' ) ;
@@ -529,10 +535,9 @@ export default async function getCSS({
529
535
530
536
let string = css . stringify ( { type : 'stylesheet' , stylesheet : { rules} } ) ;
531
537
532
- const rootBegin = string . indexOf ( '\n.markdown-body {' ) ;
533
- const rootEnd = string . indexOf ( '}' , rootBegin ) + 2 ;
534
-
535
538
if ( ! onlyVariables ) {
539
+ const rootBegin = string . indexOf ( '\n.markdown-body {' ) ;
540
+ const rootEnd = string . indexOf ( '}' , rootBegin ) + 2 ;
536
541
string = string . slice ( 0 , rootEnd ) + manuallyAddedStyle + string . slice ( rootEnd ) ;
537
542
}
538
543
0 commit comments