|
1 | 1 | import type { Carta, Plugin } from 'carta-md'; |
2 | 2 | import type { Plugin as UnifiedPlugin } from 'unified'; |
3 | 3 | import type * as hast from 'hast'; |
| 4 | +import { BROWSER } from 'esm-env'; |
4 | 5 |
|
5 | 6 | export interface TikzExtensionOptions { |
6 | 7 | /** |
@@ -48,9 +49,9 @@ export const tikz = (options?: TikzExtensionOptions): Plugin => { |
48 | 49 | [ |
49 | 50 | 'carta-render', |
50 | 51 | async (e) => { |
51 | | - const isBrowser = typeof window !== 'undefined'; |
52 | | - if (isBrowser) { |
53 | | - (await browser()).processTikzScripts(e, options); |
| 52 | + if (BROWSER) { |
| 53 | + const module = await import('./browser'); |
| 54 | + module.processTikzScripts(e, options); |
54 | 55 | } |
55 | 56 | } |
56 | 57 | ] |
@@ -83,22 +84,16 @@ export const tikz = (options?: TikzExtensionOptions): Plugin => { |
83 | 84 | }; |
84 | 85 | }; |
85 | 86 |
|
86 | | -let browserModule: typeof import('./browser') | undefined; |
87 | | -let nodeModule: typeof import('./node') | undefined; |
88 | | -const browser = async () => (browserModule ??= await import('./browser')); |
89 | | -const node = async () => (nodeModule ??= await import('./node')); |
90 | | - |
91 | 87 | const tikzTransformer: UnifiedPlugin< |
92 | 88 | [{ carta: Carta; options: TikzExtensionOptions | undefined }], |
93 | 89 | hast.Root |
94 | 90 | > = ({ carta, options }) => { |
95 | 91 | return async function (tree) { |
96 | | - const isBrowser = typeof window !== 'undefined'; |
97 | | - if (isBrowser) { |
98 | | - const browserModule = await browser(); |
| 92 | + if (BROWSER) { |
| 93 | + const browserModule = await import('./browser'); |
99 | 94 | return browserModule.browserTikzTransform(tree, carta, options); |
100 | 95 | } else { |
101 | | - const nodeModule = await node(); |
| 96 | + const nodeModule = await import('./node'); |
102 | 97 | return await nodeModule.nodeTikzTransform(tree, carta, options); |
103 | 98 | } |
104 | 99 | }; |
|
0 commit comments