Skip to content

Commit 5a6d384

Browse files
JuanM04brc-dd
andauthored
feat(build): add support for custom languages (#1837)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent e2d4edf commit 5a6d384

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

docs/config/app-config.md

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ interface MarkdownOptions extends MarkdownIt.Options {
154154
// Enable line numbers in code block.
155155
lineNumbers?: boolean
156156

157+
// Add support for your own languages.
158+
// https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki
159+
languages?: Shiki.ILanguageRegistration
160+
157161
// markdown-it-anchor plugin options.
158162
// See: https://github.com/valeriangalliat/markdown-it-anchor#usage
159163
anchor?: anchorPlugin.AnchorOptions

src/node/markdown/markdown.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import MarkdownIt from 'markdown-it'
1515
import anchorPlugin from 'markdown-it-anchor'
1616
import attrsPlugin from 'markdown-it-attrs'
1717
import emojiPlugin from 'markdown-it-emoji'
18-
import type { IThemeRegistration } from 'shiki'
18+
import type { ILanguageRegistration, IThemeRegistration } from 'shiki'
1919
import type { Logger } from 'vite'
2020
import { containerPlugin } from './plugins/containers'
2121
import { highlight } from './plugins/highlight'
@@ -47,6 +47,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
4747
headers?: HeadersPluginOptions
4848
sfc?: SfcPluginOptions
4949
theme?: ThemeOptions
50+
languages?: ILanguageRegistration[]
5051
toc?: TocPluginOptions
5152
externalLinks?: Record<string, string>
5253
}
@@ -64,7 +65,12 @@ export const createMarkdownRenderer = async (
6465
linkify: true,
6566
highlight:
6667
options.highlight ||
67-
(await highlight(options.theme, options.defaultHighlightLang, logger)),
68+
(await highlight(
69+
options.theme,
70+
options.languages,
71+
options.defaultHighlightLang,
72+
logger
73+
)),
6874
...options
6975
}) as MarkdownRenderer
7076

src/node/markdown/plugins/highlight.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { customAlphabet } from 'nanoid'
22
import c from 'picocolors'
3-
import type { HtmlRendererOptions, IThemeRegistration } from 'shiki'
3+
import {
4+
BUNDLED_LANGUAGES,
5+
type HtmlRendererOptions,
6+
type ILanguageRegistration,
7+
type IThemeRegistration
8+
} from 'shiki'
49
import {
510
addClass,
611
createDiffProcessor,
@@ -58,6 +63,7 @@ const errorLevelProcessor = defineProcessor({
5863

5964
export async function highlight(
6065
theme: ThemeOptions = 'material-theme-palenight',
66+
languages: ILanguageRegistration[] = [],
6167
defaultLang: string = '',
6268
logger: Pick<Logger, 'warn'> = console
6369
): Promise<(str: string, lang: string, attrs: string) => string> {
@@ -74,6 +80,7 @@ export async function highlight(
7480

7581
const highlighter = await getHighlighter({
7682
themes: hasSingleTheme ? [theme] : [theme.dark, theme.light],
83+
langs: [...BUNDLED_LANGUAGES, ...languages],
7784
processors
7885
})
7986

0 commit comments

Comments
 (0)