@wordpress/block-library
Version:
Block library for the WordPress editor.
105 lines (100 loc) • 2.43 kB
JavaScript
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
const metadata = {
$schema: "https://schemas.wp.org/trunk/block.json",
apiVersion: 2,
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",
__experimentalRole: "content"
},
caption: {
type: "string",
source: "html",
selector: "figcaption",
__experimentalRole: "content"
},
type: {
type: "string",
__experimentalRole: "content"
},
providerNameSlug: {
type: "string",
__experimentalRole: "content"
},
allowResponsive: {
type: "boolean",
"default": true
},
responsive: {
type: "boolean",
"default": false,
__experimentalRole: "content"
},
previewable: {
type: "boolean",
"default": true,
__experimentalRole: "content"
}
},
supports: {
align: true
},
editorStyle: "wp-block-embed-editor",
style: "wp-block-embed"
};
const {
name: EMBED_BLOCK
} = metadata;
/**
* Default transforms for generic embeds.
*/
const transforms = {
from: [{
type: 'raw',
isMatch: node => {
var _node$textContent, _node$textContent$mat;
return node.nodeName === 'P' && /^\s*(https?:\/\/\S+)\s*$/i.test(node.textContent) && ((_node$textContent = node.textContent) === null || _node$textContent === void 0 ? void 0 : (_node$textContent$mat = _node$textContent.match(/https/gi)) === null || _node$textContent$mat === void 0 ? void 0 : _node$textContent$mat.length) === 1;
},
transform: node => {
return createBlock(EMBED_BLOCK, {
url: node.textContent.trim()
});
}
}],
to: [{
type: 'block',
blocks: ['core/paragraph'],
isMatch: _ref => {
let {
url
} = _ref;
return !!url;
},
transform: _ref2 => {
let {
url,
caption
} = _ref2;
let value = `<a href="${url}">${url}</a>`;
if (caption !== null && caption !== void 0 && caption.trim()) {
value += `<br />${caption}`;
}
return createBlock('core/paragraph', {
content: value
});
}
}]
};
export default transforms;
//# sourceMappingURL=transforms.js.map