Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to use plugin with Svelte (TypeError: plugin.apply is not a function) #14

Open
vcoppe opened this issue Mar 13, 2024 · 3 comments
Open
Labels
bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities

Comments

@vcoppe
Copy link

vcoppe commented Mar 13, 2024

Hi, I am trying to import and use this plugin in a Svelte project as shown below

import markdownit from 'markdown-it';
import mk from '@vscode/markdown-it-katex';

const md = markdownit().use(mk);

but cannot avoid the following error

TypeError: plugin.apply is not a function
    at MarkdownIt.use (file:///.../node_modules/markdown-it/lib/index.mjs:485:10)

Here is a minimal reproducible example: https://svelte.dev/repl/fea74f54d05c4632ac61e2e7d46df355?version=4.2.12.

Note that this error does not occur with other versions of this plugin (iktakahiro/markdown-it-katex or traPtitech/markdown-it-katex). Do you know what could cause this?

@smearumi
Copy link

smearumi commented Apr 3, 2024

same issue here. :(

@mensah-j
Copy link

Examining the imported module with console.log(mk) yields

{ default: [Function: default_1]  }

which hints that this is due to the interaction between CommonJS and ES modules. From the Node.js documentation:

When importing CommonJS modules, the module.exports object is provided as the default export.

How do I fix this issue?

Simply replace mk with mk.default to access the plugin function. See here for an example.

I suspect this issue is due the choice of module output format by the Typescript compiler. If your compiler emits ES modules , then the above should apply. However, if the module format is CommonJS, then adding .default should not be necessary. For example, when adding this package as a plugin to Vite, you will need to use the .default syntax since the actual runtime script vite.js is compiled as an ES module.

@xcaptain
Copy link

Same issue here, use(mk.default) doesn't work for me, I suspect if this package doesn't SSR?

@mjbvz mjbvz added bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities labels Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities
Projects
None yet
Development

No branches or pull requests

5 participants