From c53cf784c1d4e064adc7172349d6fcf8693b6530 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 18 Dec 2023 19:21:41 -0800 Subject: [PATCH] chore: add cjs and mjs aliases for code block syntax highlighting (#487) --- src/theme/prism-include-languages.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/theme/prism-include-languages.js diff --git a/src/theme/prism-include-languages.js b/src/theme/prism-include-languages.js new file mode 100644 index 000000000..9a3512fc0 --- /dev/null +++ b/src/theme/prism-include-languages.js @@ -0,0 +1,24 @@ +import siteConfig from '@generated/docusaurus.config'; + +export default function prismIncludeLanguages(PrismObject) { + const { + themeConfig: { prism }, + } = siteConfig; + const { additionalLanguages } = prism; + // Prism components work on the Prism instance on the window, while prism- + // react-renderer uses its own Prism instance. We temporarily mount the + // instance onto window, import components to enhance it, then remove it to + // avoid polluting global namespace. + // You can mutate PrismObject: registering plugins, deleting languages... As + // long as you don't re-assign it + globalThis.Prism = PrismObject; + additionalLanguages.forEach((lang) => { + require(`prismjs/components/prism-${lang}`); + }); + + // Add JavaScript aliases + PrismObject.languages.mjs = PrismObject.languages.cjs = + PrismObject.languages.javascript; + + delete globalThis.Prism; +}