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

fix: does not import every langs and themes #93

Closed
Closed
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
19 changes: 5 additions & 14 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ export default defineNuxtConfig({
mdc: {
highlight: {
theme: {
default: 'vitesse-light',
dark: 'material-theme-palenight',
// default: 'vitesse-light',
// dark: 'material-theme-palenight',
},
preload: [
'sql'
]
},
remarkPlugins: {
'remark-mdc': {
options: {
experimental: {
autoUnwrap: true
}
}
}
// preload: [
// 'sql'
// ]
}
},
devtools: {
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/shiki/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { useRuntimeConfig } from '#imports'

export default lazyEventHandler(async () => {
const { highlight } = useRuntimeConfig().mdc


console.log(highlight)

try {
// try loading `.wasm` directly, for cloudflare workers
// @ts-expect-error
Expand All @@ -25,6 +27,7 @@ export default lazyEventHandler(async () => {
const theme = JSON.parse(themeString as string)
const highlights = highlightsString ? JSON.parse(highlightsString as string) as number[] : undefined

console.log('event-handler.lang', lang)
return await shiki.getHighlightedAST(code as string, lang as BuiltinLanguage, theme as BuiltinTheme, { highlights })
})
})
58 changes: 31 additions & 27 deletions src/runtime/shiki/highlighter.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import { getHighlighter, type ThemeInput, type Highlighter, type BuiltinLanguage, type BuiltinTheme } from 'shikiji'
import { type ThemeInput, type Highlighter, type BuiltinLanguage, type BuiltinTheme} from 'shikiji'

Check warning on line 1 in src/runtime/shiki/highlighter.ts

View workflow job for this annotation

GitHub Actions / test

'ThemeInput' is defined but never used

Check warning on line 1 in src/runtime/shiki/highlighter.ts

View workflow job for this annotation

GitHub Actions / test

'Highlighter' is defined but never used
import { HighlighterCore, getHighlighterCore } from 'shikiji/core'
import type { HighlightResult, HighlighterOptions, Theme } from './types'
import type { Element } from '../types/hast'

export const useShikiHighlighter = createSingleton((opts?: any) => {
// Grab highlighter config from publicRuntimeConfig
const { theme, preload, wrapperStyle } = opts || {}

Check warning on line 8 in src/runtime/shiki/highlighter.ts

View workflow job for this annotation

GitHub Actions / test

'theme' is assigned a value but never used

let promise: Promise<Highlighter> | undefined
let promise: Promise<HighlighterCore> | undefined
const getShikiHighlighter = () => {
if (!promise) {
// Initialize highlighter with defaults
promise = getHighlighter({
promise = getHighlighterCore({
themes: [
((theme as any)?.default || theme || 'dark-plus') as BuiltinTheme,
// ((theme as any)?.default || theme || 'dark-plus') as BuiltinTheme,
],
langs: [
...(preload || []),
'diff',
'json',
'js',
'ts',
'css',
'shell',
'html',
'md',
'yaml',
'vue',
'mdc'
// 'diff',
// 'json',
// 'js',
// 'ts',
// 'css',
// 'shell',
// 'html',
// 'md',
// 'yaml',
// 'vue',
// 'mdc'
] as any[]
}).then((highlighter) => {
// Load all themes on-demand
const themes = Object.values(typeof theme === 'string' ? { default: theme } : (theme || {})) as ThemeInput[]

if (themes.length) {
return Promise
.all(themes.map(theme => highlighter.loadTheme(theme)))
.then(() => highlighter)
}
return highlighter
})
// .then((highlighter) => {
// // Load all themes on-demand
// const themes = Object.values(typeof theme === 'string' ? { default: theme } : (theme || {})) as ThemeInput[]

// if (themes.length) {
// return Promise
// .all(themes.map(theme => highlighter.loadTheme(theme)))
// .then(() => highlighter)
// }
// return highlighter
// })
}
return promise
}
Expand All @@ -52,11 +54,12 @@
const themeNames = Object.values(themesObject) as BuiltinTheme[]

if (themeNames.length) {
await Promise.all(themeNames.map(theme => highlighter.loadTheme(theme)))
await Promise.all(themeNames.map(theme => highlighter.loadTheme(import(`shikiji/themes/${theme}.mjs`))))
}

console.log('lang', lang)
if (lang && !highlighter.getLoadedLanguages().includes(lang)) {
await highlighter.loadLanguage(lang)
// await highlighter.loadLanguage(bundledLanguages[lang]())
}

const root = highlighter.codeToHast(code.trimEnd(), {
Expand Down Expand Up @@ -135,6 +138,7 @@
style: styles.join(''),
}
} catch (error: any) {
console.error(error)
console.warn('[@nuxtjs/mdc] Failed to highlight code block', error.message)
return {
tree: [{ type: 'text', value: code }],
Expand Down
Loading