Skip to content

Commit

Permalink
Merge pull request #15 from firsara/master
Browse files Browse the repository at this point in the history
Allow walker callback
  • Loading branch information
rexxars committed Feb 16, 2016
2 parents f8dd03b + 4366bb1 commit f9f730e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ If you either set `escapeHtml` or `skipHtml` to `true`, this component does not
* `tag` - *string* The resolved tag name for this node
* `props` - *object* Properties for this tag
* `children` - *array* Array of unparsed children
* `walker` - *function* Defines a callback function to walk through all nodes and manipulating them where needed before rendering.

The possible types of elements that can be allowed/disallowed are:

Expand Down
2 changes: 1 addition & 1 deletion demo/dist/js/demo.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/react-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var ReactMarkdown = React.createClass({
softBreak: propTypes.string,
allowNode: propTypes.func,
allowedTypes: propTypes.array,
disallowedTypes: propTypes.array
disallowedTypes: propTypes.array,
walker: propTypes.func
},

getDefaultProps: function() {
Expand All @@ -34,6 +35,15 @@ var ReactMarkdown = React.createClass({
var renderer = new ReactRenderer(this.props);
var ast = parser.parse(this.props.source || '');

if (this.props.walker) {
var walker = ast.walker();
var event;

while ((event = walker.next())) {
this.props.walker.call(this, event);
}
}

if (this.props.className) {
containerProps.className = this.props.className;
}
Expand Down
15 changes: 15 additions & 0 deletions test/react-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,19 @@ describe('ReactMarkdown', function() {

expect(ReactDom.findDOMNode(rendered).innerHTML).to.equal('');
});

it('allows a walker callback', function() {
var walker = function(event) {
if (event.entering && event.node.type === 'Strong') {
event.node.firstChild.literal = 'walker';
}
};

var rendered = TestUtils.renderIntoDocument(
React.createElement(ReactMarkdown, { source: testMarkdown, walker: walker })
);

var main = ReactDom.findDOMNode(rendered).innerHTML;
expect(main).to.contain('walker</strong>');
});
});

0 comments on commit f9f730e

Please sign in to comment.