summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-highlight/src/getLanguage.js
blob: 6a1234fc8d29661a60420b41891127e00290f370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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((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((cl) => {
            return Boolean(cl);
        });

    return ALIASES[lang] || lang;
}

module.exports = getLanguage;