@ckeditor/ckeditor5-markdown-gfm 35.3.2 → 36.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +1,126 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module markdown-gfm/html2markdown
8
7
  */
9
-
8
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
9
+ // Importing types for this package is problematic, so it's omitted.
10
+ // @ts-ignore
10
11
  import TurndownService from 'turndown';
12
+ // There no avaialble types for 'turndown-plugin-gfm' module and it's not worth to generate them on our own.
13
+ // @ts-ignore
11
14
  import { gfm } from 'turndown-plugin-gfm';
12
-
13
15
  // Override the original escape method by not escaping links.
14
16
  const originalEscape = TurndownService.prototype.escape;
15
-
16
- function escape( string ) {
17
- string = originalEscape( string );
18
-
19
- // Escape "<".
20
- string = string.replace( /</g, '\\<' );
21
-
22
- return string;
17
+ function escape(string) {
18
+ string = originalEscape(string);
19
+ // Escape "<".
20
+ string = string.replace(/</g, '\\<');
21
+ return string;
23
22
  }
24
-
25
- TurndownService.prototype.escape = function( string ) {
26
- // Urls should not be escaped. Our strategy is using a regex to find them and escape everything
27
- // which is out of the matches parts.
28
-
29
- let escaped = '';
30
- let lastLinkEnd = 0;
31
-
32
- for ( const match of matchAutolink( string ) ) {
33
- const index = match.index;
34
-
35
- // Append the substring between the last match and the current one (if anything).
36
- if ( index > lastLinkEnd ) {
37
- escaped += escape( string.substring( lastLinkEnd, index ) );
38
- }
39
-
40
- const matchedURL = match[ 0 ];
41
-
42
- escaped += matchedURL;
43
-
44
- lastLinkEnd = index + matchedURL.length;
45
- }
46
-
47
- // Add text after the last link or at the string start if no matches.
48
- if ( lastLinkEnd < string.length ) {
49
- escaped += escape( string.substring( lastLinkEnd, string.length ) );
50
- }
51
-
52
- return escaped;
23
+ TurndownService.prototype.escape = function (string) {
24
+ // Urls should not be escaped. Our strategy is using a regex to find them and escape everything
25
+ // which is out of the matches parts.
26
+ let escaped = '';
27
+ let lastLinkEnd = 0;
28
+ for (const match of matchAutolink(string)) {
29
+ const index = match.index;
30
+ // Append the substring between the last match and the current one (if anything).
31
+ if (index > lastLinkEnd) {
32
+ escaped += escape(string.substring(lastLinkEnd, index));
33
+ }
34
+ const matchedURL = match[0];
35
+ escaped += matchedURL;
36
+ lastLinkEnd = index + matchedURL.length;
37
+ }
38
+ // Add text after the last link or at the string start if no matches.
39
+ if (lastLinkEnd < string.length) {
40
+ escaped += escape(string.substring(lastLinkEnd, string.length));
41
+ }
42
+ return escaped;
53
43
  };
54
-
55
- const turndownService = new TurndownService( {
56
- codeBlockStyle: 'fenced',
57
- hr: '---',
58
- headingStyle: 'atx'
59
- } );
60
-
61
- turndownService.use( [
62
- gfm,
63
- todoList
64
- ] );
65
-
44
+ const turndownService = new TurndownService({
45
+ codeBlockStyle: 'fenced',
46
+ hr: '---',
47
+ headingStyle: 'atx'
48
+ });
49
+ turndownService.use([
50
+ gfm,
51
+ todoList
52
+ ]);
66
53
  /**
67
54
  * Parses HTML to a markdown.
68
- *
69
- * @param {String} html
70
- * @returns {String}
71
55
  */
72
- export default function html2markdown( html ) {
73
- return turndownService.turndown( html );
56
+ export default function html2markdown(html) {
57
+ return turndownService.turndown(html);
74
58
  }
75
-
76
59
  export { turndownService };
77
-
78
60
  // This is a copy of the original taskListItems rule from turdown-plugin-gfm, with minor changes.
79
- function todoList( turndownService ) {
80
- turndownService.addRule( 'taskListItems', {
81
- filter( node ) {
82
- return node.type === 'checkbox' &&
83
- // Changes here as CKEditor outputs a deeper structure.
84
- ( node.parentNode.nodeName === 'LI' || node.parentNode.parentNode.nodeName === 'LI' );
85
- },
86
- replacement( content, node ) {
87
- return ( node.checked ? '[x]' : '[ ]' ) + ' ';
88
- }
89
- } );
61
+ function todoList(turndownService) {
62
+ turndownService.addRule('taskListItems', {
63
+ filter(node) {
64
+ return node.type === 'checkbox' &&
65
+ // Changes here as CKEditor outputs a deeper structure.
66
+ (node.parentNode.nodeName === 'LI' || node.parentNode.parentNode.nodeName === 'LI');
67
+ },
68
+ replacement(content, node) {
69
+ return (node.checked ? '[x]' : '[ ]') + ' ';
70
+ }
71
+ });
90
72
  }
91
-
92
73
  // Autolink matcher.
93
74
  const regex = new RegExp(
94
- // Prefix.
95
- /\b(?:(?:https?|ftp):\/\/|www\.)/.source +
96
-
97
- // Domain name.
98
- /(?![-_])(?:[-_a-z0-9\u00a1-\uffff]{1,63}\.)+(?:[a-z\u00a1-\uffff]{2,63})/.source +
99
-
100
- // The rest.
101
- /(?:[^\s<>]*)/.source,
102
- 'gi'
103
- );
104
-
105
- // Trimming end of link.
106
- // https://github.github.com/gfm/#autolinks-extension-
107
- function* matchAutolink( string ) {
108
- for ( const match of string.matchAll( regex ) ) {
109
- const matched = match[ 0 ];
110
- const length = autolinkFindEnd( matched );
111
-
112
- yield Object.assign(
113
- [ matched.substring( 0, length ) ],
114
- { index: match.index }
115
- );
116
-
117
- // We could adjust regex.lastIndex but it's not needed because what we skipped is for sure not a valid URL.
118
- }
75
+ // Prefix.
76
+ /\b(?:(?:https?|ftp):\/\/|www\.)/.source +
77
+ // Domain name.
78
+ /(?![-_])(?:[-_a-z0-9\u00a1-\uffff]{1,63}\.)+(?:[a-z\u00a1-\uffff]{2,63})/.source +
79
+ // The rest.
80
+ /(?:[^\s<>]*)/.source, 'gi');
81
+ /**
82
+ * Trimming end of link.
83
+ * https://github.github.com/gfm/#autolinks-extension-
84
+ */
85
+ function* matchAutolink(string) {
86
+ for (const match of string.matchAll(regex)) {
87
+ const matched = match[0];
88
+ const length = autolinkFindEnd(matched);
89
+ yield Object.assign([matched.substring(0, length)], { index: match.index });
90
+ // We could adjust regex.lastIndex but it's not needed because what we skipped is for sure not a valid URL.
91
+ }
119
92
  }
120
-
121
- // Returns the new length of the link (after it would trim trailing characters).
122
- function autolinkFindEnd( string ) {
123
- let length = string.length;
124
-
125
- while ( length > 0 ) {
126
- const char = string[ length - 1 ];
127
-
128
- if ( '?!.,:*_~\'"'.includes( char ) ) {
129
- length--;
130
- } else if ( char == ')' ) {
131
- let openBrackets = 0;
132
-
133
- for ( let i = 0; i < length; i++ ) {
134
- if ( string[ i ] == '(' ) {
135
- openBrackets++;
136
- } else if ( string[ i ] == ')' ) {
137
- openBrackets--;
138
- }
139
- }
140
-
141
- // If there is fewer opening brackets then closing ones we should remove a closing bracket.
142
- if ( openBrackets < 0 ) {
143
- length--;
144
- } else {
145
- break;
146
- }
147
- } else {
148
- break;
149
- }
150
- }
151
-
152
- return length;
93
+ /**
94
+ * Returns the new length of the link (after it would trim trailing characters).
95
+ */
96
+ function autolinkFindEnd(string) {
97
+ let length = string.length;
98
+ while (length > 0) {
99
+ const char = string[length - 1];
100
+ if ('?!.,:*_~\'"'.includes(char)) {
101
+ length--;
102
+ }
103
+ else if (char == ')') {
104
+ let openBrackets = 0;
105
+ for (let i = 0; i < length; i++) {
106
+ if (string[i] == '(') {
107
+ openBrackets++;
108
+ }
109
+ else if (string[i] == ')') {
110
+ openBrackets--;
111
+ }
112
+ }
113
+ // If there is fewer opening brackets then closing ones we should remove a closing bracket.
114
+ if (openBrackets < 0) {
115
+ length--;
116
+ }
117
+ else {
118
+ break;
119
+ }
120
+ }
121
+ else {
122
+ break;
123
+ }
124
+ }
125
+ return length;
153
126
  }
package/src/index.js CHANGED
@@ -1,11 +1,8 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module markdown-gfm
8
7
  */
9
-
10
8
  export { default as Markdown } from './markdown';
11
-
package/src/markdown.js CHANGED
@@ -1,36 +1,29 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module markdown-gfm/markdown
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import GFMDataProcessor from './gfmdataprocessor';
12
-
13
10
  /**
14
11
  * The GitHub Flavored Markdown (GFM) plugin.
15
12
  *
16
13
  * For a detailed overview, check the {@glink features/markdown Markdown feature documentation}.
17
- *
18
- * @extends module:core/plugin~Plugin
19
14
  */
20
15
  export default class Markdown extends Plugin {
21
- /**
22
- * @inheritDoc
23
- */
24
- constructor( editor ) {
25
- super( editor );
26
-
27
- editor.data.processor = new GFMDataProcessor( editor.data.viewDocument );
28
- }
29
-
30
- /**
31
- * @inheritDoc
32
- */
33
- static get pluginName() {
34
- return 'Markdown';
35
- }
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ constructor(editor) {
20
+ super(editor);
21
+ editor.data.processor = new GFMDataProcessor(editor.data.viewDocument);
22
+ }
23
+ /**
24
+ * @inheritDoc
25
+ */
26
+ static get pluginName() {
27
+ return 'Markdown';
28
+ }
36
29
  }
@@ -1,52 +1,44 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module markdown-gfm/markdown2html
8
7
  */
9
-
10
8
  import { marked } from 'marked';
11
-
12
9
  // Overrides.
13
- marked.use( {
14
- tokenizer: {
15
- // Disable the autolink rule in the lexer.
16
- autolink: () => null,
17
- url: () => null
18
- },
19
- renderer: {
20
- checkbox( ...args ) {
21
- // Remove bogus space after <input type="checkbox"> because it would be preserved
22
- // by DomConverter as it's next to an inline object.
23
- return Object.getPrototypeOf( this ).checkbox.call( this, ...args ).trimRight();
24
- },
25
-
26
- code( ...args ) {
27
- // Since marked v1.2.8, every <code> gets a trailing "\n" whether it originally
28
- // ended with one or not (see https://github.com/markedjs/marked/issues/1884 to learn why).
29
- // This results in a redundant soft break in the model when loaded into the editor, which
30
- // is best prevented at this stage. See https://github.com/ckeditor/ckeditor5/issues/11124.
31
- return Object.getPrototypeOf( this ).code.call( this, ...args ).replace( '\n</code>', '</code>' );
32
- }
33
- }
34
- } );
35
-
10
+ marked.use({
11
+ tokenizer: {
12
+ // Disable the autolink rule in the lexer.
13
+ autolink: () => null,
14
+ url: () => null
15
+ },
16
+ renderer: {
17
+ checkbox(...args) {
18
+ // Remove bogus space after <input type="checkbox"> because it would be preserved
19
+ // by DomConverter as it's next to an inline object.
20
+ return Object.getPrototypeOf(this).checkbox.call(this, ...args).trimRight();
21
+ },
22
+ code(...args) {
23
+ // Since marked v1.2.8, every <code> gets a trailing "\n" whether it originally
24
+ // ended with one or not (see https://github.com/markedjs/marked/issues/1884 to learn why).
25
+ // This results in a redundant soft break in the model when loaded into the editor, which
26
+ // is best prevented at this stage. See https://github.com/ckeditor/ckeditor5/issues/11124.
27
+ return Object.getPrototypeOf(this).code.call(this, ...args).replace('\n</code>', '</code>');
28
+ }
29
+ }
30
+ });
36
31
  /**
37
32
  * Parses markdown string to an HTML.
38
- *
39
- * @param {String} markdown
40
- * @returns {String}
41
33
  */
42
- export default function markdown2html( markdown ) {
43
- return marked.parse( markdown, {
44
- gfm: true,
45
- breaks: true,
46
- tables: true,
47
- xhtml: true,
48
- headerIds: false
49
- } );
34
+ export default function markdown2html(markdown) {
35
+ const options = {
36
+ gfm: true,
37
+ breaks: true,
38
+ tables: true,
39
+ xhtml: true,
40
+ headerIds: false
41
+ };
42
+ return marked.parse(markdown, options);
50
43
  }
51
-
52
44
  export { marked };