Skip to content

Commit 7ad2b54

Browse files
committed
fix(plugin-tikz): browser/node modules separation
1 parent c7e0a65 commit 7ad2b54

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

packages/plugin-tikz/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@
5050
"katex"
5151
],
5252
"dependencies": {
53+
"esm-env": "^1.0.0",
5354
"hast-util-from-dom": "^5.0.0",
55+
"md5": "^2.3.0",
5456
"node-tikzjax": "^1.0.3",
5557
"rehype-parse": "^9.0.0",
56-
"unist-util-visit": "^5.0.0",
57-
"md5": "^2.3.0"
58+
"unist-util-visit": "^5.0.0"
5859
}
5960
}

packages/plugin-tikz/src/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Carta, Plugin } from 'carta-md';
22
import type { Plugin as UnifiedPlugin } from 'unified';
33
import type * as hast from 'hast';
4+
import { BROWSER } from 'esm-env';
45

56
export interface TikzExtensionOptions {
67
/**
@@ -48,9 +49,9 @@ export const tikz = (options?: TikzExtensionOptions): Plugin => {
4849
[
4950
'carta-render',
5051
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);
5455
}
5556
}
5657
]
@@ -83,22 +84,16 @@ export const tikz = (options?: TikzExtensionOptions): Plugin => {
8384
};
8485
};
8586

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-
9187
const tikzTransformer: UnifiedPlugin<
9288
[{ carta: Carta; options: TikzExtensionOptions | undefined }],
9389
hast.Root
9490
> = ({ carta, options }) => {
9591
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');
9994
return browserModule.browserTikzTransform(tree, carta, options);
10095
} else {
101-
const nodeModule = await node();
96+
const nodeModule = await import('./node');
10297
return await nodeModule.nodeTikzTransform(tree, carta, options);
10398
}
10499
};

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)