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

Add Feature - Support Promise #798

Closed
wants to merge 5 commits into from
Closed

Conversation

crazyguitar
Copy link

Hi,

I made this project support Promise . Consider the following scenario:

var marked = require('marked')
    , fs = require('fs');
var renderer = new marked.Renderer();

renderer.heading = function (text, level) {
  var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
  // need read file, but we cannot using fs.readFile(filename, enc, cb).
  // We cannot use  any async functions.
},
console.log(marked('# heading+', { renderer: renderer }));

then we can solve this by using Promise object:

var marked = require('./index')
  , fs = require('fs')
  , util = require('util');
var renderer = new marked.Renderer();

var path = "layout.html";

renderer.heading = function (text, level) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, 'utf8', (err, data) => {
      try {
        resolve(util.format(data, text));
      } catch (e) {
        reject(e);
      }
    });
  });
},

marked('# Heading2\n```js\nconsole.log("hi")```\n', { promise: true, renderer: renderer })
.then(function(html) {
  console.log(html);
}).catch(e => {
  console.log(e);
});

and I can get the result like:

<h1>Heading2</h1>
<pre><code class="lang-js">console.log(&quot;hi&quot;)
</code></pre>

The Promise object solve the problem which I cannot call async functions in render function. Hope this feature can make the project better.

thanks,
chang-ning

@nchase
Copy link

nchase commented Sep 30, 2016

This would be really useful to me.

This was referenced May 15, 2017
@joshbruce joshbruce added this to Queue in PRs Dec 1, 2017
@Feder1co5oave
Copy link
Contributor

This may work in node, but not in the browser. IMO, it is out of scope.

@joshbruce
Copy link
Member

Closing due to merge conflicts and missing tests (not quite sure how you'd test a promise). Having said this, it might be interesting if Marked could be multi-threaded (more concurrent); not sure if it already is or not.

@joshbruce joshbruce closed this Dec 27, 2017
@joshbruce joshbruce moved this from Queue to Close in PRs Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
PRs
Close
Development

Successfully merging this pull request may close these issues.

None yet

4 participants