Skip to content

Commit 37697a2

Browse files
authoredNov 27, 2023
Remove shiki lang path property support (#9196)
1 parent 153a5ab commit 37697a2

File tree

7 files changed

+22
-427
lines changed

7 files changed

+22
-427
lines changed
 

‎.changeset/plenty-candles-help.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Removes support for Shiki custom language's `path` property. The language JSON file should be imported and passed to the option instead.
6+
7+
```diff
8+
// astro.config.js
9+
+ import customLang from './custom.tmLanguage.json'
10+
11+
export default defineConfig({
12+
markdown: {
13+
shikiConfig: {
14+
langs: [
15+
- { path: './custom.tmLanguage.json' },
16+
+ customLang,
17+
],
18+
},
19+
},
20+
})
21+
```

‎packages/astro/components/Code.astro

-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
---
2-
import path from 'node:path';
3-
import fs from 'node:fs';
4-
import { fileURLToPath } from 'node:url';
52
import type {
63
BuiltinLanguage,
74
BuiltinTheme,
@@ -64,23 +61,6 @@ const {
6461
6562
// shiki -> shikiji compat
6663
if (typeof lang === 'object') {
67-
// shikiji does not support `path`
68-
// https://github.com/shikijs/shiki/blob/facb6ff37996129626f8066a5dccb4608e45f649/packages/shiki/src/loader.ts#L98
69-
const langPath = (lang as any).path;
70-
if (langPath) {
71-
// shiki resolves path from within its package directory :shrug:
72-
const astroRoot = fileURLToPath(new URL('../', import.meta.url));
73-
const normalizedPath = path.isAbsolute(langPath) ? langPath : path.resolve(astroRoot, langPath);
74-
try {
75-
const content = fs.readFileSync(normalizedPath, 'utf-8');
76-
const parsed = JSON.parse(content);
77-
Object.assign(lang, parsed);
78-
} catch (e) {
79-
throw new Error(`Unable to find language file at ${normalizedPath}`, {
80-
cause: e,
81-
});
82-
}
83-
}
8464
// `id` renamed to `name` (always override)
8565
if ((lang as any).id) {
8666
lang.name = (lang as any).id;

‎packages/astro/src/core/config/schema.ts

+1-21
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import { markdownConfigDefaults } from '@astrojs/markdown-remark';
88
import { bundledThemes, type BuiltinTheme } from 'shikiji';
99
import type { AstroUserConfig, ViteUserConfig } from '../../@types/astro.js';
1010

11-
import fs from 'node:fs';
1211
import type { OutgoingHttpHeaders } from 'node:http';
1312
import path from 'node:path';
14-
import { fileURLToPath, pathToFileURL } from 'node:url';
13+
import { pathToFileURL } from 'node:url';
1514
import { z } from 'zod';
1615
import { appendForwardSlash, prependForwardSlash, removeTrailingForwardSlash } from '../path.js';
1716

@@ -238,25 +237,6 @@ export const AstroConfigSchema = z.object({
238237
for (const lang of langs) {
239238
// shiki -> shikiji compat
240239
if (typeof lang === 'object') {
241-
// shikiji does not support `path`
242-
// https://github.com/shikijs/shiki/blob/facb6ff37996129626f8066a5dccb4608e45f649/packages/shiki/src/loader.ts#L98
243-
const langPath = (lang as any).path;
244-
if (langPath) {
245-
// shiki resolves path from within its package directory :shrug:
246-
const astroRoot = fileURLToPath(new URL('../../../', import.meta.url));
247-
const normalizedPath = path.isAbsolute(langPath)
248-
? langPath
249-
: path.resolve(astroRoot, langPath);
250-
try {
251-
const content = fs.readFileSync(normalizedPath, 'utf-8');
252-
const parsed = JSON.parse(content);
253-
Object.assign(lang, parsed);
254-
} catch (e) {
255-
throw new Error(`Unable to find language file at ${normalizedPath}`, {
256-
cause: e,
257-
});
258-
}
259-
}
260240
// `id` renamed to `name` (always override)
261241
if ((lang as any).id) {
262242
lang.name = (lang as any).id;

‎packages/astro/test/astro-markdown-shiki.test.js

-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ describe('Astro Markdown Shiki', () => {
9595

9696
const unknownLang = $('.astro-code').get(1);
9797
expect(unknownLang.attribs.style).to.contain('background-color:#24292e;color:#e1e4e8;');
98-
99-
const caddyLang = $('.astro-code').last();
100-
const caddySegments = caddyLang.find('.line');
101-
expect(caddySegments.get(1).children[0].attribs.style).to.contain('color:#B392F0');
10298
});
10399
});
104100

‎packages/astro/test/fixtures/astro-markdown-shiki/langs/astro.config.mjs

-8
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ export default {
1313
grammar: riGrammar,
1414
aliases: ['ri'],
1515
},
16-
{
17-
id: 'caddy',
18-
scopeName: 'source.Caddyfile',
19-
// shiki compat: resolves from astro package directory.
20-
// careful as astro is linked, this relative path is based on astro/packages/astro.
21-
// it's weird but we're testing to prevent regressions.
22-
path: './test/fixtures/astro-markdown-shiki/langs/src/caddyfile.tmLanguage.json',
23-
},
2416
],
2517
},
2618
},

‎packages/astro/test/fixtures/astro-markdown-shiki/langs/src/caddyfile.tmLanguage.json

-365
This file was deleted.

‎packages/astro/test/fixtures/astro-markdown-shiki/langs/src/pages/index.md

-9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,3 @@ fin
2424
```unknown
2525
This language does not exist
2626
```
27-
28-
```caddy
29-
example.com {
30-
root * /var/www/wordpress
31-
encode gzip
32-
php_fastcgi unix//run/php/php-version-fpm.sock
33-
file_server
34-
}
35-
```

0 commit comments

Comments
 (0)
Please sign in to comment.