Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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