pull/4326/head
Divyansh Singh 11 months ago
parent 14c5db3f65
commit e22adc0031

@ -1,10 +0,0 @@
// @ts-check
import { bundledLanguages } from 'shiki'
import { runAsWorker } from 'synckit'
runAsWorker(async (lang) => {
const fn = bundledLanguages[lang]
if (!fn) return null
return (await fn()).default
})

@ -36,9 +36,6 @@
},
"./vue-demi": {
"default": "./lib/vue-demi.mjs"
},
"./shiki-helpers": {
"default": "./lib/shiki-helpers.mjs"
}
},
"main": "dist/node/index.js",
@ -116,7 +113,6 @@
"mark.js": "8.11.1",
"minisearch": "^7.1.0",
"shiki": "^1.22.2",
"synckit": "^0.9.2",
"vite": "^5.4.10",
"vue": "^3.5.12"
},
@ -189,6 +185,7 @@
"sirv": "^3.0.0",
"sitemap": "^8.0.0",
"supports-color": "^9.4.0",
"synckit": "^0.9.2",
"tinyglobby": "^0.2.10",
"typescript": "^5.6.3",
"vitest": "^2.1.4",

@ -61,9 +61,6 @@ importers:
shiki:
specifier: ^1.22.2
version: 1.22.2
synckit:
specifier: ^0.9.2
version: 0.9.2
vite:
specifier: ^5.4.10
version: 5.4.10(@types/node@22.8.2)
@ -275,6 +272,9 @@ importers:
supports-color:
specifier: ^9.4.0
version: 9.4.0
synckit:
specifier: ^0.9.2
version: 0.9.2
tinyglobby:
specifier: ^0.2.10
version: 0.2.10

@ -1,7 +1,7 @@
import { promises as fs } from 'fs'
import { builtinModules, createRequire } from 'module'
import { resolve } from 'path'
import { fileURLToPath } from 'url'
import * as fs from 'node:fs/promises'
import { builtinModules, createRequire } from 'node:module'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { type RollupOptions, defineConfig } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
@ -10,6 +10,7 @@ import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import alias from '@rollup/plugin-alias'
import dts from 'rollup-plugin-dts'
import { globSync } from 'tinyglobby'
const ROOT = fileURLToPath(import.meta.url)
const r = (p: string) => resolve(ROOT, '..', p)
@ -43,11 +44,15 @@ const plugins = [
]
const esmBuild: RollupOptions = {
input: [r('src/node/index.ts'), r('src/node/cli.ts')],
input: [
r('src/node/index.ts'),
r('src/node/cli.ts'),
...globSync(r('src/node/worker_*.ts'))
],
output: {
format: 'esm',
entryFileNames: `[name].js`,
chunkFileNames: 'serve-[hash].js',
chunkFileNames: 'chunk-[hash].js',
dir: r('dist/node'),
sourcemap: DEV
},

@ -1,7 +1,3 @@
import { customAlphabet } from 'nanoid'
import c from 'picocolors'
import type { ShikiTransformer } from 'shiki'
import { createHighlighter, isSpecialLang } from 'shiki'
import {
transformerCompactLineOptions,
transformerNotationDiff,
@ -10,14 +6,21 @@ import {
transformerNotationHighlight,
type TransformerCompactLineOption
} from '@shikijs/transformers'
import { customAlphabet } from 'nanoid'
import { createRequire } from 'node:module'
import c from 'picocolors'
import type { ShikiTransformer } from 'shiki'
import { createHighlighter, isSpecialLang } from 'shiki'
import { createSyncFn } from 'synckit'
import type { Logger } from 'vite'
import type { ShikiResolveLang } from 'worker_shikiResolveLang'
import type { MarkdownOptions, ThemeOptions } from '../markdown'
import { createSyncFn } from 'synckit'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const resolveLang = createSyncFn(require.resolve('vitepress/shiki-helpers'))
const resolveLangSync = createSyncFn<ShikiResolveLang>(
require.resolve('vitepress/dist/node/worker_shikiResolveLang.js')
)
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10)
/**
@ -67,7 +70,10 @@ export async function highlight(
typeof theme === 'object' && 'light' in theme && 'dark' in theme
? [theme.light, theme.dark]
: [theme],
langs: options.languages || [],
langs: [
...(options.languages || []),
...Object.values(options.languageAlias || {})
],
langAlias: options.languageAlias
})
@ -113,8 +119,8 @@ export async function highlight(
if (lang) {
const langLoaded = highlighter.getLoadedLanguages().includes(lang)
if (!langLoaded && !isSpecialLang(lang)) {
const resolvedLang = resolveLang(lang)
if (!resolveLang) {
const resolvedLang = resolveLangSync(lang)
if (!resolvedLang) {
logger.warn(
c.yellow(
`\nThe language '${lang}' is not loaded, falling back to '${
@ -124,7 +130,7 @@ export async function highlight(
)
lang = defaultLang
} else {
highlighter.loadLanguageSync(resolvedLang as any)
highlighter.loadLanguageSync(resolvedLang)
}
}
}

@ -0,0 +1,14 @@
import { bundledLanguages, type DynamicImportLanguageRegistration } from 'shiki'
import { runAsWorker } from 'synckit'
async function resolveLang(lang: string) {
return (
(bundledLanguages as Record<string, DynamicImportLanguageRegistration>)
[lang]?.()
.then((m) => m.default) || []
)
}
runAsWorker(resolveLang)
export type ShikiResolveLang = typeof resolveLang
Loading…
Cancel
Save