valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
291 stars 72 forks source link

Exception 'plugin.apply is not a function' #124

Closed cvionnet closed 1 year ago

cvionnet commented 1 year ago

When loading the plugin, the following exception raises:

'plugin.apply is not a function'

'TypeError: plugin.apply is not a function\n
at push.../../node_modules/markdown-it/lib/index.js.MarkdownIt.use (https://localhost:44300/app/vendor-es2015.js:345922:10)\n > at new MarkdownToHtmlPipe (https://localhost:44300/app/main-es2015.js:17140:17)\n
at createClass (https://localhost:44300/app/vendor-es2015.js:75922:20)\n
at createPipeInstance (https://localhost:44300/app/vendor-es2015.js:75731:12)\n
at createViewNodes (https://localhost:44300/app/vendor-es2015.js:87016:34)\n
at callViewAction (https://localhost:44300/app/vendor-es2015.js:87474:13)\n
at execComponentViewsAction (https://localhost:44300/app/vendor-es2015.js:87379:13)\n
at createViewNodes (https://localhost:44300/app/vendor-es2015.js:87053:5)\n
at createRootView (https://localhost:44300/app/vendor-es2015.js:86896:5)\n
at callWithDebugContext (https://localhost:44300/app/vendor-es2015.js:88426:27)'

Code used:

import * as markdownIt from 'markdown-it';
// (...) other plugins
import * as markdownItAttrs from 'markdown-it-attrs';
import * as markdownItAnchors from 'markdown-it-anchor';

md: any;
constructor() {
    this.md = markdownIt({ html: true });
    // (...) other plugins
    this.md.use(markdownItAttrs);
    this.md.use(markdownItAnchors, {});
}

The exception comes from "markdown-it/index.js":

MarkdownIt.prototype.use = function (plugin /*, params, ... */) {
  var args = [ this ].concat(Array.prototype.slice.call(arguments, 1));
  plugin.apply(plugin, args);     // 👉EXCEPTION
  return this;
};

Versions tried:

valeriangalliat commented 1 year ago

It seems to be related to the transpilation of import statements with CJS compatibility

import * at least for markdown-it-anchor should return an object, with default being the plugin, and other props for other exports

Depending on your bundling and transpiling toolchain, it may work with import markdownItanchors from 'markdown-it-anchor'

cvionnet commented 1 year ago

Thanks, it works by using import markdownItanchors from 'markdown-it-anchor'