Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add support for shiki defaultColors option #11341

Merged
merged 12 commits into from
Jul 17, 2024
Merged
6 changes: 6 additions & 0 deletions .changeset/cold-crabs-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/markdown-remark': minor
'astro': minor
---

Enable the "defaultColor" option from shiki
Copy link
Member

@sarah11918 sarah11918 Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Enable the "defaultColor" option from shiki
Adds support for [Shiki's "defaultColor" option](https://shiki.style/guide/dual-themes#without-default-color).
This option allows you to override the values of a theme's CSS variables and use your own colors.
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.
```js title="astro.config.mjs"
import { defineConfig } from 'astro/config';
export default defineConfig({
markdown: {
shikiConfig: {
themes: {
light: 'github-light',
dark: 'github-dark',
},
defaultColor: false,
},
},
});
```
```js
---
import { Code } from 'astro:components';
---
<Code code={`const useMyColors = true`} lang="js" defaultColor=false />

OK, thanks for waiting! I'm not sure whether all of this is entirely accurate, but this is the general style of message I'm thinking of here. So, using this as a model, can you make this accurate/appropriate? I think this would be a helpful way to introduce this new option!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! I've added a couple small changes. I hope that is okay

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and I just updated some grammar and it's great, thank you!

1 change: 1 addition & 0 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export const AstroConfigSchema = z.object({
.or(z.custom<ShikiTheme>())
)
.default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.themes!),
defaultColor: z.union([z.string(), z.literal(false)]).optional(),
wrap: z.boolean().or(z.null()).default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.wrap!),
transformers: z
.custom<ShikiTransformer>()
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/test/astro-markdown-shiki.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ describe('Astro Markdown Shiki', () => {
);
});
});

describe('Default color', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/default-color/' });
await fixture.build();
});

it('Renders default color without themes', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

assert.doesNotMatch($('pre').attr().style, /background-color/);
});
});
});

describe('Languages', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: {
theme: 'github-light',
themes: {
light: 'github-light',
dark: 'github-light'
},
defaultColor: false
},
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-markdown-skiki-default-color",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<!-- Head Stuff -->
</head>
<body>
<div class="container">
<slot></slot>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
layout: ../layouts/content.astro
---

# Hello world

```yaml
apiVersion: v3
kind: Pod
metadata:
name: rss-site
labels:
app: web
spec:
containers:
- name: front-end
image: nginx
ports:
- containerPort: 80
- name: rss-reader
image: nickchase/rss-php-nginx:v1
ports:
- containerPort: 88
```
2 changes: 2 additions & 0 deletions packages/markdown/remark/src/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function createShikiHighlighter({
langs = [],
theme = 'github-dark',
themes = {},
defaultColor,
wrap = false,
transformers = [],
}: ShikiConfig = {}): Promise<ShikiHighlighter> {
Expand Down Expand Up @@ -73,6 +74,7 @@ export async function createShikiHighlighter({

return highlighter.codeToHtml(code, {
...themeOptions,
defaultColor,
lang,
// NOTE: while we can spread `options.attributes` here so that Shiki can auto-serialize this as rendered
// attributes on the top-level tag, it's not clear whether it is fine to pass all attributes as meta, as
Expand Down
1 change: 1 addition & 0 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ShikiConfig {
langs?: LanguageRegistration[];
theme?: ThemePresets | ThemeRegistration | ThemeRegistrationRaw;
themes?: Record<string, ThemePresets | ThemeRegistration | ThemeRegistrationRaw>;
defaultColor?: 'light' | 'dark' | string | false;
wrap?: boolean | null;
transformers?: ShikiTransformer[];
}
Expand Down
16 changes: 16 additions & 0 deletions packages/markdown/remark/test/shiki.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,20 @@ describe('shiki syntax highlighting', () => {

assert.match(html, /data-test="\{1,3-4\}"/);
});

it('supports the defaultColor setting', async () => {
const processor = await createMarkdownProcessor({
shikiConfig: {
themes: {
light: 'github-light',
dark: 'github-dark',
},
defaultColor: false,
},
});
const { code } = await processor.render('```\ntest\n```');

// Doesn't have `color` or `background-color` properties.
assert.doesNotMatch(code, /color:/);
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading