0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00
ghost/core/admin/assets/js/markdown-actions.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2013-05-26 08:01:00 -05:00
/*global window, document, console, jQuery*/
2013-05-24 05:09:20 -05:00
// Polyfill for Object.create
2013-05-26 08:01:00 -05:00
if (typeof Object.create !== 'function') {
Object.create = function (obj) {
"use strict";
function F() {}
2013-05-24 05:09:20 -05:00
F.prototype = obj;
return new F();
};
}
// # Surrounds given text with Markdown syntax
2013-05-26 08:01:00 -05:00
(function ($, window, document) {
2013-05-24 05:09:20 -05:00
"use strict";
var Markdown = {
init : function (options, elem) {
var self = this;
self.elem = elem;
self.$elem = $(elem);
self.style = (typeof options === 'string') ? options : options.style;
self.options = $.extend({}, $.fn.addMarkdown.options, options);
self.replace();
},
replace: function () {
var text = this.options.target.getSelection(), md;
if (this.options.syntax[this.style]) {
md = this.options.syntax[this.style].replace('$1', text);
this.options.target.replaceSelection(md);
} else {
2013-05-26 08:01:00 -05:00
console.log("Invalid style.");
2013-05-24 05:09:20 -05:00
}
}
};
$.fn.addMarkdown = function (options) {
return this.each(function () {
var markdown = Object.create(Markdown);
markdown.init(options, this);
});
};
$.fn.addMarkdown.options = {
style: null,
target: 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://)",
image: "![$1](http://)",
blockquote: "> $1",
currentDate: new Date().toLocaleString()
}
};
2013-05-26 08:01:00 -05:00
}(jQuery, window, document));