Skip to content

Commit

Permalink
Document parser hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Dec 8, 2015
1 parent f4fc3cc commit 0d1247d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,43 @@ const expansion = {
};
```

### DOM Parsing hooks

A developer can override the default parsing behavior for leaf DOM nodes in
pasted HTML.

For example, when an `img` tag is pasted it may be appropriate to
fetch that image, upload it to an authoritative source, and create a specific
kind of image card with the new URL in its payload.

A demonstration of this:

```js
function imageToCardParser(node, builder, {addSection, addMarkerable, skipNode}) {
if (node.nodeType !== 1 || node.tagName !== 'IMG') {
return;
}
var payload = { src: node.src };
var cardSection = builder.createCardSection('my-image', payload);
addSection(cardSection);
}
var options = {
parserPlugins: [imageToCardParser]
};
var editor = new Mobiledoc.Editor(options);
var element = document.querySelector('#editor');
editor.render(element);
```

Parser hooks are called with two arguments:

* `node` - The node of DOM being parsed. This may be a text node or an element.
* `builder` - The abstract model builder.
* `env` - An object containing three callbacks to modify the abstract
* `addSection` - Close the current section and add a new one
* `addMarkerable` - Add a markerable (marker or atom) to the current section
* `skipNode` - Skip this node, bypass all remaining parse steps for it

### Contributing

Fork the repo, write a test, make a change, open a PR.
Expand Down

0 comments on commit 0d1247d

Please sign in to comment.