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

Fixes usage of Code component in Vercel #6198

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/great-mails-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes usage of Code component in Vercel
20 changes: 15 additions & 5 deletions packages/astro/components/Shiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ function stringify(opts) {

/**
* @param {import('shiki').HighlighterOptions} opts
* @returns {Promise<import('shiki').Highlighter>}
* @returns {Promise<import('shiki').HighlighterOptions>}
*/
async function resolveHighlighter(opts) {
export async function resolveHighlighterOptions(opts) {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
const resolvedThemes = [];
if (Object.keys(opts.theme).length) {
resolvedThemes.push(opts.theme);
} else if (opts.theme && opts.theme in themes) {
if (opts.theme && opts.theme in themes) {
resolvedThemes.push(await themes[opts.theme]());
} else if (Object.keys(opts.theme).length) {
resolvedThemes.push(opts.theme);
}

let resolvedLanguages;
Expand All @@ -47,6 +47,16 @@ async function resolveHighlighter(opts) {
// Do not pass through the theme as that will attempt to load it, even if it's included in themes
delete highlighterOptions['theme'];

return highlighterOptions;
}

/**
* @param {import('shiki').HighlighterOptions} opts
* @returns {Promise<import('shiki').Highlighter>}
*/
async function resolveHighlighter(opts) {
const highlighterOptions = await resolveHighlighterOptions(opts);

// Start the async getHighlighter call and cache the Promise
const highlighter = getShikiHighlighter(highlighterOptions).then((hl) => {
hl.setColorReplacements({
Expand Down
40 changes: 40 additions & 0 deletions packages/astro/test/units/shiki/shiki.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'chai';
import { createContainer } from '../../../dist/core/dev/index.js';
import { createViteLoader } from '../../../dist/core/module-loader/index.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

describe('<Code />', () => {
describe('Shiki - getHighlighterOptions', () => {
let container;
let mod;
before(async () => {
container = await createContainer({ root, disableTelemetry: true });
const loader = createViteLoader(container.viteServer);
mod = await loader.import('astro/components/Shiki.js');
});

after(async () => {
await container.close();
})

it('uses the bundles themes for built-in themes', async () => {
const { resolveHighlighterOptions } = mod;
const opts = await resolveHighlighterOptions({ theme: 'css-variables' });
const themes = opts.themes;

expect(themes).to.have.a.lengthOf(1);
expect(themes[0]).to.be.an('object');
});

it('uses the string theme name for custom themes', async () => {
const { resolveHighlighterOptions } = mod;
const opts = await resolveHighlighterOptions({ theme: 'some-custom-theme' });
const themes = opts.themes;

expect(themes).to.have.a.lengthOf(1);
expect(themes[0]).to.be.an('string');
expect(themes[0]).to.equal('some-custom-theme');
})
});
});