Skip to content

Commit

Permalink
feat: Added the ability to add custom themes/languages to Shiki (#2518)
Browse files Browse the repository at this point in the history
* Replaced `shikiTheme` with `shikiConfig`

* Code.astro now accepts custom themes/langs

* Updated docs

* Updated tests

* Fixed language loading

* Added customization examples

* Updated documentation

* Added more tests

* Changelogs

* Changed some spaces to tabs

* Fixed typo in changesets

* Moved tests fixtures

* Rolled back changes to with-markdown-shiki

* Removed lang example in docs

* Optimized Code component

* Try to fix windows errors

* Try to see if this new tests work
  • Loading branch information
JuanM04 authored Feb 7, 2022
1 parent 0caf916 commit 2bc9154
Show file tree
Hide file tree
Showing 42 changed files with 1,429 additions and 56 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-lions-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/markdown-remark': patch
---

Added the ability to use custom themes and langs with Shiki (`<Code />` and `@astrojs/markdown-remark`)
5 changes: 5 additions & 0 deletions .changeset/serious-glasses-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': patch
---

Added `wrap` to Shiki config
11 changes: 9 additions & 2 deletions docs/src/pages/en/guides/markdown-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,21 @@ export default {
{
// Pick a syntax highlighter. Can be 'prism' (default), 'shiki' or false to disable any highlighting.
syntaxHighlight: 'prism',
// If you are using shiki, here you can define a global theme.
shikiTheme: 'github-dark',
// If you are using shiki, here you can define a global theme and
// add custom languages.
shikiConfig: {
theme: 'github-dark',
langs: [],
wrap: false,
},
},
],
},
};
```

You can read more about custom Shiki [themes](https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme) and [languages](https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki).

## Markdown Pages

Astro treats any `.md` files inside of the `/src/pages` directory as pages. These files can contain frontmatter, but are otherwise processed as plain markdown files and do not support components. If you're looking to embed rich components in your markdown, take a look at the [Markdown Component](#astros-markdown-component) section.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/en/reference/builtin-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Code } from 'astro/components';
<Code code={`const foo = 'bar';`} lang="js" wrap />
```

This component provides syntax highlighting for code blocks at build time (no client-side JavaScript included). The component is powered internally by shiki and it supports all popular [themes](https://github.com/shikijs/shiki/blob/main/docs/themes.md) and [languages](https://github.com/shikijs/shiki/blob/main/docs/languages.md).
This component provides syntax highlighting for code blocks at build time (no client-side JavaScript included). The component is powered internally by shiki and it supports all popular [themes](https://github.com/shikijs/shiki/blob/main/docs/themes.md) and [languages](https://github.com/shikijs/shiki/blob/main/docs/languages.md). Plus, you can add your custom themes and languages by passing them to `theme` and `lang` respectively.

You can also use the `<Prism />` component for syntax highlighting powered by the [Prism](https://prismjs.com/) syntax highlighting library. This is the library that Astro's Markdown uses by default. However, we will be transitioning all usage over to `<Code>` as we move towards our v1.0 release.

Expand Down
6 changes: 5 additions & 1 deletion examples/with-markdown-shiki/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export default /** @type {import('astro').AstroUserConfig} */ ({
astroRemark,
{
syntaxHighlight: 'shiki',
shikiTheme: 'dracula',
shikiConfig: {
theme: 'dracula',
// Learn more about this configuration here:
// https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting
},
},
],
},
Expand Down
29 changes: 20 additions & 9 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ export interface Props {
/** The code to highlight. Required. */
code: string;
/**
* The language of your code. Defaults to "plaintext".
* Supports all languages listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md
* The language of your code.
* Supports all languages listed here: https://github.com/shikijs/shiki/blob/main/docs/languages.md#all-languages
* Instructions for loading a custom language: https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki
*
* @default "plaintext"
*/
lang?: string;
lang?: shiki.Lang | shiki.ILanguageRegistration;
/**
* The styling theme. Defaults to "github-dark".
* Supports all themes listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md
* The styling theme.
* Supports all themes listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md#all-themes
* Instructions for loading a custom theme: https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme
*
* @default "github-dark"
*/
theme?: string;
theme?: shiki.IThemeRegistration;
/**
* Enable word wrapping. Defaults to "false".
* Enable word wrapping.
* - true: enabled.
* - false: enabled.
* - null: All overflow styling removed. Code will overflow the element by default.
*
* @default false
*/
wrap?: boolean | null;
}
Expand All @@ -42,8 +49,12 @@ function repairShikiTheme(html: string): string {
return html;
}
const highlighter = await shiki.getHighlighter({ theme });
const _html = highlighter.codeToHtml(code, { lang });
const highlighter = await shiki.getHighlighter({
theme,
// Load custom lang if passed an object, otherwise load the default
langs: typeof lang !== 'string' ? [lang] : undefined
});
const _html = highlighter.codeToHtml(code, { lang: typeof lang === 'string' ? lang : lang.id });
const html = repairShikiTheme(_html);
---

Expand Down
13 changes: 12 additions & 1 deletion packages/astro/test/astro-component-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ before(async () => {
await fixture.build();
});

describe('<Code', () => {
describe('<Code>', () => {
it('<Code> without lang or theme', async () => {
let html = await fixture.readFile('/no-lang/index.html');
const $ = cheerio.load(html);
Expand Down Expand Up @@ -81,4 +81,15 @@ describe('<Code', () => {
'color: var(--astro-code-color-text)',
]);
});

it('<Code> with custom theme and lang', async () => {
let html = await fixture.readFile('/imported/index.html');
const $ = cheerio.load(html);

expect($('#theme > pre')).to.have.lengthOf(1);
expect($('#theme > pre').attr('style'), 'background-color: #FDFDFE; overflow-x: auto;');

expect($('#lang > pre')).to.have.lengthOf(1);
expect($('#lang > pre > code span').length).to.equal(3);
});
});
227 changes: 190 additions & 37 deletions packages/astro/test/astro-markdown-shiki.test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,206 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import markdownRemark from '@astrojs/markdown-remark';

describe('Astro Markdown Shiki', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-markdown-shiki/',
markdownOptions: {
render: [
markdownRemark,
{
syntaxHighlight: 'shiki',
shikiTheme: 'github-light',
},
],
},
buildOptions: {
sitemap: false,
},
});
await fixture.build();
describe('Render shiki', () => {
let fixture;

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

it('Can render markdown with shiki', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

// There should be no HTML from Prism
expect($('.token')).to.have.lengthOf(0);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').hasClass('astro-code')).to.equal(true);
expect($('pre').attr().style).to.equal('background-color: #0d1117; overflow-x: auto;');
});

it('Can render Astro <Markdown> with shiki', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

// There should be no HTML from Prism
expect($('.token')).to.have.lengthOf(0);

expect($('pre')).to.have.lengthOf(2);

expect($('span.line')).to.have.lengthOf(2);
expect($('span.line').get(0).children).to.have.lengthOf(1);
expect($('span.line').get(1).children).to.have.lengthOf(5);
});
});

it('Can render markdown with shiki', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
describe('Themes', () => {
describe('Integrated theme', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/themes-integrated/' });
await fixture.build();
});

it('Markdown file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').hasClass('astro-code')).to.equal(true);
expect($('pre').attr().style).to.equal('background-color: #ffffff; overflow-x: auto;');
});

// There should be no HTML from Prism
expect($('.token')).to.have.lengthOf(0);
it('<Markdown /> component', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').hasClass('astro-code')).to.equal(true);
expect($('pre').attr().style).to.equal('background-color: #ffffff');
expect($('pre')).to.have.lengthOf(1);
expect($('pre').hasClass('astro-code')).to.equal(true);
expect($('pre').attr().style).to.equal('background-color: #ffffff; overflow-x: auto;');
});
});

describe('Custom theme', async () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/themes-custom/' });
await fixture.build();
});

it('Markdown file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').hasClass('astro-code')).to.equal(true);
expect($('pre').attr().style).to.equal('background-color: #FDFDFE; overflow-x: auto;');
});

it('<Markdown /> component', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').hasClass('astro-code')).to.equal(true);
expect($('pre').attr().style).to.equal('background-color: #FDFDFE; overflow-x: auto;');
});
});
});

it('Can render Astro <Markdown> with shiki', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);
describe('Custom langs', () => {
let fixture;

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

// There should be no HTML from Prism
expect($('.token')).to.have.lengthOf(0);
it('Markdown file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(2);
const segments = $('.line').get(6).children;
expect(segments).to.have.lengthOf(3);
expect(segments[0].attribs.style).to.be.equal('color: #C9D1D9');
expect(segments[1].attribs.style).to.be.equal('color: #79C0FF');
expect(segments[2].attribs.style).to.be.equal('color: #C9D1D9');
});

expect($('span.line')).to.have.lengthOf(2);
expect($('span.line').get(0).children).to.have.lengthOf(1);
expect($('span.line').get(1).children).to.have.lengthOf(5);
it('<Markdown /> component', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

const segments = $('.line').get(6).children;
expect(segments).to.have.lengthOf(2);
expect(segments[0].attribs.style).to.be.equal('color: #79C0FF');
expect(segments[1].attribs.style).to.be.equal('color: #C9D1D9');
});
});

describe('Wrap', () => {
describe('wrap = true', () => {
const style = 'background-color: #0d1117; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;';
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/wrap-true/' });
await fixture.build();
});

it('Markdown file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').attr('style')).to.equal(style);
});

it('<Markdown /> component', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

expect($('pre').get(0).attribs.style).to.equal(style);
expect($('pre').get(1).attribs.style).to.equal(style);
});
});
});

describe('wrap = false', () => {
const style = 'background-color: #0d1117; overflow-x: auto;';
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/wrap-false/' });
await fixture.build();
});

it('Markdown file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').attr('style')).to.equal(style);
});

it('<Markdown /> component', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

expect($('pre').get(0).attribs.style).to.equal(style);
expect($('pre').get(1).attribs.style).to.equal(style);
});
});

describe('wrap = null', () => {
const style = 'background-color: #0d1117';
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/wrap-null/' });
await fixture.build();
});

it('Markdown file', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('pre')).to.have.lengthOf(1);
expect($('pre').attr('style')).to.equal(style);
});

it('<Markdown /> component', async () => {
const html = await fixture.readFile('/astro/index.html');
const $ = cheerio.load(html);

expect($('pre').get(0).attribs.style).to.equal(style);
expect($('pre').get(1).attribs.style).to.equal(style);
});
});
});
Loading

0 comments on commit 2bc9154

Please sign in to comment.