2019-10-11 19:01:07 +02:00
|
|
|
/**
|
2019-10-12 18:22:07 +02:00
|
|
|
* A markdown output plugin for CKEDITOR.
|
|
|
|
* Uses showdown.js for conversion and DOMPurifier for HTML filtering.
|
|
|
|
*
|
|
|
|
* This software is licensed under MIT License.
|
2019-10-11 19:01:07 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Jan Böhmer
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
function overrideDataProcessor(editor)
|
|
|
|
{
|
2019-10-12 17:41:13 +02:00
|
|
|
//Both showdown and DOMPurify must be loaded
|
2019-10-11 19:01:07 +02:00
|
|
|
if(typeof(showdown) == 'undefined') return;
|
2019-10-12 17:41:13 +02:00
|
|
|
if (typeof(DOMPurify) == 'undefined') return;
|
2019-10-11 19:01:07 +02:00
|
|
|
|
2019-10-12 18:22:07 +02:00
|
|
|
//If the dataprocessor were already be overriden do nothing
|
|
|
|
if(editor.dataProcessor.markdown) return;
|
|
|
|
|
2019-10-11 19:01:07 +02:00
|
|
|
var converter = new showdown.Converter();
|
|
|
|
//Set some useful options on Showdown
|
|
|
|
converter.setFlavor('github');
|
|
|
|
converter.setOption('tables', true);
|
|
|
|
converter.setOption('strikethrough', true);
|
|
|
|
converter.setOption('parseImgDimensions', true);
|
|
|
|
converter.setOption('smartIndentationFix', true);
|
|
|
|
|
|
|
|
editor.dataProcessor = {
|
|
|
|
toDataFormat: function(html, fixForBody) {
|
2019-10-12 18:22:07 +02:00
|
|
|
//html = html.replace(/(\r\n|\n|\r)/gm,"");
|
2019-10-11 19:01:07 +02:00
|
|
|
html = html.replace('<br>', '\n');
|
2019-10-12 18:22:07 +02:00
|
|
|
//Support for strikethrough in markdown
|
2019-10-11 19:01:07 +02:00
|
|
|
html = html.replace('<s>', '<del>').replace('</s>', '</del>');
|
|
|
|
return converter.makeMarkdown(html);
|
|
|
|
},
|
2019-10-12 18:22:07 +02:00
|
|
|
toHtml: function(data){
|
|
|
|
//Convert markdown to HTML and remove unsafe things from it.
|
|
|
|
var unsafe = converter.makeHtml(data);
|
|
|
|
return DOMPurify.sanitize(unsafe);
|
2019-10-11 23:53:12 +02:00
|
|
|
},
|
2019-10-12 18:22:07 +02:00
|
|
|
//Mark this dataprocessor
|
|
|
|
markdown: true
|
2019-10-11 19:01:07 +02:00
|
|
|
};
|
2019-10-12 18:22:07 +02:00
|
|
|
|
|
|
|
//Set the original data
|
|
|
|
if(editor.markdown_unchangedData) {
|
|
|
|
editor.setData(editor.markdown_unchangedData);
|
|
|
|
editor.markdown_unchangedData = null;
|
|
|
|
}
|
2019-10-11 19:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CKEDITOR.plugins.add( 'markdown', {
|
|
|
|
//requires: 'entities',
|
|
|
|
|
|
|
|
onLoad: function() {
|
|
|
|
|
|
|
|
CKEDITOR.addCss(
|
|
|
|
//Show borders on tables generated by Showdown
|
2019-10-11 23:53:12 +02:00
|
|
|
'table {\n' +
|
|
|
|
' border-width: 1px 0 0 1px;\n' +
|
|
|
|
' border-color: #bbb;\n' +
|
|
|
|
' border-style: solid;\n' +
|
|
|
|
' }\n' +
|
|
|
|
'\n' +
|
|
|
|
'table td, table th {\n' +
|
|
|
|
' border-width: 0 1px 1px 0;\n' +
|
|
|
|
' border-color: #bbb;\n' +
|
|
|
|
' border-style: solid;\n' +
|
|
|
|
' padding: 10px;\n' +
|
2019-10-11 19:01:07 +02:00
|
|
|
'}' +
|
|
|
|
//Show code blocks
|
|
|
|
'pre {\n' +
|
|
|
|
' display: block;\n' +
|
|
|
|
' padding: 9.5px;\n' +
|
|
|
|
' margin: 0 0 10px;\n' +
|
|
|
|
' font-size: 13px;\n' +
|
|
|
|
' line-height: 1.42857143;\n' +
|
|
|
|
' color: #333;\n' +
|
|
|
|
' word-break: break-all;\n' +
|
|
|
|
' word-wrap: break-word;\n' +
|
|
|
|
' background-color: #f5f5f5;\n' +
|
|
|
|
' border: 1px solid #ccc;\n' +
|
|
|
|
' border-radius: 4px;\n' +
|
|
|
|
'}' +
|
|
|
|
'padding: 0;\n' +
|
|
|
|
' font-size: inherit;\n' +
|
|
|
|
' color: inherit;\n' +
|
|
|
|
' white-space: pre-wrap;\n' +
|
|
|
|
' background-color: transparent;\n' +
|
|
|
|
' border-radius: 0;' +
|
|
|
|
'code, kbd, pre, samp {\n' +
|
|
|
|
' font-family: Menlo, Monaco, Consolas, "Courier New", monospace;\n' +
|
|
|
|
'}' +
|
|
|
|
//Show images small
|
|
|
|
'img {\n' +
|
|
|
|
' max-width: 35%;\n' +
|
|
|
|
' vertical-align: middle;' +
|
|
|
|
'}'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeInit: function( editor ) {
|
|
|
|
var config = editor.config;
|
|
|
|
|
|
|
|
CKEDITOR.tools.extend( config, {
|
|
|
|
basicEntities: false,
|
|
|
|
entities: false,
|
|
|
|
fillEmptyBlocks: false
|
|
|
|
}, true );
|
|
|
|
|
|
|
|
editor.filter.disable();
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function( editor ) {
|
|
|
|
var config = editor.config;
|
|
|
|
|
|
|
|
var rootPath = this.path;
|
2019-10-12 18:22:07 +02:00
|
|
|
//We override the dataprocessor later (after we loaded the needes scripts), sow we need the save untouched data now.
|
|
|
|
editor.markdown_unchangedData = editor.getData();
|
2019-10-11 19:01:07 +02:00
|
|
|
|
|
|
|
if (typeof(showdown) == 'undefined') {
|
|
|
|
CKEDITOR.scriptLoader.load(rootPath + 'js/showdown.min.js', function() {
|
|
|
|
overrideDataProcessor(editor);
|
2019-10-12 18:22:07 +02:00
|
|
|
}, CKEDITOR, true);
|
2019-10-11 19:01:07 +02:00
|
|
|
}
|
2019-10-12 17:41:13 +02:00
|
|
|
|
|
|
|
if (typeof(DOMPurify) == 'undefined') {
|
|
|
|
CKEDITOR.scriptLoader.load(rootPath + 'js/purify.min.js', function() {
|
2019-10-12 18:22:07 +02:00
|
|
|
overrideDataProcessor()
|
|
|
|
}, CKEDITOR, true);
|
2019-10-12 17:41:13 +02:00
|
|
|
}
|
2019-10-11 19:01:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
afterInit: function( editor ) {
|
|
|
|
//Override the data processor with our for Markdown.
|
|
|
|
overrideDataProcessor(editor);
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} )();
|