Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Markdown and HTML without requiring a double line break #4232

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
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: 18 additions & 4 deletions blocks/api/raw-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,30 @@ export default function rawHandler( { HTML, plainText = '', mode = 'AUTO', tagNa
return parseWithGrammar( HTML );
}

// 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 ) {
// Parse Markdown (and HTML) if:
// * There is a plain text version.
// * The HTML version has no formatting.
if ( plainText && isPlain( HTML ) ) {
const converter = new showdown.Converter();

converter.setOption( 'noHeaderId', true );
converter.setOption( 'tables', true );

HTML = converter.makeHtml( plainText );

// Switch to inline mode if:
// * The current mode is AUTO.
// * The original plain text had no line breaks.
// * The original plain text was not an HTML paragraph.
// * The converted text is just a paragraph.
if (
mode === 'AUTO' &&
plainText.indexOf( '\n' ) === -1 &&
plainText.indexOf( '<p>' ) !== 0 &&
HTML.indexOf( '<p>' ) === 0
) {
mode = 'INLINE';
}
}

// An array of HTML strings and block objects. The blocks replace matched shortcodes.
Expand Down
32 changes: 27 additions & 5 deletions blocks/api/raw-handling/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { equal, deepEqual } from 'assert';
import rawHandler from '../index';
import { registerBlockType, unregisterBlockType, setUnknownTypeHandlerName } from '../../registration';
import { createBlock } from '../../factory';
import { getBlockContent } from '../../serializer';

describe( 'rawHandler', () => {
it( 'should convert recognised raw content', () => {
Expand Down Expand Up @@ -104,13 +105,34 @@ describe( 'rawHandler', () => {
equal( filtered, '<em>test</em>' );
} );

it( 'should always return blocks', () => {
const blocks = rawHandler( {
HTML: 'test',
mode: 'BLOCKS',
it( 'should parse Markdown', () => {
const filtered = rawHandler( {
HTML: '* one<br>* two<br>* three',
plainText: '* one\n* two\n* three',
mode: 'AUTO',
} ).map( getBlockContent ).join( '' );

equal( filtered, '<ul>\n <li>one</li>\n <li>two</li>\n <li>three</li>\n</ul>' );
} );

it( 'should parse inline Markdown', () => {
const filtered = rawHandler( {
HTML: 'Some **bold** text.',
plainText: 'Some **bold** text.',
mode: 'AUTO',
} );

equal( Array.isArray( blocks ), true );
equal( filtered, 'Some <strong>bold</strong> text.' );
} );

it( 'should parse HTML in plainText', () => {
const filtered = rawHandler( {
HTML: '&lt;p&gt;Some &lt;strong&gt;bold&lt;/strong&gt; text.&lt;/p&gt;',
plainText: '<p>Some <strong>bold</strong> text.</p>',
mode: 'AUTO',
} ).map( getBlockContent ).join( '' );

equal( filtered, '<p>Some <strong>bold</strong> text.</p>' );
} );
} );

Expand Down