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

Code Block should not render embeds (or shortcodes) #13996

Merged
merged 12 commits into from
May 2, 2019
21 changes: 20 additions & 1 deletion packages/block-library/src/code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
davilera marked this conversation as resolved.
Show resolved Hide resolved
import {
Path,
SVG,
Expand Down Expand Up @@ -68,6 +69,24 @@ export const settings = {
edit,

save( { attributes } ) {
return <pre><code>{ attributes.content }</code></pre>;
const content = compose(
escapeOpeningSquareBrackets,
escapeProtocolInIsolatedUrls
)( escapeAmpersands( attributes.content || '' ) );
davilera marked this conversation as resolved.
Show resolved Hide resolved
return <pre><code>{ content }</code></pre>;
},
};

function escapeAmpersands( content ) {
return content.replace( /&/g, '&amp;' );
}

function escapeOpeningSquareBrackets( content ) {
// This replicates the escaping of HTML tags, where
// a tag like <strong> becomes &lt;strong>
return content.replace( /\[/g, '&#91;' );
}

function escapeProtocolInIsolatedUrls( content ) {
return content.replace( /(^[^.]^https?:)\/\/([^\s]+$[^.]$)/m, '$1&#47;&#47;$2' );
davilera marked this conversation as resolved.
Show resolved Hide resolved
}