mathpix-markdown-it icon indicating copy to clipboard operation
mathpix-markdown-it copied to clipboard

Vite + mathpix-markdown-it

Open rom-ger opened this issue 1 year ago • 17 comments

Hello. I created vite project npm create vite@latest (react + typescript) (node 18.15.0) then i installed "mathpix-markdown-it": "1.2.13" then i used it in my project

import { MathpixMarkdownModel as MM, TMarkdownItOptions } from 'mathpix-markdown-it';

const mathpixOptions: TMarkdownItOptions = {};

function App() {
  const generatedHtml = {
    __html:
      MM.markdownToHTML(
        '',
        mathpixOptions,
    )
};

  return (
    <>
    </>
  )
}

and finally i saw error in console TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them Снимок экрана 2023-10-03 в 09 49 05

how can i fix it?

rom-ger avatar Oct 03 '23 06:10 rom-ger

@OlgaRedozubova I`m waiting feedback

rom-ger avatar Oct 12 '23 07:10 rom-ger

This occurs in the package we use to render the math. Related issue https://github.com/mathjax/MathJax/issues/2748

This is due to the fact that

Vite cannot handle and does not support code that only runs on non-strict mode (sloppy mode). This is because Vite uses ESM and it is always strict mode inside ESM.

https://github.com/vitejs/vite/blob/fffe16ee9a9773910ef6cc8e9f0b75b3b9db7b58/docs/guide/troubleshooting.md#syntax-error--type-error-happens

You need to disable this for node-modules

OlgaRedozubova avatar Oct 12 '23 08:10 OlgaRedozubova

@OlgaRedozubova maybe you should add a patch for module "mathjax-full" inside your module?

rom-ger avatar Oct 17 '23 13:10 rom-ger

@OlgaRedozubova ?

rom-ger avatar Oct 30 '23 12:10 rom-ger

@OlgaRedozubova maybe you should add a patch for module "mathjax-full" inside your module?

it's very important for us

rom-ger avatar Nov 11 '23 11:11 rom-ger

Hi, i've got a same problem too, and fixed it using

  optimizeDeps: {
    // Disable warnings for specific dependencies
    exclude: ['mathpix-markdown-it'],
  },

But now i got an error: "Uncaught SyntaxError: ambiguous indirect export: MathpixMarkdown". I need help please.

raviadi12 avatar Nov 19 '23 20:11 raviadi12

I think the best solution here would be to disable strict mode. To avoid patching mathjax-full

@OlgaRedozubova maybe you should add a patch for module "mathjax-full" inside your module?

MathJax has no plans to make these changes yet https://github.com/mathjax/MathJax/issues/3122#issuecomment-1787232804

I added such a patch in this branch dev/olga/278-Vite-mathpix-markdown-it https://github.com/Mathpix/mathpix-markdown-it/pull/291

This works inside mathpix-markdown-it, but it won't work when installing the mathpix-markdown-it dependency

In order for the patch for mathjax-full to work, you need to create such a patch in your project.

@rom-ger @raviadi12

Creating a patch for mathjax-full

https://github.com/ds300/patch-package

  1. In node_modules to notice the files (I attached Zip archives of these files): node_modules/mathjax-full/js/input/asciimath/mathjax2/input/AsciiMath.js - AsciiMath.js.zip node_modules/mathjax-full/js/input/asciimath/mathjax2/legacy/MathJax.js - MathJax.js.zip

  2. Install patch-package and postinstall-postinstall: yarn add patch-package postinstall-postinstall or npm install patch-package postinstall-postinstall

  3. Add postinstall script:

 "scripts": {
  "postinstall": "patch-package"
 }

  1. Run patch-package to create a .patch file npx patch-package mathjax-full or yarn patch-package mathjax-full

This will produce following changes patches/mathjax-full+3.0.1.patch:

Screen Shot 2023-11-22 at 12 10 38
  1. Apply your fix via npm package manager: npm install or yarn install
Screen Shot 2023-11-22 at 11 29 05

I tested this in a test application on Angular17 which use Vite. It works to me

@rom-ger @raviadi12 I hope this solution helps you.

OlgaRedozubova avatar Nov 22 '23 10:11 OlgaRedozubova

@OlgaRedozubova it is really work! thanks!

rom-ger avatar Dec 05 '23 13:12 rom-ger

我认为最好的解决方案是禁用严格模式。 为了避免打补丁mathjax-full

@OlgaRedozubova也许您应该在模块内添加模块“mathjax-full”的补丁?

MathJax 尚无计划进行这些更改mathjax/MathJax#3122(评论)

我在这个分支中添加了这样的补丁dev/olga/278-Vite-mathpix-markdown-it #291

这在内部有效,但在安装依赖项 mathpix-markdown-it时不起作用mathpix-markdown-it

为了使补丁mathjax-full发挥作用,您需要在项目中创建这样的补丁。

@rom-ger @raviadi12

创建补丁mathjax-full

https://github.com/ds300/patch-package

  1. 注意node_modules这些文件(我附上了这些文件的 Zip 存档): node_modules/mathjax-full/js/input/asciimath/mathjax2/input/AsciiMath.js- AsciiMath.js.zip node_modules/mathjax-full/js/input/asciimath/mathjax2/legacy/MathJax.js - MathJax.js.zip
  2. 安装patch-packagepostinstall-postinstallyarn add patch-package postinstall-postinstallnpm install patch-package postinstall-postinstall
  3. 添加安装后脚本:
 "scripts": {
  "postinstall": "patch-package"
 }
  1. 运行 patch-package 以创建 .patch 文件 npx patch-package mathjax-fullyarn patch-package mathjax-full

这将产生以下变化patches/mathjax-full+3.0.1.patch

屏幕截图 2023-11-22 12 10 38 5. 通过 npm 包管理器应用修复: `npm install` 或 `yarn install` 屏幕截图 2023-11-22 11 29 05 我在 Angular17 上使用 Vite 的测试应用程序中对此进行了测试。这对我有用

@rom-ger @raviadi12我希望这个解决方案对您有所帮助。

npx patch-package mathjax-full patch-package 8.0.0 patch-package: you have both yarn.lock and package-lock.json Defaulting to using npm You can override this setting by passing --use-yarn or deleting package-lock.json if you don't need it

• Creating temporary folder • Installing [email protected] with npm • Diffing your files with clean files ⁉️ Not creating patch file for package 'mathjax-full' ⁉️ There don't appear to be any changes.

I need help when encountering this issue during patch installation.

jiang1654 avatar Jan 11 '24 02:01 jiang1654

Same issue as above. I'm using Vite + React 18 MathJax 1.3.1

elitebyte avatar Feb 20 '24 20:02 elitebyte

@OlgaRedozubova The patch helped with local development using, however, when I build it with vite build and deploy it, I get an error Uncaught ReferenceError: MathJax is not defined. You can reproduce it by running vite build and vite preview afterwards.

Any ideas what causes the problem?

tomas-sipko avatar Mar 04 '24 13:03 tomas-sipko

I'm also experiencing this. Did you figure out how to solve this? @tomas-sipko

@OlgaRedozubova The patch helped with local development using, however, when I build it with vite build and deploy it, I get an error Uncaught ReferenceError: MathJax is not defined. You can reproduce it by running vite build and vite preview afterwards.

Any ideas what causes the problem?

bumberboy avatar Mar 23 '24 08:03 bumberboy

@OlgaRedozubova The patch helped with local development using, however, when I build it with vite build and deploy it, I get an error Uncaught ReferenceError: MathJax is not defined. You can reproduce it by running vite build and vite preview afterwards.

Any ideas what causes the problem?

...
// Add the following four lines of code
if (typeof global.MathJax === 'undefined') {
  global.MathJax = {};
}
MathJax = Object.assign(global.MathJax || {}, require("../xxx/MathJax.js").MathJax); // Modify the file reference path here

MathJax.xxx // The code where the error occurred
...

I also encountered this problem. I referred to the patch solution above and modified the code where the error was reported. It worked successfully.

xvweimin avatar Apr 16 '24 03:04 xvweimin

@OlgaRedozubova The patch helped with local development using, however, when I build it with vite build and deploy it, I get an error Uncaught ReferenceError: MathJax is not defined. You can reproduce it by running vite build and vite preview afterwards. Any ideas what causes the problem?

...
// Add the following four lines of code
if (typeof global.MathJax === 'undefined') {
  global.MathJax = {};
}
MathJax = Object.assign(global.MathJax || {}, require("../xxx/MathJax.js").MathJax); // Modify the file reference path here

MathJax.xxx // The code where the error occurred
...

I also encountered this problem. I referred to the patch solution above and modified the code where the error was reported. It worked successfully.

@xvweimin I just got the same problem, but I did not understand your solution. Could you please expand the explanation?

enricobolzonello avatar Apr 19 '24 10:04 enricobolzonello

@enricobolzonello Have you followed the patch solution at https://github.com/Mathpix/mathpix-markdown-it/issues/278#issuecomment-1822482690 ? You can pay attention to the changes in the patch when replacing the file content, and you should be able to understand my solution.

xvweimin avatar Apr 25 '24 07:04 xvweimin

I know this is a bit old, but here's how I fixed the above Uncaught ReferenceError: MathJax is not defined error in both development and with vite build:

First, make sure to define MathJax and global on the window object by putting it somewhere in a file that gets loaded first in your app. I'm using Vue, so I placed the following in my main.ts file before any JS imports:

if (!window.MathJax) {
	window.MathJax = {};
}

window.global = window;

global is used throughout the MathJax library, so we have to define that on the window object as well.

Then, in the .patch file linked above, add the following on Line 16:

@@ -1,7 +1,1 @@ exports.MathJax = MathJax;
-var MathJax = {debug:true};
+MathJax = {debug:true};
-
-var window = {
-  MathJax: MathJax
-};
-var navigator = {};
-var document = null;

Also, MathJax is now on version 3.2.2, but this patch is specifically for version 3.0.1, so make sure you install the correct version with your package manager.

jdk2pq avatar Sep 12 '24 17:09 jdk2pq