4

I'm not exactly sure what's going on here, but it has been bugging me for a while. The marked node markdown parser works like this:

var marked = require('marked');
marked.setOptions({ mathjax : false });

That all works. But I'm building a plugin system where I want to pass the marked module function into a function from another file and set the options in that function:

// main.js
var marked = require('marked');
var plugin = require('./plugin');
plugin(marked);
marked("# my markdown\n $$5 + 5$$", function(err, result) {
  // this result still parses mathjax. Setting the option in the main
  // file will disable mathjax.
  console.log(result);
});

// plugin.js
module.exports = function(marked) {
  marked.setOptions({ mathjax: false });
}

The marked function is passed correctly to my plugin function, and the setOptions function is called, but when I use marked afterwards in my main script, the options are not set. If I set the options in the main script, it's working.

I'm a bit unsure of whether marked being a Function and its implementation of setOptions() might be the culprit of this?

Any thoughts?

Ronze
  • 1,464
  • 2
  • 15
  • 33
  • How do you understand that it is not working? – Roman Dibikhin Mar 22 '16 at 01:35
  • 2
    What happens if you `console.log(marked)` in `plugin.js`? – Calvin Belden Mar 22 '16 at 01:37
  • This should work just fine, something else must be going on? – adeneo Mar 22 '16 at 02:53
  • Describe "not working." Do you get an error? Is the `marked` function argument some unexpected value? Does your computer monitor turn off when you run the script? – mscdex Mar 22 '16 at 05:22
  • I updated the question to specify what's not working: Marked is getting passed to my plugin, I can correctly call the setOptions function, but when I use kramed afterwards in my main script, the options are not set. Maybe this relates to how kramed is written? When I console.log kramed, it's typeof Function. – Ronze Mar 22 '16 at 11:35
  • "*I'm a bit unsure of how `marked` can be Function and still have a `setOptions()` function*" - every function is an object in js, and can have instance properties. Nothing unusual about that. – Bergi Mar 22 '16 at 11:56
  • I know the basics of prototypical inheritance, but my question was whether or not this is causing my problem. – Ronze Mar 22 '16 at 12:37
  • No, it's not causing your problem. To be honest, I cannot see what would cause your problem, I cannot reproduce it. Can you maybe post the output of `console.dir(marked)` before and after calling `setOptions` (in both working and non-working versions)? – Bergi Mar 22 '16 at 12:55
  • Here's a gist that logs the output from inside the plugin (inside.txt) and outside in the main function (outside.txt). I'm using the `kramed` fork of `marked`, but it's the same concept. – Ronze Mar 22 '16 at 15:25
  • I realized that the problem is that `kramed` and `marked` save their default config NOT on the prototype object, but on the pure function itself. Because of Node's` require` caching behavior, it was sharing the same function reference across all my tests, spilling over the settings. This seems like a really bad implementation. Switching to `markdown-it` immediately fixed it because it works on the prototype instance. – Ronze Mar 22 '16 at 18:26

1 Answers1

0

You said you switched to kramed, an looking at the definition of setOptions it returns an instance of kramed. So setting the options in another file may not mutate the global instance of kramed in main.js. At the very least changing to the following works:

// main.js
var kramed = require('kramed');
var plugin = require('./plugin');
kramed = plugin(kramed);
kramed("# my markdown\n  $$5 + 5$$", function(err, result) { console.log(result); });

//plugin.js
module.exports = function(kramed) {
  return kramed.setOptions({ mathjax: false });
}