UNPKG

1.54 kBJavaScriptView Raw
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) {
21 if (languages === undefined) {
22 languages = Object.keys(components.languages).filter(l => l != 'meta');
23 } else if (!Array.isArray(languages)) {
24 languages = [languages];
25 }
26
27 // the user might have loaded languages via some other way or used `prism.js` which already includes some
28 // we don't need to validate the ids because `getLoader` will ignore invalid ones
29 const loaded = [...loadedLanguages, ...Object.keys(Prism.languages)];
30
31 getLoader(components, languages, loaded).load(lang => {
32 if (!(lang in components.languages)) {
33 if (!loadLanguages.silent) {
34 console.warn('Language does not exist: ' + lang);
35 }
36 return;
37 }
38
39 const pathToLanguage = './prism-' + lang;
40
41 // remove from require cache and from Prism
42 delete require.cache[require.resolve(pathToLanguage)];
43 delete Prism.languages[lang];
44
45 require(pathToLanguage);
46
47 loadedLanguages.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;