From c1c21fc52789f5138b902d4ef5bee9247c8cdf49 Mon Sep 17 00:00:00 2001 From: iseulde Date: Fri, 20 Apr 2018 22:38:12 +0200 Subject: [PATCH 1/2] Skip inline shortcodes --- blocks/api/raw-handling/shortcode-converter.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/blocks/api/raw-handling/shortcode-converter.js b/blocks/api/raw-handling/shortcode-converter.js index 11a9491326f2f8..62cfac7c043f05 100644 --- a/blocks/api/raw-handling/shortcode-converter.js +++ b/blocks/api/raw-handling/shortcode-converter.js @@ -15,7 +15,7 @@ import { getBlockAttributes } from '../parser'; */ const { shortcode } = window.wp; -function segmentHTMLToShortcodeBlock( HTML ) { +function segmentHTMLToShortcodeBlock( HTML, lastIndex = 0 ) { // Get all matches. const transformsFrom = getBlockTransforms( 'from' ); @@ -32,11 +32,18 @@ function segmentHTMLToShortcodeBlock( HTML ) { const transformTag = first( transformTags ); let match; - let lastIndex = 0; if ( ( match = shortcode.next( transformTag, HTML, lastIndex ) ) ) { + const beforeHTML = HTML.substr( 0, match.index ); + lastIndex = match.index + match.content.length; + // If not on new line (or in paragraph from Markdown converter), + // consider shortcode as inline text. + if ( ! /(\n|

)\s*$/.test( beforeHTML ) ) { + return segmentHTMLToShortcodeBlock( HTML, lastIndex ); + } + const attributes = mapValues( pickBy( transformation.attributes, ( schema ) => schema.shortcode ), // Passing all of `match` as second argument is intentionally broad @@ -59,9 +66,9 @@ function segmentHTMLToShortcodeBlock( HTML ) { ); return [ - HTML.substr( 0, match.index ), + beforeHTML, block, - ...segmentHTMLToShortcodeBlock( HTML.substr( match.index + match.content.length ) ), + ...segmentHTMLToShortcodeBlock( HTML.substr( lastIndex ) ), ]; } From 10df6018248ce9b7eb9c05130db55c8ee31fed28 Mon Sep 17 00:00:00 2001 From: iseulde Date: Fri, 20 Apr 2018 22:58:03 +0200 Subject: [PATCH 2/2] If the shortcode contains HTML, convert to block --- blocks/api/raw-handling/shortcode-converter.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/blocks/api/raw-handling/shortcode-converter.js b/blocks/api/raw-handling/shortcode-converter.js index 62cfac7c043f05..013615a3a6c498 100644 --- a/blocks/api/raw-handling/shortcode-converter.js +++ b/blocks/api/raw-handling/shortcode-converter.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { some, castArray, first, mapValues, pickBy } from 'lodash'; +import { some, castArray, first, mapValues, pickBy, includes } from 'lodash'; /** * Internal dependencies @@ -38,9 +38,14 @@ function segmentHTMLToShortcodeBlock( HTML, lastIndex = 0 ) { lastIndex = match.index + match.content.length; - // If not on new line (or in paragraph from Markdown converter), - // consider shortcode as inline text. - if ( ! /(\n|

)\s*$/.test( beforeHTML ) ) { + // If the shortcode content does not contain HTML and the shortcode is + // not on a new line (or in paragraph from Markdown converter), + // consider the shortcode as inline text, and thus skip conversion for + // this segment. + if ( + ! includes( match.shortcode.content || '', '<' ) && + ! /(\n|

)\s*$/.test( beforeHTML ) + ) { return segmentHTMLToShortcodeBlock( HTML, lastIndex ); }