Transfer function module function to another file

I'm not quite sure what is going on here, but it distorted me a little. The marked node markdown parameter works as follows:

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

It all works. But I am creating a plug-in system where I want to transfer the marked function of the module to a function from another file and set the parameters in this 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 setOptions , but when I use marked subsequently in my main script, no parameters are set. If I set the parameters in the main script, it will work.

I'm a little unsure that marked is Function , and its implementation of setOptions() might be the culprit of this?

Any thoughts?

+5
source share
1 answer

You said you switched to kramed, looking at the definition of setOptions , it returns an instance of kramed . Therefore, setting parameters in another file may not mutate the global kramed instance in main.js At least the transition 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 }); } 
0
source

Source: https://habr.com/ru/post/1245522/


All Articles