diff options
Diffstat (limited to 'packages/gitbook-plugin-highlight/src/getLanguage.js')
-rw-r--r-- | packages/gitbook-plugin-highlight/src/getLanguage.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-highlight/src/getLanguage.js b/packages/gitbook-plugin-highlight/src/getLanguage.js new file mode 100644 index 0000000..7a1bf8e --- /dev/null +++ b/packages/gitbook-plugin-highlight/src/getLanguage.js @@ -0,0 +1,34 @@ +const GitBook = require('gitbook-core'); +const { List } = GitBook.Immutable; + +const ALIASES = require('./ALIASES'); + +/** + * Return language for a code blocks from a list of class names + * + * @param {String} className + * @return {String} + */ +function getLanguage(className) { + const lang = List(className.split(' ')) + .map(function(cl) { + // Markdown + if (cl.search('lang-') === 0) { + return cl.slice('lang-'.length); + } + + // Asciidoc + if (cl.search('language-') === 0) { + return cl.slice('language-'.length); + } + + return null; + }) + .find(function(cl) { + return Boolean(cl); + }); + + return ALIASES[lang] || lang; +} + +module.exports = getLanguage; |