-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 Block Transform to transform Embed blocks into Paragraph blocks. #17413
Changes from 1 commit
f0d1f15
708e1c4
60a3ce4
69f6926
2fa4e1b
bef557a
7a00726
6d88232
5e3eaba
2aa5ec5
5adc5f4
2b22121
2e838d9
1f54392
f8ecaa9
81d4f4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { concat, get } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { common as commonEmbeds, others as otherEmbeds } from './core-embeds'; | ||
import { embedContentIcon } from './icons'; | ||
import { getEmbedBlockSettings } from './settings'; | ||
import transforms from './transforms'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
|
||
export const name = 'core/embed'; | ||
|
||
|
@@ -19,35 +24,37 @@ export const settings = getEmbedBlockSettings( { | |
icon: embedContentIcon, | ||
// Unknown embeds should not be responsive by default. | ||
responsive: false, | ||
transforms: { | ||
from: [ | ||
{ | ||
type: 'raw', | ||
isMatch: ( node ) => node.nodeName === 'P' && /^\s*(https?:\/\/\S+)\s*$/i.test( node.textContent ), | ||
transform: ( node ) => { | ||
return createBlock( 'core/embed', { | ||
url: node.textContent.trim(), | ||
} ); | ||
}, | ||
}, | ||
], | ||
}, | ||
transforms, | ||
} ); | ||
|
||
export const common = commonEmbeds.map( | ||
( embedDefinition ) => { | ||
const embedSettings = getEmbedBlockSettings( embedDefinition.settings ); | ||
return { | ||
...embedDefinition, | ||
settings: getEmbedBlockSettings( embedDefinition.settings ), | ||
settings: { | ||
...embedSettings, | ||
transforms: { | ||
from: concat( get( transforms, 'from', [] ), get( embedSettings, 'transforms.from', [] ) ).filter( Boolean ), | ||
to: concat( get( transforms, 'to', [] ), get( embedSettings, 'transforms.to', [] ) ).filter( Boolean ), | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, apply combined transformations of Same for |
||
}, | ||
}; | ||
} | ||
); | ||
|
||
export const others = otherEmbeds.map( | ||
( embedDefinition ) => { | ||
const embedSettings = getEmbedBlockSettings( embedDefinition.settings ); | ||
return { | ||
...embedDefinition, | ||
settings: getEmbedBlockSettings( embedDefinition.settings ), | ||
settings: { | ||
...embedSettings, | ||
transforms: { | ||
from: concat( get( transforms, 'from', [] ), get( embedSettings, 'transforms.from', [] ) ).filter( Boolean ), | ||
to: concat( get( transforms, 'to', [] ), get( embedSettings, 'transforms.to', [] ) ).filter( Boolean ), | ||
}, | ||
}, | ||
}; | ||
} | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { renderToString } from '@wordpress/element'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
|
||
const transforms = { | ||
from: [ | ||
{ | ||
type: 'raw', | ||
isMatch: ( node ) => node.nodeName === 'P' && /^\s*(https?:\/\/\S+)\s*$/i.test( node.textContent ), | ||
transform: ( node ) => { | ||
return createBlock( 'core/embed', { | ||
url: node.textContent.trim(), | ||
} ); | ||
}, | ||
}, | ||
], | ||
to: [ | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/paragraph' ], | ||
transform: ( { url, caption } ) => { | ||
const link = <a href={ url }>{ caption || url }</a>; | ||
return createBlock( 'core/paragraph', { | ||
content: renderToString( link ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not write the html as a string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mind the added peace of mind of running this through the element serialiser. Do you have drawbacks in mind? |
||
} ); | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export default transforms; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is a breaking change or not. Simply changed the schema of JSON to make it consistent with other transforms.
Same for Speaker Deck block.