Skip to content

Commit 89ae773

Browse files
committed
feat: expose guessEmbeddedLanguages from core
1 parent be1d614 commit 89ae773

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

packages/core/src/utils.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
HighlighterGeneric,
23
MaybeArray,
34
MaybeGetter,
45
PlainTextLanguage,
@@ -262,3 +263,36 @@ export function createPositionConverter(code: string): {
262263
posToIndex,
263264
}
264265
}
266+
267+
/**
268+
* Guess embedded languages from given code and highlighter.
269+
*
270+
* When highlighter is provided, only bundled languages will be included.
271+
*/
272+
export function guessEmbeddedLanguages(
273+
code: string,
274+
_lang: string | undefined,
275+
highlighter?: HighlighterGeneric<any, any>,
276+
): string[] {
277+
const langs = new Set<string>()
278+
// For HTML code blocks like Vue SFC
279+
for (const match of code.matchAll(/lang=["']([\w-]+)["']/g)) {
280+
langs.add(match[1])
281+
}
282+
// For markdown code blocks
283+
for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) {
284+
langs.add(match[1])
285+
}
286+
// For latex
287+
for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) {
288+
langs.add(match[1])
289+
}
290+
291+
if (!highlighter)
292+
return Array.from(langs)
293+
294+
// Only include known languages
295+
const bundle = highlighter.getBundledLanguages()
296+
return Array.from(langs)
297+
.filter(l => l && bundle[l])
298+
}

packages/shiki/src/bundle-full.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { CreateHighlighterFactory, HighlighterGeneric } from '@shikijs/type
22
import type {} from 'hast'
33
import type { BundledLanguage } from './langs-bundle-full'
44
import type { BundledTheme } from './themes'
5+
import { guessEmbeddedLanguages } from '@shikijs/core'
56
import { createdBundledHighlighter, createSingletonShorthands, warnDeprecated } from './core'
67
import { createOnigurumaEngine } from './engine-oniguruma'
7-
import { guessEmbeddedLanguages } from './guess'
88
import { bundledLanguages } from './langs-bundle-full'
99
import { bundledThemes } from './themes'
1010

packages/shiki/src/bundle-web.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { CreateHighlighterFactory, HighlighterGeneric } from '@shikijs/type
22
import type {} from 'hast'
33
import type { BundledLanguage } from './langs-bundle-web'
44
import type { BundledTheme } from './themes'
5+
import { guessEmbeddedLanguages } from '@shikijs/core'
56
import { createdBundledHighlighter, createSingletonShorthands, warnDeprecated } from './core'
67
import { createOnigurumaEngine } from './engine-oniguruma'
7-
import { guessEmbeddedLanguages } from './guess'
88
import { bundledLanguages } from './langs-bundle-web'
99
import { bundledThemes } from './themes'
1010

packages/shiki/src/guess.ts

-26
This file was deleted.

packages/shiki/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './bundle-full'
2-
export type { BuiltinLanguage, BuiltinTheme } from './types'
32

3+
export type { BuiltinLanguage, BuiltinTheme } from './types'
44
export { createJavaScriptRegexEngine, defaultJavaScriptRegexConstructor } from '@shikijs/engine-javascript'
55
export { createOnigurumaEngine, loadWasm } from '@shikijs/engine-oniguruma'

0 commit comments

Comments
 (0)