feat(build): add support for custom languages (#1837)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/1995/head
Juan Martín Seery 2 years ago committed by GitHub
parent e2d4edf45b
commit 5a6d384952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -154,6 +154,10 @@ interface MarkdownOptions extends MarkdownIt.Options {
// Enable line numbers in code block. // Enable line numbers in code block.
lineNumbers?: boolean lineNumbers?: boolean
// Add support for your own languages.
// https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki
languages?: Shiki.ILanguageRegistration
// markdown-it-anchor plugin options. // markdown-it-anchor plugin options.
// See: https://github.com/valeriangalliat/markdown-it-anchor#usage // See: https://github.com/valeriangalliat/markdown-it-anchor#usage
anchor?: anchorPlugin.AnchorOptions anchor?: anchorPlugin.AnchorOptions

@ -15,7 +15,7 @@ import MarkdownIt from 'markdown-it'
import anchorPlugin from 'markdown-it-anchor' import anchorPlugin from 'markdown-it-anchor'
import attrsPlugin from 'markdown-it-attrs' import attrsPlugin from 'markdown-it-attrs'
import emojiPlugin from 'markdown-it-emoji' import emojiPlugin from 'markdown-it-emoji'
import type { IThemeRegistration } from 'shiki' import type { ILanguageRegistration, IThemeRegistration } from 'shiki'
import type { Logger } from 'vite' import type { Logger } from 'vite'
import { containerPlugin } from './plugins/containers' import { containerPlugin } from './plugins/containers'
import { highlight } from './plugins/highlight' import { highlight } from './plugins/highlight'
@ -47,6 +47,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
headers?: HeadersPluginOptions headers?: HeadersPluginOptions
sfc?: SfcPluginOptions sfc?: SfcPluginOptions
theme?: ThemeOptions theme?: ThemeOptions
languages?: ILanguageRegistration[]
toc?: TocPluginOptions toc?: TocPluginOptions
externalLinks?: Record<string, string> externalLinks?: Record<string, string>
} }
@ -64,7 +65,12 @@ export const createMarkdownRenderer = async (
linkify: true, linkify: true,
highlight: highlight:
options.highlight || options.highlight ||
(await highlight(options.theme, options.defaultHighlightLang, logger)), (await highlight(
options.theme,
options.languages,
options.defaultHighlightLang,
logger
)),
...options ...options
}) as MarkdownRenderer }) as MarkdownRenderer

@ -1,6 +1,11 @@
import { customAlphabet } from 'nanoid' import { customAlphabet } from 'nanoid'
import c from 'picocolors' import c from 'picocolors'
import type { HtmlRendererOptions, IThemeRegistration } from 'shiki' import {
BUNDLED_LANGUAGES,
type HtmlRendererOptions,
type ILanguageRegistration,
type IThemeRegistration
} from 'shiki'
import { import {
addClass, addClass,
createDiffProcessor, createDiffProcessor,
@ -58,6 +63,7 @@ const errorLevelProcessor = defineProcessor({
export async function highlight( export async function highlight(
theme: ThemeOptions = 'material-theme-palenight', theme: ThemeOptions = 'material-theme-palenight',
languages: ILanguageRegistration[] = [],
defaultLang: string = '', defaultLang: string = '',
logger: Pick<Logger, 'warn'> = console logger: Pick<Logger, 'warn'> = console
): Promise<(str: string, lang: string, attrs: string) => string> { ): Promise<(str: string, lang: string, attrs: string) => string> {
@ -74,6 +80,7 @@ export async function highlight(
const highlighter = await getHighlighter({ const highlighter = await getHighlighter({
themes: hasSingleTheme ? [theme] : [theme.dark, theme.light], themes: hasSingleTheme ? [theme] : [theme.dark, theme.light],
langs: [...BUNDLED_LANGUAGES, ...languages],
processors processors
}) })

Loading…
Cancel
Save