Completed
Push — master ( ac3e5d...b47817 )
by Jan
05:29
created

plugin.js ➔ overrideDataProcessor   B

Complexity

Conditions 8

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 22
c 0
b 0
f 0
dl 0
loc 40
rs 7.3333
1
/**
2
 * A markdown output plugin for CKEDITOR.
3
 * Uses showdown.js for conversion and DOMPurifier for HTML filtering.
4
 *
5
 * This software is licensed under MIT License.
6
 *
7
 * Copyright (c) 2019 Jan Böhmer
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 */
27
28
(function() {
29
30
	function overrideDataProcessor(editor)
31
	{
32
		//Both showdown and DOMPurify must be loaded
33
		if(typeof(showdown) == 'undefined') return;
0 ignored issues
show
Bug introduced by
The variable showdown seems to be never declared. If this is a global, consider adding a /** global: showdown */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
34
		if (typeof(DOMPurify) == 'undefined') return;
0 ignored issues
show
Bug introduced by
The variable DOMPurify seems to be never declared. If this is a global, consider adding a /** global: DOMPurify */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
35
36
		//If the dataprocessor were already be overriden do nothing
37
		if(editor.dataProcessor.markdown) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
38
39
		var converter = new showdown.Converter();
40
		//Set some useful options on Showdown
41
		converter.setFlavor('github');
42
		converter.setOption('tables', true);
43
		converter.setOption('strikethrough', true);
44
		converter.setOption('parseImgDimensions', true);
45
		converter.setOption('smartIndentationFix', true);
46
47
		editor.dataProcessor = {
48
			toDataFormat: function(html, fixForBody) {
0 ignored issues
show
Unused Code introduced by
The parameter fixForBody is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
49
				//html = html.replace(/(\r\n|\n|\r)/gm,"");
50
				html = html.replace('<br>', '\n');
51
				//Support for strikethrough in markdown
52
				html = html.replace('<s>', '<del>').replace('</s>', '</del>');
53
				return converter.makeMarkdown(html);
54
			},
55
			toHtml: function(data){
56
				//Convert markdown to HTML and remove unsafe things from it.
57
				var unsafe = converter.makeHtml(data);
58
				return DOMPurify.sanitize(unsafe);
0 ignored issues
show
Bug introduced by
The variable DOMPurify seems to be never declared. If this is a global, consider adding a /** global: DOMPurify */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
			},
60
			//Mark this dataprocessor
61
			markdown: true
62
		};
63
64
		//Set the original data
65
		if(editor.markdown_unchangedData) {
66
			editor.setData(editor.markdown_unchangedData);
67
			editor.markdown_unchangedData = null;
68
		}
69
	}
70
71
	CKEDITOR.plugins.add( 'markdown', {
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
72
		//requires: 'entities',
73
74
		onLoad: function() {
75
76
			CKEDITOR.addCss(
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
77
				//Show borders on tables generated by Showdown
78
				'table {\n' +
79
				'     border-width: 1px 0 0 1px;\n' +
80
				'     border-color: #bbb;\n' +
81
				'     border-style: solid;\n' +
82
				' }\n' +
83
				'\n' +
84
				'table td, table th {\n' +
85
				'    border-width: 0 1px 1px 0;\n' +
86
				'    border-color: #bbb;\n' +
87
				'    border-style: solid;\n' +
88
				'    padding: 10px;\n' +
89
				'}' +
90
				//Show code blocks
91
				'pre {\n' +
92
				'    display: block;\n' +
93
				'    padding: 9.5px;\n' +
94
				'    margin: 0 0 10px;\n' +
95
				'    font-size: 13px;\n' +
96
				'    line-height: 1.42857143;\n' +
97
				'    color: #333;\n' +
98
				'    word-break: break-all;\n' +
99
				'    word-wrap: break-word;\n' +
100
				'    background-color: #f5f5f5;\n' +
101
				'    border: 1px solid #ccc;\n' +
102
				'    border-radius: 4px;\n' +
103
				'}' +
104
				'padding: 0;\n' +
105
				'    font-size: inherit;\n' +
106
				'    color: inherit;\n' +
107
				'    white-space: pre-wrap;\n' +
108
				'    background-color: transparent;\n' +
109
				'    border-radius: 0;' +
110
				'code, kbd, pre, samp {\n' +
111
				'    font-family: Menlo, Monaco, Consolas, "Courier New", monospace;\n' +
112
				'}' +
113
				//Show images small
114
				'img {\n' +
115
				'    max-width: 35%;\n' +
116
				'    vertical-align: middle;' +
117
				'}'
118
			);
119
		},
120
121
		beforeInit: function( editor ) {
122
			var config = editor.config;
123
124
			CKEDITOR.tools.extend( config, {
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
125
				basicEntities: false,
126
				entities: false,
127
				fillEmptyBlocks: false
128
			}, true );
129
130
			editor.filter.disable();
131
		},
132
133
		init: function( editor ) {
134
			var config = editor.config;
0 ignored issues
show
Unused Code introduced by
The assignment to variable config seems to be never used. Consider removing it.
Loading history...
135
136
			var rootPath = this.path;
137
			//We override the dataprocessor later (after we loaded the needes scripts), sow we need the save untouched data now.
138
			editor.markdown_unchangedData = editor.getData();
139
140
			if (typeof(showdown) == 'undefined') {
0 ignored issues
show
Bug introduced by
The variable showdown seems to be never declared. If this is a global, consider adding a /** global: showdown */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
141
				CKEDITOR.scriptLoader.load(rootPath + 'js/showdown.min.js', function() {
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
142
					overrideDataProcessor(editor);
143
				}, CKEDITOR, true);
144
			}
145
146
			if (typeof(DOMPurify) == 'undefined') {
0 ignored issues
show
Bug introduced by
The variable DOMPurify seems to be never declared. If this is a global, consider adding a /** global: DOMPurify */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
147
				CKEDITOR.scriptLoader.load(rootPath + 'js/purify.min.js', function() {
148
					overrideDataProcessor()
149
				}, CKEDITOR, true);
150
			}
151
		},
152
153
		afterInit: function( editor ) {
154
			//Override the data processor with our for Markdown.
155
			overrideDataProcessor(editor);
156
		}
157
	} );
158
} )();
159