|
| 1 | +# Theme Colors Manipulation |
| 2 | + |
| 3 | +## Arbitrary Color Values |
| 4 | + |
| 5 | +Usually, TextMate themes would expect the color values of each token to be a valid hex color value, the limitation is inherit from [`vscode-textmate`](https://github.com/microsoft/vscode-textmate). Since Shikiji v0.9.15, we introduced an automatic workaround by replacing non-hex color values with a placeholder and replacing them back on tokenization. This would allows you to use themes with arbitrary color values for the rendering without worrying about the technical details: |
| 6 | + |
| 7 | +```ts twoslash |
| 8 | +import { getHighlighter } from 'shikiji' |
| 9 | + |
| 10 | +const highlighter = await getHighlighter({ |
| 11 | + langs: ['javascript'], |
| 12 | + themes: [ |
| 13 | + { |
| 14 | + name: 'my-theme', |
| 15 | + settings: [ |
| 16 | + { |
| 17 | + scope: ['comment'], |
| 18 | + settings: { |
| 19 | + // use `rgb`, `hsl`, `hsla`, // [!code hl:3] |
| 20 | + // or any anything supported by your renderer |
| 21 | + foreground: 'rgb(128, 128, 128)' |
| 22 | + } |
| 23 | + }, |
| 24 | + { |
| 25 | + scope: ['string'], |
| 26 | + settings: { |
| 27 | + foreground: 'var(--code-string)' // CSS variable // [!code hl:1] |
| 28 | + } |
| 29 | + }, |
| 30 | + // ...more |
| 31 | + ], |
| 32 | + // Background and foreground colors // [!code hl:3] |
| 33 | + bg: 'var(--code-bg)', |
| 34 | + fg: 'var(--code-fg)' |
| 35 | + } |
| 36 | + ] |
| 37 | +}) |
| 38 | + |
| 39 | +const html = highlighter.codeToHtml('const foo = "bar"', { lang: 'javascript', theme: 'my-theme' }) |
| 40 | +``` |
| 41 | + |
| 42 | +::: info Notice |
| 43 | +Use this with caution as this will diverge from the TextMate theme compatibility. |
| 44 | + |
| 45 | +This may make the theme incompatible with non-web usage like [`shikiji-cli`](/packages/cli) and [`shikiji-monaco`](/packages/monaco). |
| 46 | +::: |
| 47 | + |
| 48 | +Learn more about how to [load themes](./load-theme). |
| 49 | + |
| 50 | +## Color Replacements |
| 51 | + |
| 52 | +You can also use the `colorReplacements` option to replace the color values of the theme. This is useful when you want to use a theme with a different color palette. It can be provided on both the theme object and the `codeToHast` `codeToHtml` options. |
| 53 | + |
| 54 | +## CSS Variables Theme |
| 55 | + |
| 56 | +::: warning Experimental |
| 57 | +This feature is experimental and may change without following semver. |
| 58 | +::: |
| 59 | + |
| 60 | +Shikiji provides a factory function helper `createCssVariablesTheme` to for creating a theme that uses CSS variables easier. Note that this theme is a lot less granular than most of the other themes and requires to define the CSS variables in your app. This is provided for easier migration from Shiki's [`css-variables theme`](https://github.com/shikijs/shiki/blob/main/docs/themes.md#theming-with-css-variables). For better highlighting result, we recommend construct the theme manually with [Arbitrary Color Values](#arbitrary-color-values) or use [Color Replacements](#color-replacements) to override existing theme. |
| 61 | + |
| 62 | +This theme is **not included by default** and requires to be registered explicitly: |
| 63 | + |
| 64 | +```ts twoslash |
| 65 | +import { createCssVariablesTheme, getHighlighter } from 'shikiji' |
| 66 | + |
| 67 | +// Create a custom CSS variables theme, the following are the default values |
| 68 | +const myTheme = createCssVariablesTheme({ // [!code hl:6] |
| 69 | + name: 'css-variables', |
| 70 | + variablePrefix: '--shiki-', |
| 71 | + variableDefaults: {}, |
| 72 | + fontStyle: true |
| 73 | +}) |
| 74 | + |
| 75 | +const highlighter = await getHighlighter({ |
| 76 | + langs: ['javascript'], |
| 77 | + themes: [myTheme] // register the theme // [!code hl] |
| 78 | +}) |
| 79 | + |
| 80 | +const html = highlighter.codeToHtml('const foo = "bar"', { |
| 81 | + lang: 'javascript', |
| 82 | + theme: 'css-variables' // use the theme // [!code hl] |
| 83 | +}) |
| 84 | +``` |
| 85 | + |
| 86 | +CSS variables example: |
| 87 | + |
| 88 | +```css |
| 89 | +:root { |
| 90 | + --shiki-foreground: #eeeeee; |
| 91 | + --shiki-background: #333333; |
| 92 | + --shiki-token-constant: #660000; |
| 93 | + --shiki-token-string: #770000; |
| 94 | + --shiki-token-comment: #880000; |
| 95 | + --shiki-token-keyword: #990000; |
| 96 | + --shiki-token-parameter: #aa0000; |
| 97 | + --shiki-token-function: #bb0000; |
| 98 | + --shiki-token-string-expression: #cc0000; |
| 99 | + --shiki-token-punctuation: #dd0000; |
| 100 | + --shiki-token-link: #ee0000; |
| 101 | + |
| 102 | + /* Only required if using lang: 'ansi' */ |
| 103 | + --shiki-ansi-black: #000000; |
| 104 | + --shiki-ansi-black-dim: #00000080; |
| 105 | + --shiki-ansi-red: #bb0000; |
| 106 | + --shiki-ansi-red-dim: #bb000080; |
| 107 | + --shiki-ansi-green: #00bb00; |
| 108 | + --shiki-ansi-green-dim: #00bb0080; |
| 109 | + --shiki-ansi-yellow: #bbbb00; |
| 110 | + --shiki-ansi-yellow-dim: #bbbb0080; |
| 111 | + --shiki-ansi-blue: #0000bb; |
| 112 | + --shiki-ansi-blue-dim: #0000bb80; |
| 113 | + --shiki-ansi-magenta: #ff00ff; |
| 114 | + --shiki-ansi-magenta-dim: #ff00ff80; |
| 115 | + --shiki-ansi-cyan: #00bbbb; |
| 116 | + --shiki-ansi-cyan-dim: #00bbbb80; |
| 117 | + --shiki-ansi-white: #eeeeee; |
| 118 | + --shiki-ansi-white-dim: #eeeeee80; |
| 119 | + --shiki-ansi-bright-black: #555555; |
| 120 | + --shiki-ansi-bright-black-dim: #55555580; |
| 121 | + --shiki-ansi-bright-red: #ff5555; |
| 122 | + --shiki-ansi-bright-red-dim: #ff555580; |
| 123 | + --shiki-ansi-bright-green: #00ff00; |
| 124 | + --shiki-ansi-bright-green-dim: #00ff0080; |
| 125 | + --shiki-ansi-bright-yellow: #ffff55; |
| 126 | + --shiki-ansi-bright-yellow-dim: #ffff5580; |
| 127 | + --shiki-ansi-bright-blue: #5555ff; |
| 128 | + --shiki-ansi-bright-blue-dim: #5555ff80; |
| 129 | + --shiki-ansi-bright-magenta: #ff55ff; |
| 130 | + --shiki-ansi-bright-magenta-dim: #ff55ff80; |
| 131 | + --shiki-ansi-bright-cyan: #55ffff; |
| 132 | + --shiki-ansi-bright-cyan-dim: #55ffff80; |
| 133 | + --shiki-ansi-bright-white: #ffffff; |
| 134 | + --shiki-ansi-bright-white-dim: #ffffff80; |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +If you are migrating form Shiki, some variables are renamed from Shiki's `css-variables`: |
| 139 | + |
| 140 | +| Shiki | Shikiji | |
| 141 | +| -------------------------- | -------------------- | |
| 142 | +| `--shiki-color-text` | `--shiki-forground` | |
| 143 | +| `--shiki-color-background` | `--shiki-background` | |
| 144 | +| `--shiki-color-ansi-*` | `--shiki-ansi-*` | |
0 commit comments