Skip to content

Commit

Permalink
Raw handling: skip inline shortcodes (#6329)
Browse files Browse the repository at this point in the history
* Skip inline shortcodes

* If the shortcode contains HTML, convert to block
  • Loading branch information
ellatrix authored and mcsf committed Apr 27, 2018
1 parent 0a1cd7d commit 81af13e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions blocks/api/raw-handling/shortcode-converter.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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' );

Expand All @@ -32,11 +32,23 @@ 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 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|<p>)\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
Expand All @@ -59,9 +71,9 @@ function segmentHTMLToShortcodeBlock( HTML ) {
);

return [
HTML.substr( 0, match.index ),
beforeHTML,
block,
...segmentHTMLToShortcodeBlock( HTML.substr( match.index + match.content.length ) ),
...segmentHTMLToShortcodeBlock( HTML.substr( lastIndex ) ),
];
}

Expand Down

0 comments on commit 81af13e

Please sign in to comment.