summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-highlight/src/getLanguage.js
blob: 47b68cf6e2085596aa519496b972e1f3ba9d96cd (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
const GitBook = require('gitbook-core');
const { List } = GitBook.Immutable;

/**
 * Return language for a code blocks from a list of class names
 *
 * @param {Array<String>}
 * @return {String}
 */
function getLanguage(classNames) {
    return List(classNames)
        .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);
        });
}

module.exports = getLanguage;