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

Custom renderers #7

Closed
wants to merge 3 commits into from

Conversation

Yomguithereal
Copy link
Contributor

Hello @rexxars. Sorry for this PR a bit heavy but I tried to follow up on the ideas of #5 and updated your code to be able to handle custom renderers for every types.

A rendering function has therefore the following signature:

function renderParagraph(node, props, children, opts) {

  // node is the commonmark parser node
  // props are the renderer's pre-computed props such as keys etc.
  // children are the component's children
  // opts are the renderer's options

  // the function must simply return a react element
  return <p {...props}>{children}</p>;
}

You can then pass your custom renderers likewise:

var renderer = new ReactRenderer({
  renderers: {
    Strong: function(node, props, children) {
      return React.createElement('b', props, children);
    },
    Text: function() {
      return 'Placeholder';
    },
    Paragraph: function(node, props, children) {
      return React.createElement('section', props, children);
    }
  }
});

// will render "This is some *important* text."
// into "<section>Placeholder<em>Placeholder</em>Placeholder</section>"

This has some collateral benefits: one can now very easily highlight code (without the lib imposing a precise method on him). I will probably, for instance, develop a code renderer using highlight.js on a project when have with @glenjamin.

Plus, node type solving is now O(1), compared to the O(n) switch statement and while the number of function calls has increased a bit, this should somewhat increase the renderer's performance.

What still remains to be discussed with this solution however is what should be passed to the allowNode function.

Here it is :). If you like it and find it suitable, I can also edit the PR to add some docs about custom rendering.

@Yomguithereal
Copy link
Contributor Author

I also added a little fix concerning keys and warnings again.

@unstubbable
Copy link

+1

@rexxars
Copy link
Owner

rexxars commented Jan 30, 2016

You've done some tremendous work here, and I hate to let that work go to waste. However, I discovered a few bugs last week that I had to fix, and it ended up changing major parts of the code. In doing so, I realized that the road to custom renderers was pretty short, so I decided to take a stab at it.

We came up with something very similar, API-wise. I decided to just have the renderers take a params object, and also tried to explicitly define the properties that I wanted to pass on to the individual types. I hope you find this to be an acceptable solution.

I've co-credited you in the changelog since you took the time to do all this work, and I hope the new solution will be suitable for your needs. I'm hoping to release a new version tomorrow or on Sunday, along with a new version of react-markdown.

@rexxars rexxars closed this Jan 30, 2016
@Yomguithereal
Copy link
Contributor Author

Thanks @rexxars. Happy to see we are going the same way :). I'll check the changes next week to see if I can help some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants