index.js 1.5 KB

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