From 1ae05969396ef1651af56c66696ed77f8c4e7e85 Mon Sep 17 00:00:00 2001 From: Clark Cui <46164858+clark-cui@users.noreply.github.com> Date: Sat, 14 Jan 2023 22:22:08 +0800 Subject: [PATCH] feat(build): don't hard fail on unknown languages in fences (#1750) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- src/node/markdown/plugins/highlight.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/node/markdown/plugins/highlight.ts b/src/node/markdown/plugins/highlight.ts index 291473ec..72bb3f22 100644 --- a/src/node/markdown/plugins/highlight.ts +++ b/src/node/markdown/plugins/highlight.ts @@ -1,3 +1,5 @@ +import { customAlphabet } from 'nanoid' +import c from 'picocolors' import type { HtmlRendererOptions, IThemeRegistration } from 'shiki' import { addClass, @@ -10,7 +12,6 @@ import { type Processor } from 'shiki-processor' import type { ThemeOptions } from '../markdown' -import { customAlphabet } from 'nanoid' const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10) @@ -85,6 +86,18 @@ export async function highlight( lang = lang.replace(lineNoRE, '').replace(vueRE, '').toLowerCase() || defaultLang + const langLoaded = highlighter.getLoadedLanguages().includes(lang as any) + if (!langLoaded) { + console.warn( + c.yellow( + `The language '${lang}' is not loaded, falling back to '${ + defaultLang || 'txt' + }' for syntax highlighting.` + ) + ) + lang = defaultLang + } + const lineOptions = attrsToLines(attrs) const cleanup = (str: string) => str