2013-05-24 14:09:20 +04:00
|
|
|
// # Surrounds given text with Markdown syntax
|
2013-06-25 16:23:09 +04:00
|
|
|
|
|
|
|
/*global $, CodeMirror */
|
|
|
|
(function () {
|
2013-05-24 14:09:20 +04:00
|
|
|
"use strict";
|
|
|
|
var Markdown = {
|
|
|
|
init : function (options, elem) {
|
|
|
|
var self = this;
|
|
|
|
self.elem = elem;
|
|
|
|
|
|
|
|
self.style = (typeof options === 'string') ? options : options.style;
|
|
|
|
|
2013-05-28 16:54:55 +04:00
|
|
|
self.options = $.extend({}, CodeMirror.prototype.addMarkdown.options, options);
|
2013-05-24 14:09:20 +04:00
|
|
|
|
|
|
|
self.replace();
|
|
|
|
},
|
|
|
|
replace: function () {
|
2013-05-28 16:54:55 +04:00
|
|
|
var text = this.elem.getSelection(), md;
|
2013-05-24 14:09:20 +04:00
|
|
|
if (this.options.syntax[this.style]) {
|
|
|
|
md = this.options.syntax[this.style].replace('$1', text);
|
2013-05-28 16:54:55 +04:00
|
|
|
this.elem.replaceSelection(md);
|
2013-05-24 14:09:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-28 16:54:55 +04:00
|
|
|
CodeMirror.prototype.addMarkdown = function (options) {
|
|
|
|
var markdown = Object.create(Markdown);
|
|
|
|
markdown.init(options, this);
|
2013-05-24 14:09:20 +04:00
|
|
|
};
|
|
|
|
|
2013-05-28 16:54:55 +04:00
|
|
|
CodeMirror.prototype.addMarkdown.options = {
|
2013-05-24 14:09:20 +04:00
|
|
|
style: null,
|
|
|
|
syntax: {
|
|
|
|
bold: "**$1**",
|
|
|
|
italic: "_$1_",
|
|
|
|
strike: "~~$1~~",
|
|
|
|
code: "`$1`",
|
|
|
|
h1: "\n# $1\n",
|
|
|
|
h2: "\n## $1\n",
|
|
|
|
h3: "\n### $1\n",
|
|
|
|
h4: "\n#### $1\n",
|
|
|
|
h5: "\n##### $1\n",
|
|
|
|
h6: "\n###### $1\n",
|
|
|
|
link: "[$1](http://)",
|
2013-05-28 16:54:55 +04:00
|
|
|
image: "!image[$1](http://)",
|
2013-05-24 14:09:20 +04:00
|
|
|
blockquote: "> $1",
|
|
|
|
currentDate: new Date().toLocaleString()
|
|
|
|
}
|
|
|
|
};
|
2013-06-25 16:23:09 +04:00
|
|
|
|
|
|
|
}());
|