Skip to content

Commit cf55c61

Browse files
committed
Add transparentBackground option
Fixes #16
1 parent 776016d commit cf55c61

4 files changed

Lines changed: 78 additions & 24 deletions

File tree

cli.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ const cli = meow(
99
github-markdown-css > <filename>
1010
1111
Options
12-
--list List available themes
12+
--list List available themes
1313
1414
Set theme:
15-
--light Light theme name from --list values
16-
--dark Dark theme name from --list values
17-
--theme Theme name: 'auto', light', 'dark', or another from --list values.
18-
'auto' means using the media query (prefers-color-scheme)
19-
to switch between the 'light' and 'dark' theme.
15+
--light Light theme name from --list values
16+
--dark Dark theme name from --list values
17+
--theme Theme name: 'auto', light', 'dark', or another from --list values.
18+
'auto' means using the media query (prefers-color-scheme)
19+
to switch between the 'light' and 'dark' theme.
2020
2121
Output options:
22-
--preserve-variables Preserve variables in the output. Only applies if light
23-
and dark themes match or if type is not 'auto'
24-
--only-style Only output the styles, forces --preserve-variables on
25-
--only-variables Only output the variables for the specified themes
26-
--no-use-fixture Exclude generated classes that come from GitHub Markdown API rendered fixture.md
27-
--root-selector Specify the root selector when outputting styles, default '.markdown-body'
22+
--preserve-variables Preserve variables in the output. Only applies if light
23+
and dark themes match or if type is not 'auto'
24+
--only-style Only output the styles, forces --preserve-variables on
25+
--only-variables Only output the variables for the specified themes
26+
--no-use-fixture Exclude generated classes that come from GitHub Markdown API rendered fixture.md
27+
--root-selector Specify the root selector when outputting styles, default '.markdown-body'
28+
--transparent-background Make the background transparent instead of white/black
2829
2930
Examples
3031
$ github-markdown-css --list
@@ -79,6 +80,9 @@ const cli = meow(
7980
type: 'boolean',
8081
default: true,
8182
},
83+
transparentBackground: {
84+
type: 'boolean',
85+
},
8286
},
8387
},
8488
);
@@ -91,6 +95,7 @@ const {
9195
onlyVariables,
9296
rootSelector,
9397
useFixture,
98+
transparentBackground,
9499
} = cli.flags;
95100

96101
let {light, dark} = cli.flags;
@@ -147,5 +152,6 @@ console.log(
147152
onlyVariables,
148153
rootSelector,
149154
useFixture,
155+
transparentBackground,
150156
}),
151157
);

index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ In "single" mode, the output will apply the values of all variables to the rules
230230
@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.
231231
@param {boolean} [options.useFixture=true] - Include extra styles from GitHub Flavored Markdown, like code snippets.
232232
@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`.
233+
@param {boolean} [options.transparentBackground=false] - If `true`, the background color will be set to `transparent`. Useful when you want to embed the Markdown content in a page with a custom background color.
233234
*/
234235
// eslint-disable-next-line complexity
235236
export default async function getCSS({
@@ -241,6 +242,7 @@ export default async function getCSS({
241242
onlyStyles = false,
242243
useFixture = true,
243244
rootSelector = '.markdown-body',
245+
transparentBackground = false,
244246
} = {}) {
245247
if (onlyVariables && onlyStyles) {
246248
// Would result in an empty output
@@ -300,6 +302,21 @@ export default async function getCSS({
300302
return colors.join('\n');
301303
}
302304

305+
if (transparentBackground) {
306+
for (const theme of [colors[light], colors[dark], colors.shared]) {
307+
if (!theme) {
308+
continue;
309+
}
310+
311+
const property = '--bgColor-default';
312+
const index = theme.findIndex(declaration => declaration.prop === property);
313+
if (index !== -1) {
314+
theme[index].value = 'transparent';
315+
theme[property] = 'transparent';
316+
}
317+
}
318+
}
319+
303320
rules = reverseUnique(rules, rule => rule.toString());
304321

305322
rules = classifyRules(rules).rules;

readme.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ console.log(await githubMarkdownCss({
4646
// Set the root selector of the rendered Markdown body as it should appear
4747
// in the output CSS. Defaults to `.markdown-body`.
4848
rootSelector: '.markdown-body',
49+
// Make the background transparent instead of white/black. Useful when
50+
// embedding the Markdown content in a page with a custom background.
51+
transparentBackground: false,
4952
}
5053
));
5154
//=> '.markdown-body { …'
@@ -64,22 +67,23 @@ $ github-markdown-css --help
6467
github-markdown-css > <filename>
6568
6669
Options
67-
--list List available themes
70+
--list List available themes
6871
6972
Set theme:
70-
--light Light theme name from --list values
71-
--dark Dark theme name from --list values
72-
--theme Theme name: 'auto', light', 'dark', or another from --list values.
73-
'auto' means using the media query (prefers-color-scheme)
74-
to switch between the 'light' and 'dark' theme.
73+
--light Light theme name from --list values
74+
--dark Dark theme name from --list values
75+
--theme Theme name: 'auto', light', 'dark', or another from --list values.
76+
'auto' means using the media query (prefers-color-scheme)
77+
to switch between the 'light' and 'dark' theme.
7578
7679
Output options:
77-
--preserve-variables Preserve variables in the output. Only applies if light
78-
and dark themes match or if type is not 'auto'
79-
--only-style Only output the styles, forces --preserve-variables on
80-
--only-variables Only output the variables for the specified themes
81-
--no-use-fixture Exclude generated classes that come from GitHub Markdown API rendered fixture.md
82-
--root-selector Specify the root selector when outputting styles, default '.markdown-body'
80+
--preserve-variables Preserve variables in the output. Only applies if light
81+
and dark themes match or if type is not 'auto'
82+
--only-style Only output the styles, forces --preserve-variables on
83+
--only-variables Only output the variables for the specified themes
84+
--no-use-fixture Exclude generated classes that come from GitHub Markdown API rendered fixture.md
85+
--root-selector Specify the root selector when outputting styles, default '.markdown-body'
86+
--transparent-background Make the background transparent instead of white/black
8387
8488
Examples
8589
$ github-markdown-css --list

test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import assert from 'node:assert/strict';
12
import fs from 'node:fs';
3+
import {test} from 'node:test';
24
import {renderMarkdown} from './utilities.js';
35
import githubMarkdownCss from './index.js';
46

@@ -87,3 +89,28 @@ ${fixture}
8789
`;
8890

8991
fs.writeFileSync('dist/vars/index.html', htmlVars);
92+
93+
test('transparentBackground - auto mode', async () => {
94+
const css = await githubMarkdownCss({transparentBackground: true});
95+
assert.ok(css.includes('--bgColor-default: transparent'), 'should set --bgColor-default to transparent in auto mode');
96+
});
97+
98+
test('transparentBackground - single mode inlines transparent', async () => {
99+
const css = await githubMarkdownCss({light: 'light', dark: 'light', transparentBackground: true});
100+
assert.ok(css.includes('background-color: transparent'), 'should inline transparent as background-color');
101+
});
102+
103+
test('transparentBackground - single mode with preserveVariables', async () => {
104+
const css = await githubMarkdownCss({light: 'dark', dark: 'dark', transparentBackground: true, preserveVariables: true});
105+
assert.ok(css.includes('--bgColor-default: transparent'), 'should set --bgColor-default to transparent');
106+
});
107+
108+
test('transparentBackground - onlyVariables', async () => {
109+
const css = await githubMarkdownCss({transparentBackground: true, onlyVariables: true});
110+
assert.ok(css.includes('--bgColor-default: transparent'), 'should set --bgColor-default to transparent');
111+
});
112+
113+
test('no transparentBackground by default', async () => {
114+
const css = await githubMarkdownCss();
115+
assert.ok(!css.includes('--bgColor-default: transparent'), 'should not have transparent background by default');
116+
});

0 commit comments

Comments
 (0)