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 Block Transform to transform Embed blocks into Paragraph blocks. #17413

Merged
merged 16 commits into from
May 11, 2020
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
30 changes: 12 additions & 18 deletions packages/block-library/src/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
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';

Expand All @@ -21,33 +21,27 @@ 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,
},
};
} );

export const others = otherEmbeds.map( ( embedDefinition ) => {
const embedSettings = getEmbedBlockSettings( embedDefinition.settings );
return {
...embedDefinition,
settings: getEmbedBlockSettings( embedDefinition.settings ),
settings: {
...embedSettings,
transforms,
},
};
} );
38 changes: 38 additions & 0 deletions packages/block-library/src/embed/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { renderToString } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';

/**
* Default transforms for generic embeds.
*/
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 ),
Copy link
Member

Choose a reason for hiding this comment

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

Why not write the html as a string?

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
2 changes: 1 addition & 1 deletion packages/e2e-tests/fixtures/block-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const EXPECTED_TRANSFORMS = {
},
core__embed: {
originalBlock: 'Embed',
availableTransforms: [ 'Group' ],
availableTransforms: [ 'Group', 'Paragraph' ],
},
'core__file__new-window': {
originalBlock: 'File',
Expand Down