This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branches 't/ckeditor5/5988' and 't/ckeditor5/6007' into github-rte
# Conflicts: # package.json
- Loading branch information
Showing
26 changed files
with
538 additions
and
2,568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
node_modules/ | ||
/node_modules/ | ||
/build/ | ||
/package-lock.json | ||
/yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import TurndownService from 'turndown'; | ||
import { gfm } from 'turndown-plugin-gfm'; | ||
|
||
// Overrides the escape() method, enlarging it. | ||
{ | ||
const originalEscape = TurndownService.prototype.escape; | ||
TurndownService.prototype.escape = function( 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. | ||
|
||
// eslint-disable-next-line max-len | ||
const regex = /\b(?:https?:\/\/|www\.)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’])/g; | ||
|
||
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; | ||
} | ||
}; | ||
} | ||
|
||
const turndownService = new TurndownService( { | ||
codeBlockStyle: 'fenced', | ||
hr: '---', | ||
headingStyle: 'atx' | ||
} ); | ||
|
||
turndownService.use( [ | ||
gfm, | ||
todoList | ||
] ); | ||
|
||
export default function html2markdown( html ) { | ||
return turndownService.turndown( html ); | ||
} | ||
|
||
export { turndownService }; | ||
|
||
// This is a copy of the original taskListItems rule from turdown-plugin-gfm, with minor changes. | ||
function todoList( turndownService ) { | ||
turndownService.addRule( 'taskListItems', { | ||
filter( node ) { | ||
return node.type === 'checkbox' && | ||
// Changes here as CKEditor outputs a deeper structure. | ||
( node.parentNode.nodeName === 'LI' || node.parentNode.parentNode.nodeName === 'LI' ); | ||
}, | ||
replacement( content, node ) { | ||
return ( node.checked ? '[x]' : '[ ]' ) + ' '; | ||
} | ||
} ); | ||
} |
Oops, something went wrong.