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

Allow walker callback #15

Merged
merged 5 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>');
});
});