|
|
@@ -10,48 +10,51 @@ 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;
|
|
|
+const originalEscape = TurndownService.prototype.escape;
|
|
|
|
|
|
- let escaped = '';
|
|
|
- let lastIndex = 0;
|
|
|
- let m;
|
|
|
- do {
|
|
|
- m = regex.exec( string );
|
|
|
+function escape( string ) {
|
|
|
+ string = originalEscape( 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;
|
|
|
+ // Escape "<".
|
|
|
+ string = string.replace( /</g, '\\<' );
|
|
|
|
|
|
- // Append the substring between the last match and the current one (if anything).
|
|
|
- if ( index > lastIndex ) {
|
|
|
- escaped += escape( string.substring( lastIndex, index ) );
|
|
|
- }
|
|
|
+ return string;
|
|
|
+}
|
|
|
|
|
|
- // Append the match itself now, if anything.
|
|
|
- m && ( escaped += m[ 0 ] );
|
|
|
+// eslint-disable-next-line max-len
|
|
|
+const regex = /\b(?:https?:\/\/|www\.)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’])/g;
|
|
|
|
|
|
- lastIndex = regex.lastIndex;
|
|
|
- }
|
|
|
- while ( m );
|
|
|
+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.
|
|
|
|
|
|
- return escaped;
|
|
|
+ let escaped = '';
|
|
|
+ let lastIndex = 0;
|
|
|
+ let m;
|
|
|
|
|
|
- function escape( string ) {
|
|
|
- string = originalEscape( string );
|
|
|
+ do {
|
|
|
+ m = regex.exec( string );
|
|
|
|
|
|
- // Escape "<".
|
|
|
- string = string.replace( /</g, '\\<' );
|
|
|
+ // The substring should to the matched index or, if nothing found, the end of the string.
|
|
|
+ const index = m ? m.index : string.length;
|
|
|
|
|
|
- return string;
|
|
|
+ // 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.
|
|
|
+ if ( m ) {
|
|
|
+ escaped += m[ 0 ];
|
|
|
+ }
|
|
|
+
|
|
|
+ lastIndex = regex.lastIndex;
|
|
|
+ }
|
|
|
+ while ( m );
|
|
|
+
|
|
|
+ return escaped;
|
|
|
+};
|
|
|
|
|
|
const turndownService = new TurndownService( {
|
|
|
codeBlockStyle: 'fenced',
|