Skip to content

Commit a12f7e7

Browse files
yvbopengantfu
andauthored
fix(shiki): astro partial syntax highlighting error (#1035)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 2bda450 commit a12f7e7

2 files changed

Lines changed: 77 additions & 33 deletions

File tree

packages/langs/scripts/langs.ts

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,53 @@ export const LANGS_LAZY_EMBEDDED_ALL = {
1818
* Single-file-component-like languages that have embedded langs
1919
* For these langs, we exclude the standalone embedded langs from the main bundle
2020
*/
21-
export const LANGS_LAZY_EMBEDDED_PARTIAL = [
22-
'vue',
23-
'vue-html',
24-
'svelte',
25-
'pug',
26-
'haml',
27-
'astro',
28-
]
29-
30-
/**
31-
* Languages to be excluded from SFC langs
32-
*/
33-
export const STANDALONE_LANGS_EMBEDDED = [
34-
'pug',
35-
'stylus',
36-
'sass',
37-
'scss',
38-
'coffee',
39-
'jsonc',
40-
'json5',
41-
'yaml',
42-
'toml',
43-
'scss',
44-
'graphql',
45-
'markdown',
46-
'less',
47-
'jsx',
48-
'tsx',
49-
'ruby',
50-
]
21+
export const LANGS_LAZY_EMBEDDED_PARTIAL = {
22+
'vue': [
23+
'markdown',
24+
'pug',
25+
'stylus',
26+
'sass',
27+
'scss',
28+
'less',
29+
'jsx',
30+
'tsx',
31+
'coffee',
32+
'jsonc',
33+
'json5',
34+
'yaml',
35+
'toml',
36+
'graphql',
37+
],
38+
'vue-html': [],
39+
'svelte': [
40+
'coffee',
41+
'stylus',
42+
'sass',
43+
'scss',
44+
'less',
45+
'pug',
46+
'markdown',
47+
],
48+
'pug': [
49+
'sass',
50+
'scss',
51+
'stylus',
52+
'coffee',
53+
],
54+
'haml': [
55+
'ruby',
56+
'sass',
57+
'coffee',
58+
'markdown',
59+
],
60+
// Since Astro is a extension of MDX, we don't exclude `jsx` here.
61+
'astro': [
62+
'sass',
63+
'scss',
64+
'stylus',
65+
'less',
66+
],
67+
} as Record<string, string[]>
5168

5269
export async function loadLangs() {
5370
const allLangFiles = await fg('*.json', {
@@ -83,9 +100,10 @@ export async function loadLangs() {
83100
json.embeddedLangsLazy = (json.embeddedLangs || []).filter(i => !includes.includes(i)) || []
84101
json.embeddedLangs = includes
85102
}
86-
else if (LANGS_LAZY_EMBEDDED_PARTIAL.includes(lang.name)) {
87-
json.embeddedLangsLazy = (json.embeddedLangs || []).filter(i => STANDALONE_LANGS_EMBEDDED.includes(i)) || []
88-
json.embeddedLangs = (json.embeddedLangs || []).filter(i => !STANDALONE_LANGS_EMBEDDED.includes(i)) || []
103+
else if (LANGS_LAZY_EMBEDDED_PARTIAL[lang.name]) {
104+
const includes = LANGS_LAZY_EMBEDDED_PARTIAL[lang.name]
105+
json.embeddedLangsLazy = includes
106+
json.embeddedLangs = (json.embeddedLangs || []).filter(i => !includes.includes(i)) || []
89107
}
90108

91109
resolvedLangs.push(json)

packages/shiki/test/astro.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createHighlighter } from 'shiki'
2+
import { describe, expect, it } from 'vitest'
3+
4+
describe('should', async () => {
5+
it('astro syntax highlighting', async () => {
6+
const highlighter = await createHighlighter({
7+
langs: ['astro'],
8+
themes: ['vitesse-dark'],
9+
})
10+
const code = `---
11+
const title = "Astro";
12+
---
13+
14+
<p>{title}</p>
15+
`
16+
expect(highlighter.codeToHtml(code, { lang: 'astro', theme: 'vitesse-dark' })).toMatchInlineSnapshot(`
17+
"<pre class="shiki vitesse-dark" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#758575DD">---</span></span>
18+
<span class="line"><span style="color:#CB7676">const </span><span style="color:#BD976A">title</span><span style="color:#666666"> =</span><span style="color:#C98A7D77"> "</span><span style="color:#C98A7D">Astro</span><span style="color:#C98A7D77">"</span><span style="color:#666666">;</span></span>
19+
<span class="line"><span style="color:#758575DD">---</span></span>
20+
<span class="line"></span>
21+
<span class="line"><span style="color:#666666">&#x3C;</span><span style="color:#4D9375">p</span><span style="color:#666666">>{</span><span style="color:#BD976A">title</span><span style="color:#666666">}&#x3C;/</span><span style="color:#4D9375">p</span><span style="color:#666666">></span></span>
22+
<span class="line"></span></code></pre>"
23+
`)
24+
highlighter.dispose()
25+
})
26+
})

0 commit comments

Comments
 (0)