Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branches 't/github-rte/17' and 't/github-rte/93' into t/ckedito…
Browse files Browse the repository at this point in the history
…r5/5988
  • Loading branch information
fredck committed Mar 13, 2020
3 parents baf4543 + 43c7972 + 19446ee commit 504fb7b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@ckeditor/ckeditor5-engine": "^17.0.0",
"marked": "^0.7.0",
"turndown": "^5.0.3",
"turndown": "^6.0.0",
"turndown-plugin-gfm": "^1.0.2"
},
"devDependencies": {
Expand Down
39 changes: 35 additions & 4 deletions src/html2markdown/html2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,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 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 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 504fb7b

Please sign in to comment.