Skip to content

Commit 1fd2b53

Browse files
authored
feat(astro): add expressiveCodeThemes option (#461)
1 parent a4ebd50 commit 1fd2b53

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

docs/tutorialkit.dev/src/content/docs/reference/configuration.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,19 @@ See [Overriding Components](/guides/overriding-components/) for details of all t
529529

530530
Controls whether the tutorial routes are automatically added to your project. When set to `true`, it additionally adds a redirect from `/` to the first tutorial.
531531
Use `"tutorial-only"` to only add the tutorial routes without the redirect.
532+
533+
### `expressiveCodeThemes`
534+
535+
**type**: `[ThemeObjectOrShikiThemeName, ThemeObjectOrShikiThemeName]`<br/>
536+
**default**: `['light-plus', 'dark-plus']`
537+
538+
Controls which themes are applied to [Expressive Code](https://expressive-code.com/).
539+
540+
```ts
541+
tutorialkit({
542+
expressiveCodeThemes: ['catppuccin-latte', 'catppuccin-mocha'],
543+
});
544+
```
545+
546+
Make sure to provide a light and a dark theme if you want support for both light and dark modes.
547+
See the [themes](https://expressive-code.com/guides/themes/) section of Expressive Code to learn more.

packages/astro/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fileURLToPath } from 'node:url';
22
import type { AstroConfig, AstroIntegration } from 'astro';
3-
import type { ExpressiveCodePlugin } from 'astro-expressive-code';
3+
import type { ExpressiveCodePlugin, ThemeObjectOrShikiThemeName } from 'astro-expressive-code';
44
import { extraIntegrations } from './integrations.js';
55
import { updateMarkdownConfig } from './remark/index.js';
66
import { tutorialkitCore } from './vite-plugins/core.js';
@@ -67,6 +67,15 @@ export interface Options {
6767
* @default []
6868
*/
6969
expressiveCodePlugins?: ExpressiveCodePlugin[];
70+
71+
/**
72+
* Themes for expressive code.
73+
* Make sure to provide a light and a dark theme if you want support for both light and dark modes.
74+
* Default values are ['light-plus', 'dark-plus']
75+
*
76+
* @default ['light-plus', 'dark-plus']
77+
*/
78+
expressiveCodeThemes?: [ThemeObjectOrShikiThemeName, ThemeObjectOrShikiThemeName];
7079
}
7180

7281
export default function createPlugin({
@@ -75,6 +84,7 @@ export default function createPlugin({
7584
isolation,
7685
enterprise,
7786
expressiveCodePlugins = [],
87+
expressiveCodeThemes,
7888
}: Options = {}): AstroIntegration {
7989
const webcontainerFiles = new WebContainerFiles();
8090

@@ -149,7 +159,7 @@ export default function createPlugin({
149159
config.integrations.splice(
150160
selfIndex + 1,
151161
0,
152-
...extraIntegrations({ root: fileURLToPath(config.root), expressiveCodePlugins }),
162+
...extraIntegrations({ root: fileURLToPath(config.root), expressiveCodePlugins, expressiveCodeThemes }),
153163
);
154164
},
155165
'astro:config:done'({ config }) {

packages/astro/src/integrations.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@ import react from '@astrojs/react';
44
import { pluginCollapsibleSections } from '@expressive-code/plugin-collapsible-sections';
55
import { pluginLineNumbers } from '@expressive-code/plugin-line-numbers';
66
import { getInlineContentForPackage } from '@tutorialkit/theme';
7-
import expressiveCode, { type ExpressiveCodePlugin } from 'astro-expressive-code';
7+
import expressiveCode, { type ExpressiveCodePlugin, type ThemeObjectOrShikiThemeName } from 'astro-expressive-code';
88
import UnoCSS from 'unocss/astro';
99

1010
export function extraIntegrations({
1111
root,
1212
expressiveCodePlugins = [],
13+
expressiveCodeThemes = ['light-plus', 'dark-plus'],
1314
}: {
1415
root: string;
1516
expressiveCodePlugins?: ExpressiveCodePlugin[];
17+
18+
/**
19+
* Themes for Expressive Code.
20+
* Takes a tuple of themes, e.g. `[lightTheme, darkTheme]`.
21+
*/
22+
expressiveCodeThemes?: [ThemeObjectOrShikiThemeName, ThemeObjectOrShikiThemeName];
1623
}) {
1724
return [
1825
react(),
1926
expressiveCode({
2027
plugins: [pluginCollapsibleSections(), pluginLineNumbers(), ...expressiveCodePlugins],
21-
themes: ['dark-plus', 'light-plus'],
28+
themes: expressiveCodeThemes,
2229
customizeTheme: (theme) => {
2330
const isDark = theme.type === 'dark';
2431

@@ -35,13 +42,7 @@ export function extraIntegrations({
3542
};
3643
},
3744
themeCssSelector: (theme) => {
38-
let customThemeName = 'light';
39-
40-
if (theme.name === 'dark-plus') {
41-
customThemeName = 'dark';
42-
}
43-
44-
return `[data-theme='${customThemeName}']`;
45+
return `[data-theme='${theme.type}']`;
4546
},
4647
defaultProps: {
4748
showLineNumbers: false,

0 commit comments

Comments
 (0)