async function importHighlightFileForLanguage()

in src/utils/syntax-highlight.js [58:95]


async function importHighlightFileForLanguage(language) {
  // this previously needed to be an array to support languages with
  // dependencies on other languages, but this is no longer the case with v11 of
  // highlight.js, so this could be refactored in the future to eliminate this
  // array entirely
  const files = [language];
  try {
    // use a reduce to sequentially resolve promises, in the order given.
    await files.reduce(async (previousPromise, file) => {
      // This line will wait for the last async function to finish.
      // The first iteration uses an already resolved Promise
      // so, it will immediately continue.
      await previousPromise;
      let languageFile;

      if (CustomLanguagesSet.has(file)) {
        languageFile = await import(
          /* webpackChunkName: "highlight-js-custom-[request]" */
          `../utils/custom-highlight-lang/${file}`
        );
      } else {
        languageFile = await import(
          /* webpackChunkName: "highlight-js-[request]" */
          // eslint-disable-next-line max-len
          /* webpackInclude: /\/(bash|c|s?css|cpp|diff|http|java|llvm|perl|php|python|ruby|xml|javascript|json|markdown|objectivec|shell|swift)\.js$/ */
          `highlight.js/lib/languages/${file}`
        );
      }

      hljs.registerLanguage(file, languageFile.default);
    }, Promise.resolve());

    return true;
  } catch (err) {
    console.error(`Could not load ${language} file`);
    return false;
  }
}