Skip to content

Commit 49b5145

Browse files
madcamposbluwysarah11918
authoredJul 17, 2024··
Feature: add support for shiki defaultColors option (#11341)
* feat: add shiki option for default color * feat: propagate shiki option for default color to astro config * feat: add tests for default color * chore: add change set * fix: add complete type to shiki default color config * fix: remove unneeded heavy shiki theme from fixture * fix: add literals to schema validation Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> * Update .changeset/cold-crabs-arrive.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * feat: improve changeset * grammar tweak --------- Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
1 parent f4870e5 commit 49b5145

File tree

11 files changed

+129
-0
lines changed

11 files changed

+129
-0
lines changed
 

‎.changeset/cold-crabs-arrive.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
'@astrojs/markdown-remark': minor
3+
'astro': minor
4+
---
5+
6+
Adds support for [Shiki's `defaultColor` option](https://shiki.style/guide/dual-themes#without-default-color).
7+
8+
This option allows you to override the values of a theme's inline style, adding only CSS variables to give you more flexibility in applying multiple color themes.
9+
10+
Configure `defaultColor: false` in your Shiki config to apply throughout your site, or pass to Astro's built-in `<Code>` component to style an individual code block.
11+
12+
```js title="astro.config.mjs"
13+
import { defineConfig } from 'astro/config';
14+
export default defineConfig({
15+
markdown: {
16+
shikiConfig: {
17+
themes: {
18+
light: 'github-light',
19+
dark: 'github-dark',
20+
},
21+
defaultColor: false,
22+
},
23+
},
24+
});
25+
```
26+
27+
```astro
28+
---
29+
import { Code } from 'astro:components';
30+
---
31+
<Code code={`const useMyColors = true`} lang="js" defaultColor={false} />
32+
```

‎packages/astro/src/core/config/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export const AstroConfigSchema = z.object({
321321
.or(z.custom<ShikiTheme>())
322322
)
323323
.default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.themes!),
324+
defaultColor: z.union([z.literal('light'), z.literal('dark'), z.string(), z.literal(false)]).optional(),
324325
wrap: z.boolean().or(z.null()).default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.wrap!),
325326
transformers: z
326327
.custom<ShikiTransformer>()

‎packages/astro/test/astro-markdown-shiki.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ describe('Astro Markdown Shiki', () => {
7878
);
7979
});
8080
});
81+
82+
describe('Default color', async () => {
83+
let fixture;
84+
85+
before(async () => {
86+
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/default-color/' });
87+
await fixture.build();
88+
});
89+
90+
it('Renders default color without themes', async () => {
91+
const html = await fixture.readFile('/index.html');
92+
const $ = cheerio.load(html);
93+
94+
assert.doesNotMatch($('pre').attr().style, /background-color/);
95+
});
96+
});
8197
});
8298

8399
describe('Languages', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
markdown: {
3+
syntaxHighlight: 'shiki',
4+
shikiConfig: {
5+
theme: 'github-light',
6+
themes: {
7+
light: 'github-light',
8+
dark: 'github-light'
9+
},
10+
defaultColor: false
11+
},
12+
},
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@test/astro-markdown-skiki-default-color",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<!-- Head Stuff -->
4+
</head>
5+
<body>
6+
<div class="container">
7+
<slot></slot>
8+
</div>
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
layout: ../layouts/content.astro
3+
---
4+
5+
# Hello world
6+
7+
```yaml
8+
apiVersion: v3
9+
kind: Pod
10+
metadata:
11+
name: rss-site
12+
labels:
13+
app: web
14+
spec:
15+
containers:
16+
- name: front-end
17+
image: nginx
18+
ports:
19+
- containerPort: 80
20+
- name: rss-reader
21+
image: nickchase/rss-php-nginx:v1
22+
ports:
23+
- containerPort: 88
24+
```

‎packages/markdown/remark/src/shiki.ts

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export async function createShikiHighlighter({
4242
langs = [],
4343
theme = 'github-dark',
4444
themes = {},
45+
defaultColor,
4546
wrap = false,
4647
transformers = [],
4748
}: ShikiConfig = {}): Promise<ShikiHighlighter> {
@@ -73,6 +74,7 @@ export async function createShikiHighlighter({
7374

7475
return highlighter.codeToHtml(code, {
7576
...themeOptions,
77+
defaultColor,
7678
lang,
7779
// NOTE: while we can spread `options.attributes` here so that Shiki can auto-serialize this as rendered
7880
// attributes on the top-level tag, it's not clear whether it is fine to pass all attributes as meta, as

‎packages/markdown/remark/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface ShikiConfig {
3939
langs?: LanguageRegistration[];
4040
theme?: ThemePresets | ThemeRegistration | ThemeRegistrationRaw;
4141
themes?: Record<string, ThemePresets | ThemeRegistration | ThemeRegistrationRaw>;
42+
defaultColor?: 'light' | 'dark' | string | false;
4243
wrap?: boolean | null;
4344
transformers?: ShikiTransformer[];
4445
}

‎packages/markdown/remark/test/shiki.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,20 @@ describe('shiki syntax highlighting', () => {
8585

8686
assert.match(html, /data-test="\{1,3-4\}"/);
8787
});
88+
89+
it('supports the defaultColor setting', async () => {
90+
const processor = await createMarkdownProcessor({
91+
shikiConfig: {
92+
themes: {
93+
light: 'github-light',
94+
dark: 'github-dark',
95+
},
96+
defaultColor: false,
97+
},
98+
});
99+
const { code } = await processor.render('```\ntest\n```');
100+
101+
// Doesn't have `color` or `background-color` properties.
102+
assert.doesNotMatch(code, /color:/);
103+
});
88104
});

‎pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.