Skip to content

Commit

Permalink
Add shiki lang path compat (#8996)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 6, 2023
1 parent 910eb00 commit 3988bbc
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-toes-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Adds compatibility for shiki languages with the `path` property
24 changes: 22 additions & 2 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import type {
BuiltinLanguage,
BuiltinTheme,
Expand Down Expand Up @@ -56,8 +59,25 @@ const {
// shiki -> shikiji compat
if (typeof lang === 'object') {
// `id` renamed to `name
if ((lang as any).id && !lang.name) {
// shikiji does not support `path`
// https://github.com/shikijs/shiki/blob/facb6ff37996129626f8066a5dccb4608e45f649/packages/shiki/src/loader.ts#L98
const langPath = (lang as any).path;
if (langPath) {
// shiki resolves path from within its package directory :shrug:
const astroRoot = fileURLToPath(new URL('../', import.meta.url));
const normalizedPath = path.isAbsolute(langPath) ? langPath : path.resolve(astroRoot, langPath);
try {
const content = fs.readFileSync(normalizedPath, 'utf-8');
const parsed = JSON.parse(content);
Object.assign(lang, parsed);
} catch (e) {
throw new Error(`Unable to find language file at ${normalizedPath}`, {
cause: e,
});
}
}
// `id` renamed to `name` (always override)
if ((lang as any).id) {
lang.name = (lang as any).id;
}
// `grammar` flattened to lang itself
Expand Down
26 changes: 23 additions & 3 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type { AstroUserConfig, ViteUserConfig } from '../../@types/astro.js';

import type { OutgoingHttpHeaders } from 'node:http';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import fs from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { z } from 'zod';
import { appendForwardSlash, prependForwardSlash, removeTrailingForwardSlash } from '../path.js';

Expand Down Expand Up @@ -247,8 +248,27 @@ export const AstroConfigSchema = z.object({
for (const lang of langs) {
// shiki -> shikiji compat
if (typeof lang === 'object') {
// `id` renamed to `name
if ((lang as any).id && !lang.name) {
// shikiji does not support `path`
// https://github.com/shikijs/shiki/blob/facb6ff37996129626f8066a5dccb4608e45f649/packages/shiki/src/loader.ts#L98
const langPath = (lang as any).path;
if (langPath) {
// shiki resolves path from within its package directory :shrug:
const astroRoot = fileURLToPath(new URL('../../../', import.meta.url));
const normalizedPath = path.isAbsolute(langPath)
? langPath
: path.resolve(astroRoot, langPath);
try {
const content = fs.readFileSync(normalizedPath, 'utf-8');
const parsed = JSON.parse(content);
Object.assign(lang, parsed);
} catch (e) {
throw new Error(`Unable to find language file at ${normalizedPath}`, {
cause: e,
});
}
}
// `id` renamed to `name` (always override)
if ((lang as any).id) {
lang.name = (lang as any).id;
}
// `grammar` flattened to lang itself
Expand Down
8 changes: 6 additions & 2 deletions packages/astro/test/astro-markdown-shiki.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ describe('Astro Markdown Shiki', () => {
expect(segments[0].attribs.style).to.be.equal('color:#79B8FF');
expect(segments[1].attribs.style).to.be.equal('color:#E1E4E8');

const unknownLang = $('.astro-code').last();
expect(unknownLang.attr('style')).to.contain('background-color:#24292e;color:#e1e4e8;');
const unknownLang = $('.astro-code').get(1);
expect(unknownLang.attribs.style).to.contain('background-color:#24292e;color:#e1e4e8;');

const caddyLang = $('.astro-code').last();
const caddySegments = caddyLang.find('.line');
expect(caddySegments.get(1).children[0].attribs.style).to.contain('color:#B392F0');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export default {
grammar: riGrammar,
aliases: ['ri'],
},
{
id: 'caddy',
scopeName: 'source.Caddyfile',
// shiki compat: resolves from astro package directory.
// careful as astro is linked, this relative path is based on astro/packages/astro.
// it's weird but we're testing to prevent regressions.
path: './test/fixtures/astro-markdown-shiki/langs/src/caddyfile.tmLanguage.json',
},
],
},
},
Expand Down
Loading

0 comments on commit 3988bbc

Please sign in to comment.