Skip to content

Commit

Permalink
Pasting: Convert unknown shortcodes to Shortcode block
Browse files Browse the repository at this point in the history
Blocks can specify shortcodes from which blocks should be automatically
created. For any other shortcode, the core Shortcode block will now act
as a fallback.

The main benefit of this is that, within the Shortcode block, the code
will be properly handled and never mishandled. Notably, this fixes the
case where a shortcode sitting in a Paragraph block will be picked up
with its double quotes rendered as `&doubl;`, thus breaking parsing of a
shortcode's attributes.
  • Loading branch information
mcsf committed Nov 22, 2017
1 parent 174e6b9 commit b714abe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion blocks/api/raw-handling/shortcode-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function( HTML ) {

const attributes = mapValues(
pickBy( transform.attributes, ( schema ) => schema.shortcode ),
( schema ) => schema.shortcode( match.shortcode.attrs ),
( schema ) => schema.shortcode( match.shortcode.attrs, match ),
);

const block = createBlock(
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './shortcode';
import './image';
import './gallery';
import './heading';
Expand All @@ -16,7 +17,6 @@ import './freeform';
import './latest-posts';
import './categories';
import './cover-image';
import './shortcode';
import './text-columns';
import './verse';
import './video';
Expand Down
21 changes: 21 additions & 0 deletions blocks/library/shortcode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ registerBlockType( 'core/shortcode', {
},
},

transforms: {
from: [
{
type: 'shortcode',
// Per "Shortcode names should be all lowercase and use all
// letters, but numbers and underscores should work fine too.
// Be wary of using hyphens (dashes), you'll be better off not
// using them." in https://codex.wordpress.org/Shortcode_API
tag: '[A-z0-9_-]+',
attributes: {
text: {
type: 'string',
shortcode: ( attrs, { content } ) => {
return content;
},
},
},
},
],
},

className: false,

supportHTML: false,
Expand Down

0 comments on commit b714abe

Please sign in to comment.