zephraph / nunjucks-markdown

Markdown extension for Nunjucks. Use your own renderer!
MIT License
49 stars 12 forks source link

jstransformer #12

Closed JosefJezek closed 9 years ago

JosefJezek commented 9 years ago

Could you add jstransformer for load own markdown renderer? I need CommonMark.

https://github.com/jstransformers/inputformat-to-jstransformer https://github.com/jstransformers/inputformat-to-jstransformer/blob/master/dictionary.json https://github.com/jstransformers/jstransformer-commonmark

Thank you

zephraph commented 9 years ago

You can already use your own markdown tool. I specifically designed it that way.

Call the register method with the first argument being the environment returned from nunjucks and the second being a method that renders markdown.

var nunjucks = require('nunjucks'),
    markdown = require('nunjucks-markdown');

var env = nunjucks.configure('views');

// The second argument can be any function that renders markdown
markdown.register(env, yourRenderMethod);
JosefJezek commented 9 years ago

CommonMark have different API than marked. :-( I get error.

https://github.com/jgm/commonmark.js

zephraph commented 9 years ago

Can you give me an example of what you're trying to accomplish?

JosefJezek commented 9 years ago

require('nunjucks-markdown').register(env, require('commonmark'));

Message:
    (unknown path)
  TypeError: object is not a function
zephraph commented 9 years ago

You are trying to pass in the commonmark module as a whole, but that doesn't work. The reason I could pass in marked in my example is because when you do require('marked') it actually returns a method that takes in a string of markdown and returns the rendered html.

It looks to me like you'll have to do something like this:

var nunjucks = require('nunjucks'),
    markdown = require('nunjucks-markdown'),
    cm = require('commonmark');

var env = nunjucks.configure('views');
var reader = new cm.Parser();
var writer = new cm.HtmlRenderer();

function render(markdown) {
  var parsed = reader.parse(markdown);
  return writer.render(parsed);
}

markdown.register(env, render);

This may or may not work, but it's just something I slapped together by looking at the commonmark examples.

JosefJezek commented 9 years ago

Thank you :+1:

I will use it in this repo https://github.com/StartPolymer/polymer-starter-kit-plus

zephraph commented 9 years ago

No problem. Let me know if you have any more issues.