From 58ae668f6486a0a46bd22fc21547ab7351c7e68d Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Thu, 9 Aug 2018 08:44:01 +0300 Subject: [PATCH] fix: crash on any backticks code block without lang specified --- src/services/__tests__/prism.test.ts | 4 ++++ src/utils/highlight.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/services/__tests__/prism.test.ts b/src/services/__tests__/prism.test.ts index 8f982f1d..75396c7b 100644 --- a/src/services/__tests__/prism.test.ts +++ b/src/services/__tests__/prism.test.ts @@ -16,4 +16,8 @@ describe('prism.js helpers', () => { test('highlight raw text should just return text', () => { expect(highlight('Hello world', 'clike')).toBe('Hello world'); }); + + test('highlight should not throw with lang undefined', () => { + expect(highlight('Hello world', undefined)).toBe('Hello world'); + }); }); diff --git a/src/utils/highlight.ts b/src/utils/highlight.ts index a642e25a..feae2a3b 100644 --- a/src/utils/highlight.ts +++ b/src/utils/highlight.ts @@ -42,7 +42,7 @@ export function mapLang(lang: string): string { * @param lang highlight language * @return highlighted souce code as **html string** */ -export function highlight(source: string, lang: string): string { +export function highlight(source: string, lang: string = DEFAULT_LANG): string { lang = lang.toLowerCase(); let grammar = Prism.languages[lang]; if (!grammar) {