lavkach3
1const components = require('../components.js');2const getLoader = require('../dependencies');3
4
5/**
6* The set of all languages which have been loaded using the below function.
7*
8* @type {Set<string>}
9*/
10const loadedLanguages = new Set();11
12/**
13* Loads the given languages and adds them to the current Prism instance.
14*
15* If no languages are provided, __all__ Prism languages will be loaded.
16*
17* @param {string|string[]} [languages]
18* @returns {void}
19*/
20function loadLanguages(languages) {21if (languages === undefined) {22languages = Object.keys(components.languages).filter(l => l != 'meta');23} else if (!Array.isArray(languages)) {24languages = [languages];25}26
27// the user might have loaded languages via some other way or used `prism.js` which already includes some28// we don't need to validate the ids because `getLoader` will ignore invalid ones29const loaded = [...loadedLanguages, ...Object.keys(Prism.languages)];30
31getLoader(components, languages, loaded).load(lang => {32if (!(lang in components.languages)) {33if (!loadLanguages.silent) {34console.warn('Language does not exist: ' + lang);35}36return;37}38
39const pathToLanguage = './prism-' + lang;40
41// remove from require cache and from Prism42delete require.cache[require.resolve(pathToLanguage)];43delete Prism.languages[lang];44
45require(pathToLanguage);46
47loadedLanguages.add(lang);48});49}
50
51/**
52* Set this to `true` to prevent all warning messages `loadLanguages` logs.
53*/
54loadLanguages.silent = false;55
56module.exports = loadLanguages;57