|
| 1 | +/** |
| 2 | + * Prism is used to syntax highlight code blocks in markdown files. |
| 3 | + * |
| 4 | + * Original source: |
| 5 | + * @link https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-theme-classic/src/theme/prism-include-languages.ts |
| 6 | + * |
| 7 | + * Reason for overriding: |
| 8 | + * - Add Vue language support since it's not included in Prism |
| 9 | + */ |
| 10 | + |
| 11 | +import siteConfig from '@generated/docusaurus.config'; |
| 12 | +import type * as PrismNamespace from 'prismjs'; |
| 13 | +import type { Optional } from 'utility-types'; |
| 14 | + |
| 15 | +export default function prismIncludeLanguages(PrismObject: typeof PrismNamespace): void { |
| 16 | + const { |
| 17 | + themeConfig: { prism }, |
| 18 | + } = siteConfig; |
| 19 | + const { additionalLanguages } = prism as { additionalLanguages: string[] }; |
| 20 | + |
| 21 | + // Prism components work on the Prism instance on the window, while prism- |
| 22 | + // react-renderer uses its own Prism instance. We temporarily mount the |
| 23 | + // instance onto window, import components to enhance it, then remove it to |
| 24 | + // avoid polluting global namespace. |
| 25 | + // You can mutate PrismObject: registering plugins, deleting languages... As |
| 26 | + // long as you don't re-assign it |
| 27 | + |
| 28 | + const PrismBefore = globalThis.Prism; |
| 29 | + globalThis.Prism = PrismObject; |
| 30 | + |
| 31 | + additionalLanguages.forEach((lang) => { |
| 32 | + if (lang === 'php') { |
| 33 | + // eslint-disable-next-line global-require |
| 34 | + require('prismjs/components/prism-markup-templating.js'); |
| 35 | + } |
| 36 | + // eslint-disable-next-line global-require, import/no-dynamic-require |
| 37 | + require(`prismjs/components/prism-${lang}`); |
| 38 | + }); |
| 39 | + |
| 40 | + // CUSTOM CODE |
| 41 | + require('../theme/prism-languages/prism-vue'); |
| 42 | + // CUSTOM CODE END |
| 43 | + |
| 44 | + // Clean up and eventually restore former globalThis.Prism object (if any) |
| 45 | + delete (globalThis as Optional<typeof globalThis, 'Prism'>).Prism; |
| 46 | + if (typeof PrismBefore !== 'undefined') { |
| 47 | + globalThis.Prism = PrismObject; |
| 48 | + } |
| 49 | +} |
0 commit comments