Skip to content

Commit

Permalink
Remove shiki lang path property support (#9196)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 27, 2023
1 parent 153a5ab commit 37697a2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 427 deletions.
21 changes: 21 additions & 0 deletions .changeset/plenty-candles-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'astro': major
---

Removes support for Shiki custom language's `path` property. The language JSON file should be imported and passed to the option instead.

```diff
// astro.config.js
+ import customLang from './custom.tmLanguage.json'

export default defineConfig({
markdown: {
shikiConfig: {
langs: [
- { path: './custom.tmLanguage.json' },
+ customLang,
],
},
},
})
```
20 changes: 0 additions & 20 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
---
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import type {
BuiltinLanguage,
BuiltinTheme,
Expand Down Expand Up @@ -64,23 +61,6 @@ const {
// shiki -> shikiji compat
if (typeof lang === 'object') {
// 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;
Expand Down
22 changes: 1 addition & 21 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { markdownConfigDefaults } from '@astrojs/markdown-remark';
import { bundledThemes, type BuiltinTheme } from 'shikiji';
import type { AstroUserConfig, ViteUserConfig } from '../../@types/astro.js';

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

Expand Down Expand Up @@ -238,25 +237,6 @@ export const AstroConfigSchema = z.object({
for (const lang of langs) {
// shiki -> shikiji compat
if (typeof lang === 'object') {
// 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;
Expand Down
4 changes: 0 additions & 4 deletions packages/astro/test/astro-markdown-shiki.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ describe('Astro Markdown Shiki', () => {

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,14 +13,6 @@ 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 37697a2

Please sign in to comment.