Skip to content

Commit

Permalink
Merge branches 'i/github-writer/17' and 'i/github-writer/93' into i/5988
Browse files Browse the repository at this point in the history
  • Loading branch information
fredck committed Jun 1, 2020
3 parents 8794d95 + d174181 + 19503db commit 57e8958
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/ckeditor5-markdown-gfm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@ckeditor/ckeditor5-engine": "^19.0.1",
"marked": "^0.7.0",
"turndown": "^5.0.3",
"turndown": "^6.0.0",
"turndown-plugin-gfm": "^1.0.2"
},
"engines": {
Expand Down
39 changes: 35 additions & 4 deletions packages/ckeditor5-markdown-gfm/src/html2markdown/html2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,43 @@ import { gfm } from 'turndown-plugin-gfm';
{
const originalEscape = TurndownService.prototype.escape;
TurndownService.prototype.escape = function( string ) {
string = originalEscape( string );
// Urls should not be escaped. Our strategy is using a regex to find them and escape everything
// which is out of the matches parts.

// Escape "<".
string = string.replace( /</g, '\\<' );
// eslint-disable-next-line max-len
const regex = /\b(?:https?:\/\/|www\.)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’])/g;

return string;
let escaped = '';
let lastIndex = 0;
let m;
do {
m = regex.exec( string );

// The substring should to to the matched index or, if nothing found, the end of the string.
const index = m ? m.index : string.length;

// Append the substring between the last match and the current one (if anything).
if ( index > lastIndex ) {
escaped += escape( string.substring( lastIndex, index ) );
}

// Append the match itself now, if anything.
m && ( escaped += m[ 0 ] );

lastIndex = regex.lastIndex;
}
while ( m );

return escaped;

function escape( string ) {
string = originalEscape( string );

// Escape "<".
string = string.replace( /</g, '\\<' );

return string;
}
};
}

Expand Down
36 changes: 36 additions & 0 deletions packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,41 @@ describe( 'GFMDataProcessor', () => {
'`code `` code ``` `'
);
} );

it( 'should handle triple ticks inside code', () => {
testDataProcessor(
'````\n' +
'```\n' +
'Code\n' +
'```\n' +
'````',

'<pre><code>' +
'```\n' +
'Code\n' +
'```' +
'</code></pre>'
);
} );

it( 'should handle triple and quatruple ticks inside code', () => {
testDataProcessor(
'`````\n' +
'````\n' +
'```\n' +
'Code\n' +
'```\n' +
'````\n' +
'`````',

'<pre><code>' +
'````\n' +
'```\n' +
'Code\n' +
'```\n' +
'````' +
'</code></pre>'
);
} );
} );
} );
43 changes: 43 additions & 0 deletions packages/ckeditor5-markdown-gfm/tests/gfmdataprocessor/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import { testDataProcessor } from '../_utils/utils';

describe( 'GFMDataProcessor', () => {
describe( 'text', () => {
describe( 'urls', () => {
it( 'should not escape urls', () => {
testDataProcessor(
'escape\\_this https://test.com/do_[not]-escape escape\\_this',
'<p>escape_this https://test.com/do_[not]-escape escape_this</p>'
);
} );

it( 'should not escape urls (at start)', () => {
testDataProcessor(
'https://test.com/do_[not]-escape escape\\_this',
'<p>https://test.com/do_[not]-escape escape_this</p>'
);
} );

it( 'should not escape urls (at end)', () => {
testDataProcessor(
'escape\\_this https://test.com/do_[not]-escape',
'<p>escape_this https://test.com/do_[not]-escape</p>'
);
} );

[
'https://test.com/do_[not]-escape',
'http://test.com/do_[not]-escape',
'www.test.com/do_[not]-escape'
].forEach( url => {
it( `should not escape urls (${ url })`, () => {
testDataProcessor( url, `<p>${ url }</p>` );
} );
} );
} );
} );
} );

0 comments on commit 57e8958

Please sign in to comment.