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()
main
Konstantin Gorodinskiy 6 years ago committed by Michael Fix
parent 1378b619e4
commit eefc49b737

@ -11,6 +11,7 @@ import {
COLORS, COLORS,
LANGUAGE_MODE_HASH, LANGUAGE_MODE_HASH,
LANGUAGE_NAME_HASH, LANGUAGE_NAME_HASH,
LANGUAGE_MIME_HASH,
THEMES_HASH, THEMES_HASH,
DEFAULT_SETTINGS DEFAULT_SETTINGS
} from '../lib/constants' } from '../lib/constants'
@ -19,6 +20,14 @@ const Watermark = dynamic(() => import('./svg/Watermark'), {
loading: () => null 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 { class Carbon extends React.PureComponent {
static defaultProps = { static defaultProps = {
onAspectRatioChange: () => {}, onAspectRatioChange: () => {},
@ -43,14 +52,19 @@ class Carbon extends React.PureComponent {
if (language === 'auto') { if (language === 'auto') {
// try to set the language // try to set the language
const detectedLanguage = hljs.highlightAuto(newCode).language const detectedLanguage = hljs.highlightAuto(newCode).language
const languageMode = const languageMode = searchLanguage(detectedLanguage)
LANGUAGE_MODE_HASH[detectedLanguage] || LANGUAGE_NAME_HASH[detectedLanguage]
if (languageMode) { if (languageMode) {
return languageMode.mime || languageMode.mode return languageMode
} }
} }
const languageMode = searchLanguage(language)
if (languageMode) {
return languageMode
}
return language return language
}, },
ms('300ms'), ms('300ms'),
@ -69,7 +83,10 @@ class Carbon extends React.PureComponent {
render() { render() {
const config = { ...DEFAULT_SETTINGS, ...this.props.config } 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 = { const options = {
lineNumbers: config.lineNumbers, lineNumbers: config.lineNumbers,

Loading…
Cancel
Save