@wordpress/block-library
Version:
Block library for the WordPress editor.
104 lines (102 loc) • 2.18 kB
JavaScript
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
const metadata = {
$schema: "https://schemas.wp.org/trunk/block.json",
apiVersion: 3,
name: "core/embed",
title: "Embed",
category: "embed",
description: "Add a block that displays content pulled from other sites, like Twitter or YouTube.",
textdomain: "default",
attributes: {
url: {
type: "string",
role: "content"
},
caption: {
type: "rich-text",
source: "rich-text",
selector: "figcaption",
role: "content"
},
type: {
type: "string",
role: "content"
},
providerNameSlug: {
type: "string",
role: "content"
},
allowResponsive: {
type: "boolean",
"default": true
},
responsive: {
type: "boolean",
"default": false,
role: "content"
},
previewable: {
type: "boolean",
"default": true,
role: "content"
}
},
supports: {
align: true,
spacing: {
margin: true
},
interactivity: {
clientNavigation: true
}
},
editorStyle: "wp-block-embed-editor",
style: "wp-block-embed"
};
import { removeAspectRatioClasses } from './util';
const {
name: EMBED_BLOCK
} = metadata;
/**
* Default transforms for generic embeds.
*/
const transforms = {
from: [{
type: 'raw',
isMatch: node => node.nodeName === 'P' && /^\s*(https?:\/\/\S+)\s*$/i.test(node.textContent) && node.textContent?.match(/https/gi)?.length === 1,
transform: node => {
return createBlock(EMBED_BLOCK, {
url: node.textContent.trim()
});
}
}],
to: [{
type: 'block',
blocks: ['core/paragraph'],
isMatch: ({
url
}) => !!url,
transform: ({
url,
caption,
className
}) => {
let value = `<a href="${url}">${url}</a>`;
if (caption?.trim()) {
value += `<br />${caption}`;
}
return createBlock('core/paragraph', {
content: value,
className: removeAspectRatioClasses(className)
});
}
}]
};
export default transforms;
//# sourceMappingURL=transforms.js.map