Use markdown inside CKEditors.

Markdown offers more features and looks much cleaner than BBCode.
This commit is contained in:
Jan Böhmer 2019-10-11 19:01:07 +02:00
parent ac3e5d2669
commit 46959b74ed
5 changed files with 441 additions and 3 deletions

View file

@ -8,13 +8,13 @@ fos_ck_editor:
default_config: complex_config
configs:
complex_config:
extraPlugins: ["bbcode"]
extraPlugins: "markdown"
toolbar: standard
simple_config:
extraPlugins: "bbcode"
extraPlugins: "markdown"
toolbar: basic
description_config:
extraPlugins: "bbcode"
extraPlugins: "markdown"
height: 60
toolbar: description_toolbar
@ -23,6 +23,9 @@ fos_ck_editor:
bbcode:
path: "ckeditor/plugins/bbcode/" # with trailing slash
filename: "plugin.js"
markdown:
path: "ckeditor/plugins/markdown/"
filename: "plugin.js"
specialchar:
path: "ckeditor/plugins/specialchar"
filename: "plugin.js"

View file

@ -0,0 +1,168 @@
<!DOCTYPE html>
<!--
Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
-->
<html>
<head>
<meta charset="utf-8">
<title>BBCode plugin playground &ndash; CKEditor Sample</title>
<script src="../../../ckeditor.js"></script>
<link href="../../../samples/old/sample.css" rel="stylesheet">
<style>
body {
margin: 0;
}
#editables
{
float:left;
width: 49%
}
#preview
{
width: 50%;
float:right;
}
#console
{
height: 300px;
padding: 5px 10px;
border: solid 3px #555;
overflow: auto;
color: #000;
transition: border-color 0.5s;
font-family: Monaco, monospace;
font-size: 13px;
white-space: normal;
}
#console.updated {
border-color: #0B0;
}
</style>
</head>
<body>
<h1 class="samples">
CKEditor Sample &mdash; BBCode plugin playground
</h1>
<div id="main">
<div class="description">
<p>
This sample shows how to configure CKEditor to output <a href="http://en.wikipedia.org/wiki/BBCode">BBCode</a> format instead of HTML.
Please note that the editor configuration was modified to reflect what is needed in a BBCode editing environment.
Smiley images, for example, were stripped to the emoticons that are commonly used in some BBCode dialects.
</p>
<p>
Please note that currently there is no standard for the BBCode markup language, so its implementation
for different platforms (message boards, blogs etc.) can vary. This means that before using CKEditor to
output BBCode you may need to adjust the implementation to your own environment.
</p>
<p>
A snippet of the configuration code can be seen below; check the source of this page for
a full definition:
</p>
<pre class="samples">
CKEDITOR.inline( 'editor1', {
<strong>extraPlugins : 'bbcode',</strong>
<i>(below configurations details omitted:)</i>
toolbar : ...,
fontSize_sizes : ...,
smiley_images : ...,
smiley_descriptions : ...
});</pre>
</div>
<div id="editables">
<textarea id="editor1">
[img]http://a.cksource.com/f/1/img/logo-ckeditor.gif[/img]
"[b]Little Red Riding Hood[/b]" is a famous [url=http://en.wikipedia.org/wiki/Fairy_tale]fairy tale[/url] about a young girl's encounter with a wolf. The story has been changed considerably in its history and subject to numerous modern adaptations and readings.
[list]
[*]Chinese: [i]小紅帽[/i]
[*]Italian: [i]Cappuccetto Rosso[/i]
[*]Spanish: [i]Caperucita Roja[/i]
[/list]
[quote]It is about a girl called [color=#FF0000]Little Red Riding Hood :)[/color], after the red [url=http://en.wikipedia.org/wiki/Hood_%28headgear%29]hooded[/url] [url=http://en.wikipedia.org/wiki/Cape]cape[/url] or [url=http://en.wikipedia.org/wiki/Cloak]cloak[/url] she wears.
The girl walks through the woods to deliver food to her sick grandmother. A wolf wants to eat the girl but is afraid to do so in public. He approaches the girl, and she naïvely tells him where she is going.
He suggests the girl pick some flowers, which she does. In the meantime, he goes to the grandmother's house and gains entry by pretending to be the girl. He swallows the grandmother whole, and waits for the girl, disguised as the grandmother. When the girl arrives, she notices he looks very strange to be her grandma. In most retellings, this eventually culminates with Little Red Riding Hood saying, "My, what big teeth you have!"
To which the wolf replies, "The better to eat you with," and swallows her whole, too.[/quote]
The above version most widely known today is based on the [url=http://en.wikipedia.org/wiki/Brothers_Grimm]Brothers Grimm[/url] variant.
</textarea>
</div>
<div id="preview">
<h3 for="console">BBCode Output: </h3>
<pre id="console">
</pre>
</div>
</div>
<div id="footer">
<hr>
<p>
CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
</p>
<p id="copy">
Copyright &copy; 2003-2019, <a class="samples" href="https://cksource.com/">CKSource</a> - Frederico
Knabben. All rights reserved.
</p>
</div>
<script>
(function() {
'use strict';
var editor = CKEDITOR.replace( 'editor1', {
extraPlugins: 'bbcode',
// Remove unused plugins.
removePlugins: 'bidi,dialogadvtab,div,filebrowser,flash,format,forms,horizontalrule,iframe,justify,liststyle,pagebreak,showborders,stylescombo,table,tabletools,templates',
// Width and height are not supported in the BBCode format, so object resizing is disabled.
disableObjectResizing: true,
height: 250,
toolbar: [
['Source', '-', 'Save', 'NewPage', '-', 'Undo', 'Redo'],
['Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
['Link', 'Unlink', 'Image'],
'/',
['FontSize', 'Bold', 'Italic', 'Underline'],
['NumberedList', 'BulletedList', '-', 'Blockquote'],
['TextColor', '-', 'Smiley', 'SpecialChar', '-', 'Maximize']
],
// Define font sizes in percent values.
fontSize_sizes: '30/30%;50/50%;100/100%;120/120%;150/150%;200/200%;300/300%',
// Strip CKEditor smileys to those commonly used in BBCode.
smiley_images: [
'regular_smile.png', 'sad_smile.png', 'wink_smile.png', 'teeth_smile.png', 'tongue_smile.png',
'embarrassed_smile.png', 'omg_smile.png', 'whatchutalkingabout_smile.png', 'angel_smile.png',
'shades_smile.png', 'cry_smile.png', 'kiss.png'
],
smiley_descriptions: [
'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
'indecision', 'angel', 'cool', 'crying', 'kiss'
]
} );
function updatePreview() {
var consoleEl = CKEDITOR.document.getById( 'console' );
consoleEl.addClass( 'updated' );
setTimeout( function() { consoleEl.removeClass( 'updated' ); }, 500 );
// IE needs <br>, it doesn't even understand new lines.
consoleEl.setHtml( editor.getData().replace( /\n\r?/g, '<br>' ) );
}
function checkUpdatePreview() {
setTimeout( function() {
if ( editor.checkDirty() ) {
updatePreview();
editor.resetDirty();
}
}, 0 );
}
editor.on( 'instanceReady', updatePreview );
editor.on( 'key', checkUpdatePreview );
editor.on( 'selectionChange', checkUpdatePreview );
})();
</script>
</body>
</html>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,150 @@
/**
* This software is licensed under MIT License
*
* 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() {
var unchangedData = null;
/**
* Remove html tags from given string.
* Taken from here: https://stackoverflow.com/questions/822452/strip-html-from-text-javascript/47140708#47140708
* @param html
* @returns {string | string}
*/
function stripHtml(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
function overrideDataProcessor(editor)
{
if(typeof(showdown) == 'undefined') return;
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) {
html = html.replace(/(\r\n|\n|\r)/gm,"");
html = html.replace('<br>', '\n');
//Support for strikethrough
html = html.replace('<s>', '<del>').replace('</s>', '</del>');
return converter.makeMarkdown(html);
},
toHtml: function(data) {
if(unchangedData) {
data = unchangedData;
unchangedData = null;
}
//Strip html tags from data.
//This is useful, to convert unsupported HTML feauters to plain text and adds an basic XSS protection
//The HTML is inside an iframe so an XSS attack can not do much harm.
data = stripHtml(data);
return converter.makeHtml(data);
}
};
}
CKEDITOR.plugins.add( 'markdown', {
//requires: 'entities',
onLoad: function() {
CKEDITOR.addCss(
//Show borders on tables generated by Showdown
'table, th, td {\n' +
' border: 1px solid black;\n' +
'}' +
//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;
//We override the dataprocessor later (after we loaded t
unchangedData = editor.getData();
if (typeof(showdown) == 'undefined') {
CKEDITOR.scriptLoader.load(rootPath + 'js/showdown.min.js', function() {
overrideDataProcessor(editor);
editor.setData(unchangedData);
});
}
},
afterInit: function( editor ) {
//Override the data processor with our for Markdown.
overrideDataProcessor(editor);
}
} );
} )();

View file

@ -0,0 +1,114 @@
<!DOCTYPE html>
<!--
Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
-->
<html>
<head>
<meta charset="utf-8">
<title>BBCode Plugin &mdash; CKEditor Sample</title>
<script src="../../../ckeditor.js"></script>
<script src="../../../samples/old/sample.js"></script>
<link rel="stylesheet" href="../../../samples/old/sample.css">
<meta name="ckeditor-sample-required-plugins" content="sourcearea">
<meta name="ckeditor-sample-name" content="Output for BBCode">
<meta name="ckeditor-sample-group" content="Additional Plugins">
<meta name="ckeditor-sample-description" content="Configuring CKEditor to produce BBCode tags instead of HTML.">
</head>
<body>
<h1 class="samples">
<a href="../../../samples/old/index.html">CKEditor Samples</a> &raquo; BBCode Plugin
</h1>
<div class="warning deprecated">
This sample is not maintained anymore. Check out its <a href="https://ckeditor.com/docs/ckeditor4/latest/examples/bbcode.html">brand new version in CKEditor Examples</a>.
</div>
<div class="description">
<p>
This sample shows how to configure CKEditor to output <a href="http://en.wikipedia.org/wiki/BBCode">BBCode</a> format instead of HTML.
Please note that the editor configuration was modified to reflect what is needed in a BBCode editing environment.
Smiley images, for example, were stripped to the emoticons that are commonly used in some BBCode dialects.
</p>
<p>
Please note that currently there is no standard for the BBCode markup language, so its implementation
for different platforms (message boards, blogs etc.) can vary. This means that before using CKEditor to
output BBCode you may need to adjust the implementation to your own environment.
</p>
<p>
A snippet of the configuration code can be seen below; check the source of this page for
a full definition:
</p>
<pre class="samples">
CKEDITOR.replace( 'editor1', {
<strong>extraPlugins: 'bbcode',</strong>
toolbar: [
[ 'Source', '-', 'Save', 'NewPage', '-', 'Undo', 'Redo' ],
[ 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat' ],
[ 'Link', 'Unlink', 'Image' ],
'/',
[ 'FontSize', 'Bold', 'Italic', 'Underline' ],
[ 'NumberedList', 'BulletedList', '-', 'Blockquote' ],
[ 'TextColor', '-', 'Smiley', 'SpecialChar', '-', 'Maximize' ]
],
... <i>some other configurations omitted here</i>
}); </pre>
</div>
<form action="../../../samples/sample_posteddata.php" method="post">
<p>
<label for="editor1">
Editor 1:
</label>
<textarea cols="80" id="editor1" name="editor1" rows="10">This is some [b]sample text[/b]. You are using [url=https://ckeditor.com/]CKEditor[/url].</textarea>
<script>
// Replace the <textarea id="editor"> with an CKEditor
// instance, using the "bbcode" plugin, shaping some of the
// editor configuration to fit BBCode environment.
CKEDITOR.replace( 'editor1', {
extraPlugins: 'bbcode',
// Remove unused plugins.
removePlugins: 'bidi,dialogadvtab,div,filebrowser,flash,format,forms,horizontalrule,iframe,justify,liststyle,pagebreak,showborders,stylescombo,table,tabletools,templates',
// Width and height are not supported in the BBCode format, so object resizing is disabled.
disableObjectResizing: true,
// Define font sizes in percent values.
fontSize_sizes: "30/30%;50/50%;100/100%;120/120%;150/150%;200/200%;300/300%",
toolbar: [
[ 'Source', '-', 'Save', 'NewPage', '-', 'Undo', 'Redo' ],
[ 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat' ],
[ 'Link', 'Unlink', 'Image', 'Smiley', 'SpecialChar' ],
'/',
[ 'Bold', 'Italic', 'Underline' ],
[ 'FontSize' ],
[ 'TextColor' ],
[ 'NumberedList', 'BulletedList', '-', 'Blockquote' ],
[ 'Maximize' ]
],
// Strip CKEditor smileys to those commonly used in BBCode.
smiley_images: [
'regular_smile.png', 'sad_smile.png', 'wink_smile.png', 'teeth_smile.png', 'tongue_smile.png',
'embarrassed_smile.png', 'omg_smile.png', 'whatchutalkingabout_smile.png', 'angel_smile.png',
'shades_smile.png', 'cry_smile.png', 'kiss.png'
],
smiley_descriptions: [
'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
'indecision', 'angel', 'cool', 'crying', 'kiss'
]
});
</script>
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
<div id="footer">
<hr>
<p>
CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
</p>
<p id="copy">
Copyright &copy; 2003-2019, <a class="samples" href="https://cksource.com/">CKSource</a> - Frederico
Knabben. All rights reserved.
</p>
</div>
</body>
</html>