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

Add documentation describing block transforms #5189

Merged
merged 3 commits into from
Feb 23, 2018
Merged
Changes from 1 commit
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
61 changes: 60 additions & 1 deletion docs/block-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,66 @@ attributes: {

#### Transforms (optional)

Work in progress...
* **Type:** `Array`

Transforms provide rules for what a block can be transformed from and what it can be transformed to. A block can be transformed from another block, a shortcode, a regular expression or a raw DOM node.

For example, a paragraph block can be transformed into a heading block.

```js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do an es5/esnext version like the other examples in the handbook?

transforms: {
from: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { content } ) => {
return createBlock( 'core/heading', {
content,
} );
},
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we stop here in this example and leave the shortcode for another example (to match the text above)?

{
type: 'shortcode',
// Shortcode tag can also be an array of shortcode aliases
tag: 'caption',
attributes: {
// An attribute can be source from a tag attribute in the shortcode content
url: {
type: 'string',
source: 'attribute',
attribute: 'src',
selector: 'img',
},
// An attribute can be source from the shortcode attributes
align: {
type: 'string',
shortcode: ( { named: { align = 'alignnone' } } ) => {
return align.replace( 'align', '' );
},
},
},
},
]
},
```

A block can also be transformed into another block type. For example, a heading block can be transformed into a paragraph block.

```js
transforms: {
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { content } ) => {
return createBlock( 'core/paragraph', {
content,
} );
},
},
],
},
```

#### useOnce (optional)

Expand Down