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

Add "css-variables" theme, support dark mode #212

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,65 @@ shiki.getHighlighter({
theme: t
})
```
## Dark Mode Support

Because Shiki generates themes at build time, client-side theme switching support is not built in. There are two popular two options for supporting something like Dark Mode with Shiki.

#### 1. Use the "css-variables" theme.

This gives you access to CSS variable styling, which you can control across Dark and Light mode. See the [Theming with CSS Variables](#theming-with-css-variables) section below for more details.
#### 2. Generate two Shiki code blocks, one for each theme.

```css
@media (prefers-color-scheme: light) {
.shiki.dark-plus {
display: none;
}
}
@media (prefers-color-scheme: dark) {
.shiki.light-plus {
display: none;
}
}
```

## Theming with CSS Variables

Shiki handles all theme logic at build-time, so that the browser only ever sees already-computed `style="color: #XXXXXX"` attributes. This allows more granular theme support in a way that doesn't require any additional steps to add global CSS to your page.

In some cases, a user may require custom client-side theming via CSS. To support this, you may use the `css-variables` theme with Shiki. This is a special theme that uses CSS variables for colors instead of hardcoded values. Each token in your code block is given an attribute of `style="color: var(--code-block-XXX)"` which you can use to style your code blocks using CSS.


```js
const shiki = require('shiki')
shiki.getHighlighter({theme: 'css-variables'})
```

Note that this client-side theme is less granular than most other supported VSCode themes. Also, be aware that this will generate unstyled code if you do not define these CSS variables somewhere else on your page:

```html
<style>
:root {
--code-foreground: #123456;
--code-background: #ABCDEF;
--code-token-default: #123456;
--code-token-constant: #123456;
--code-token-string: #123456;
--code-token-comment: #123456;
--code-token-keyword: #123456;
--code-token-parameter: #123456;
--code-token-function: #123456;
--code-token-string-expression: #123456;
--code-token-info: #123456;
--code-token-warn: #123456;
--code-token-warn: #123456;
--code-token-debug: #123456;
--code-token-strong: #123456;
--code-token-punctuation: #123456;
--code-token-link: #123456;
}
</style>
```
## All Themes

```ts
Expand Down
15 changes: 15 additions & 0 deletions packages/shiki/src/__tests__/__snapshots__/cssVars.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`The theme with css-variables renders correctly 1`] = `
"<pre class=\\"shiki\\" style=\\"background-color: var(--code-background)\\"><code><span class=\\"line\\"></span>
<span class=\\"line\\"><span style=\\"color: #000000\\">import { getHighlighter } from </span><span style=\\"color: #000000\\">&#39;../index&#39;</span></span>
<span class=\\"line\\"></span>
<span class=\\"line\\"><span style=\\"color: #000000\\">test(</span><span style=\\"color: #000000\\">&#39;The theme with css-variables renders correctly&#39;</span><span style=\\"color: #000000\\">, async () =&gt; {</span></span>
<span class=\\"line\\"><span style=\\"color: #000000\\"> const highlighter = await getHighlighter({</span></span>
<span class=\\"line\\"><span style=\\"color: #000000\\"> theme: </span><span style=\\"color: #000000\\">&#39;css-variables&#39;</span></span>
<span class=\\"line\\"><span style=\\"color: #000000\\"> })</span></span>
<span class=\\"line\\"><span style=\\"color: #000000\\"> const out = highlighter.codeToHtml(</span><span style=\\"color: #000000\\">&quot;console.log(&#39;shiki&#39;);&quot;</span><span style=\\"color: #000000\\">, </span><span style=\\"color: #000000\\">&#39;js&#39;</span><span style=\\"color: #000000\\">)</span></span>
<span class=\\"line\\"><span style=\\"color: #000000\\"> expect(out).toMatchSnapshot()</span></span>
<span class=\\"line\\"><span style=\\"color: #000000\\">})</span></span>
<span class=\\"line\\"></span></code></pre>"
`;
22 changes: 22 additions & 0 deletions packages/shiki/src/__tests__/cssVars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getHighlighter } from '../index'

test('The theme with css-variables renders correctly', async () => {
const highlighter = await getHighlighter({
theme: 'css-variables'
})

const kindOfAQuine = `
import { getHighlighter } from '../index'

test('The theme with css-variables renders correctly', async () => {
const highlighter = await getHighlighter({
theme: 'css-variables'
})
const out = highlighter.codeToHtml("console.log('shiki');", 'js')
expect(out).toMatchSnapshot()
})
`

const out = highlighter.codeToHtml(kindOfAQuine, 'js')
expect(out).toMatchSnapshot()
})
200 changes: 200 additions & 0 deletions packages/shiki/themes/css-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"name": "css-variables",
"type": "light",
"colors": {
"editor.foreground": "var(--code-foreground)",
"editor.background": "var(--code-background)"
},
"tokenColors": [
{
"settings": {
"foreground": "var(--code-token-default)"
}
},
{
"scope": [
"keyword.operator.accessor",
"meta.group.braces.round.function.arguments",
"meta.template.expression",
"markup.fenced_code meta.embedded.block"
],
"settings": {
"foreground": "var(--code-token-default)"
}
},
{
"scope": "emphasis",
"settings": {
"fontStyle": "italic"
}
},
{
"scope": ["strong", "markup.heading.markdown", "markup.bold.markdown"],
"settings": {
"fontStyle": "bold"
}
},
{
"scope": ["markup.italic.markdown"],
"settings": {
"fontStyle": "italic"
}
},
{
"scope": "meta.link.inline.markdown",
"settings": {
"fontStyle": "underline",
"foreground": "var(--code-token-constant)"
}
},
{
"scope": ["string", "markup.fenced_code", "markup.inline"],
"settings": {
"foreground": "var(--code-token-string)"
}
},
{
"scope": ["comment", "string.quoted.docstring.multi"],
"settings": {
"foreground": "var(--code-token-comment)"
}
},
{
"scope": [
"constant.numeric",
"constant.language",
"constant.other.placeholder",
"constant.character.format.placeholder",
"variable.language.this",
"variable.other.object",
"variable.other.class",
"variable.other.constant",
"meta.property-name",
"meta.property-value",
"support"
],
"settings": {
"foreground": "var(--code-token-constant)"
}
},
{
"scope": [
"keyword",
"storage.modifier",
"storage.type",
"storage.control.clojure",
"entity.name.function.clojure",
"entity.name.tag.yaml",
"support.function.node",
"support.type.property-name.json",
"punctuation.separator.key-value",
"punctuation.definition.template-expression"
],
"settings": {
"foreground": "var(--code-token-keyword)"
}
},
{
"scope": "variable.parameter.function",
"settings": {
"foreground": "var(--code-token-parameter)"
}
},
{
"scope": [
"support.function",
"entity.name.type",
"entity.other.inherited-class",
"meta.function-call",
"meta.instance.constructor",
"entity.other.attribute-name",
"entity.name.function",
"constant.keyword.clojure"
],
"settings": {
"foreground": "var(--code-token-function)"
}
},
{
"scope": [
"entity.name.tag",
"string.quoted",
"string.regexp",
"string.interpolated",
"string.template",
"string.unquoted.plain.out.yaml",
"keyword.other.template"
],
"settings": {
"foreground": "var(--code-token-string-expression)"
}
},
{
"scope": "token.info-token",
"settings": {
"foreground": "var(--code-token-info)"
}
},
{
"scope": "token.warn-token",
"settings": {
"foreground": "var(--code-token-warn)"
}
},
{
"scope": "token.error-token",
"settings": {
"foreground": "var(--code-token-warn)"
}
},
{
"scope": "token.debug-token",
"settings": {
"foreground": "var(--code-token-debug)"
}
},
{
"scope": ["strong", "markup.heading.markdown", "markup.bold.markdown"],
"settings": {
"foreground": "var(--code-token-strong)"
}
},
{
"scope": [
"punctuation.definition.arguments",
"punctuation.definition.dict",
"punctuation.separator",
"meta.function-call.arguments"
],
"settings": {
"foreground": "var(--code-token-punctuation)"
}
},
{
"name": "[Custom] Markdown links",
"scope": ["markup.underline.link", "punctuation.definition.metadata.markdown"],
"settings": {
"foreground": "var(--code-token-link)"
}
},
{
"name": "[Custom] Markdown list",
"scope": ["beginning.punctuation.definition.list.markdown"],
"settings": {
"foreground": "var(--code-token-string)"
}
},
{
"name": "[Custom] Markdown punctuation definition brackets",
"scope": [
"punctuation.definition.string.begin.markdown",
"punctuation.definition.string.end.markdown",
"string.other.link.title.markdown",
"string.other.link.description.markdown"
],
"settings": {
"foreground": "var(--code-token-keyword)"
}
}
]
}