@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.
- package/LICENSE.md +1 -1
- package/build/markdown-gfm.js +2 -2
- package/package.json +17 -12
- package/src/gfmdataprocessor.js +56 -76
- package/src/html2markdown/html2markdown.js +103 -130
- package/src/index.js +1 -4
- package/src/markdown.js +14 -21
- package/src/markdown2html/markdown2html.js +31 -39
- package/build/markdown-gfm.js.map +0 -1
@@ -1,153 +1,126 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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(
|
73
|
-
|
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(
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
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-
|
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-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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 };
|