Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created November 24, 2017 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsf/879ed2f099d079db6d3df9992eee38cb to your computer and use it in GitHub Desktop.
Save mcsf/879ed2f099d079db6d3df9992eee38cb to your computer and use it in GitHub Desktop.
diff --git a/blocks/api/raw-handling/index.js b/blocks/api/raw-handling/index.js
index c5a95ecc..66fb4e39 100644
--- a/blocks/api/raw-handling/index.js
+++ b/blocks/api/raw-handling/index.js
@@ -22,7 +22,7 @@ import imageCorrector from './image-corrector';
import blockquoteNormaliser from './blockquote-normaliser';
import tableNormaliser from './table-normaliser';
import inlineContentConverter from './inline-content-converter';
-import { deepFilterHTML, isInvalidInline, isNotWhitelisted, isPlain, isInline } from './utils';
+import { deepFilterHTML, isInvalidInline, isNotWhitelisted, isPlain, isInline, stripOuterP } from './utils';
import shortcodeConverter from './shortcode-converter';
/**
@@ -48,13 +48,20 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO' } ) {
// If there is a plain text version, the HTML version has no formatting,
// and there is at least a double line break,
// parse any Markdown inside the plain text.
- if ( plainText && isPlain( HTML ) && plainText.indexOf( '\n\n' ) !== -1 ) {
+ if ( plainText && isPlain( HTML ) ) {
const converter = new showdown.Converter();
converter.setOption( 'noHeaderId', true );
converter.setOption( 'tables', true );
HTML = converter.makeHtml( plainText );
+
+ // If there are no paragraphs in the original plain text, the paste
+ // should be treated as inline. However, Markdown conversion returns
+ // P-wrapped paragraphs, so we strip that outer P node.
+ if ( plainText.indexOf( '\n\n' ) === -1 ) {
+ HTML = stripOuterP( HTML );
+ }
}
// Return filtered HTML if it's inline paste or all content is inline.
diff --git a/blocks/api/raw-handling/utils.js b/blocks/api/raw-handling/utils.js
index 1d17199c..9a147124 100644
--- a/blocks/api/raw-handling/utils.js
+++ b/blocks/api/raw-handling/utils.js
@@ -151,6 +151,26 @@ export function isPlain( HTML ) {
return doc.body.childNodes.length === 1 && doc.body.firstChild.nodeType === TEXT_NODE;
}
+/**
+ * Given a string of HTML representing a P node with contents,
+ *
+ * '<p>Hello, <em>you</em>!</p>'
+ *
+ * returns a string representing the contents of the P node:
+ *
+ * 'Hello, <em>you</em>!'
+ *
+ * @param {String} HTML The HTML from which to strip the node.
+ * @return {String} The stripped out HTML, or the original HTML as a fallback.
+ */
+export function stripOuterP( HTML ) {
+ const inputEl = document.createElement( 'div' );
+ inputEl.innerHTML = HTML;
+ return inputEl.children.length === 1 ?
+ inputEl.children[ 0 ].innerHTML :
+ HTML;
+}
+
/**
* Given node filters, deeply filters and mutates a NodeList.
*
diff --git a/blocks/editable/index.js b/blocks/editable/index.js
index a75e28a5..201005a6 100644
--- a/blocks/editable/index.js
+++ b/blocks/editable/index.js
@@ -259,6 +259,7 @@ export default class Editable extends Component {
onPastePreProcess( event ) {
const HTML = this.isPlainTextPaste ? this.pastedPlainText : event.content;
+ //const HTML = event.content;
// Allows us to ask for this information when we get a report.
window.console.log( 'Received HTML:\n\n', this.pastedContent || HTML );
window.console.log( 'Received plain text:\n\n', this.pastedPlainText );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment