From eefc49b737831b0e3d8454dea0b6c0601b70d31f Mon Sep 17 00:00:00 2001 From: Konstantin Gorodinskiy Date: Thu, 28 Mar 2019 22:17:20 +0100 Subject: [PATCH] Fix language mime type lookup (#712) * add editorconfig to keep files consistent automatically * lookup language mime type in languages dictionary * use language maps instead of language list * Revert "add editorconfig to keep files consistent automatically" This reverts commit 3cfdd4a5444791e763ffae59fde199406677f82f. * move location of toLowerCase() --- components/Carbon.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/components/Carbon.js b/components/Carbon.js index b4900e1..acdb112 100644 --- a/components/Carbon.js +++ b/components/Carbon.js @@ -11,6 +11,7 @@ import { COLORS, LANGUAGE_MODE_HASH, LANGUAGE_NAME_HASH, + LANGUAGE_MIME_HASH, THEMES_HASH, DEFAULT_SETTINGS } from '../lib/constants' @@ -19,6 +20,14 @@ const Watermark = dynamic(() => import('./svg/Watermark'), { loading: () => null }) +function searchLanguage(l) { + const config = LANGUAGE_NAME_HASH[l] || LANGUAGE_MODE_HASH[l] || LANGUAGE_MIME_HASH[l] + + if (config) { + return config.mime || config.mode + } +} + class Carbon extends React.PureComponent { static defaultProps = { onAspectRatioChange: () => {}, @@ -43,14 +52,19 @@ class Carbon extends React.PureComponent { if (language === 'auto') { // try to set the language const detectedLanguage = hljs.highlightAuto(newCode).language - const languageMode = - LANGUAGE_MODE_HASH[detectedLanguage] || LANGUAGE_NAME_HASH[detectedLanguage] + const languageMode = searchLanguage(detectedLanguage) if (languageMode) { - return languageMode.mime || languageMode.mode + return languageMode } } + const languageMode = searchLanguage(language) + + if (languageMode) { + return languageMode + } + return language }, ms('300ms'), @@ -69,7 +83,10 @@ class Carbon extends React.PureComponent { render() { const config = { ...DEFAULT_SETTINGS, ...this.props.config } - const languageMode = this.handleLanguageChange(this.props.children, config.language) + const languageMode = this.handleLanguageChange( + this.props.children, + config.language && config.language.toLowerCase() + ) const options = { lineNumbers: config.lineNumbers,