Skip to content

Commit

Permalink
Markdonw block: fixes issue pasting Markdown
Browse files Browse the repository at this point in the history
There was a bug when pasting Markdown code containing any type of list
in the live preview. The Markdown parser, Markdown-It, removes empty
space at the beginning of new lines, making impossible to paste lists
and nested lists. Also, changes the whitespace CSS property for
paragraphs inside the Live Preview component, to `pre-wrap` so white
space and new lines are not collapsed.
  • Loading branch information
Ferdev committed Jun 25, 2018
1 parent 0a98895 commit ec681b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions modules/markdown/assets/css/jetpack-markdown-block.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
}
.wp-block-jetpack-markdown-block-live-preview p {
min-height: 1.8em;
white-space: pre-wrap;
}
.wp-block-jetpack-markdown-block__live-preview__token {
/* $dark-gray-300 from Gutenberg _colors.scss */
Expand Down
51 changes: 44 additions & 7 deletions modules/markdown/assets/js/components/markdown-live-preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const {
createElement
} = window.wp.element;

const markdownIt = new MarkdownIt( 'zero' ).enable( [
'heading',
'emphasis',
'backticks',
] );
const markdownIt = new MarkdownIt( 'zero' )
.set( { breaks: true } )
.enable( [
'heading',
'emphasis',
'backticks',
'newline',
] );

/*
* Redefines the rules applied by the parser to render each token. This adds
* Redefines the rules applied by the parser to render each token. This adds
* to each token's content, the delimiter the user used (which the parser
* obviously removes in the resulting HTML)
*/
Expand Down Expand Up @@ -64,6 +67,40 @@ markdownIt.renderer.rules.heading_open = function( tokens, idx ) {
}
return `<${ token.tag }><span class="${ LIVE_PREVIEW_TOKEN_CSS_CLASS }">${ token.markup }</span>`;
};
// Overrides the newline rule to keep whitespace at the beginning of new lines
// Original source from `markdown-it/lib/rules_inline/newline.js`
markdownIt.inline.ruler.at( 'newline', function replace( state, silent ) {
let position = state.pos;

if ( state.src.charCodeAt( position ) !== 0x0A/* \n */ ) {
return false;
}

const lastCharPosition = state.pending.length - 1;

// ' \n' -> hardbreak
// Lookup in pending chars is bad practice! Don't copy to other rules!
// Pending string is stored in concat mode, indexed lookups will cause
// convertion to flat mode.
if ( ! silent ) {
if ( lastCharPosition >= 0 && state.pending.charCodeAt( lastCharPosition ) === 0x20 ) {
if ( lastCharPosition >= 1 && state.pending.charCodeAt( lastCharPosition - 1 ) === 0x20 ) {
state.pending = state.pending.replace( / +$/, '' );
state.push( 'hardbreak', 'br', 0 );
} else {
state.pending = state.pending.slice( 0, -1 );
state.push( 'softbreak', 'br', 0 );
}
} else {
state.push( 'softbreak', 'br', 0 );
}
}

position++;

state.pos = position;
return true;
} );

const renderHTML = function( source ) {
if ( source ) {
Expand Down Expand Up @@ -96,7 +133,7 @@ const triggerOnChange = function( evt, source ) {
};

const sourceIsEmpty = function( source ) {
return ! source || '' === source.trim();
return ! source;
};

const ignoreLastInput = function( source ) {
Expand Down

0 comments on commit ec681b3

Please sign in to comment.