Stackedit/public/res/extensions/markdownExtra.js

110 lines
5.2 KiB
JavaScript
Raw Normal View History

2013-11-07 23:10:38 +00:00
/*globals Markdown */
2013-05-27 19:45:33 +00:00
define([
2013-07-09 22:57:24 +00:00
"jquery",
2013-07-28 17:14:42 +00:00
"underscore",
2013-05-27 19:45:33 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
"text!html/markdownExtraSettingsBlock.html",
2013-11-07 23:10:38 +00:00
'google-code-prettify',
'highlightjs',
2013-09-15 14:14:42 +00:00
'pagedown-extra',
2013-11-09 00:09:42 +00:00
], function($, _, utils, Extension, markdownExtraSettingsBlockHTML, prettify, hljs) {
2013-05-29 19:55:23 +00:00
2013-06-22 23:48:57 +00:00
var markdownExtra = new Extension("markdownExtra", "Markdown Extra", true);
markdownExtra.settingsBlock = markdownExtraSettingsBlockHTML;
markdownExtra.defaultConfig = {
2013-07-09 22:57:24 +00:00
extensions: [
"fenced_code_gfm",
"tables",
"def_list",
"attr_list",
2013-10-05 01:30:54 +00:00
"footnotes",
"smartypants",
"strikethrough",
"newlines",
2013-07-09 22:57:24 +00:00
],
intraword: true,
2014-01-21 23:48:21 +00:00
comments: true,
2013-07-09 22:57:24 +00:00
highlighter: "prettify"
2013-05-25 18:13:59 +00:00
};
2013-05-29 19:55:23 +00:00
2013-05-25 18:13:59 +00:00
markdownExtra.onLoadSettings = function() {
2013-07-09 22:57:24 +00:00
function hasExtension(extensionName) {
return _.some(markdownExtra.config.extensions, function(extension) {
return extension == extensionName;
});
}
utils.setInputChecked("#input-markdownextra-fencedcodegfm", hasExtension("fenced_code_gfm"));
utils.setInputChecked("#input-markdownextra-tables", hasExtension("tables"));
utils.setInputChecked("#input-markdownextra-deflist", hasExtension("def_list"));
utils.setInputChecked("#input-markdownextra-attrlist", hasExtension("attr_list"));
utils.setInputChecked("#input-markdownextra-footnotes", hasExtension("footnotes"));
2013-10-05 18:18:38 +00:00
utils.setInputChecked("#input-markdownextra-smartypants", hasExtension("smartypants"));
utils.setInputChecked("#input-markdownextra-strikethrough", hasExtension("strikethrough"));
utils.setInputChecked("#input-markdownextra-newlines", hasExtension("newlines"));
utils.setInputChecked("#input-markdownextra-intraword", markdownExtra.config.intraword);
2014-01-21 23:48:21 +00:00
utils.setInputChecked("#input-markdownextra-comments", markdownExtra.config.comments);
2013-07-09 22:57:24 +00:00
utils.setInputValue("#input-markdownextra-highlighter", markdownExtra.config.highlighter);
2013-05-25 18:13:59 +00:00
};
2013-05-29 19:55:23 +00:00
2013-11-07 23:10:38 +00:00
markdownExtra.onSaveSettings = function(newConfig) {
2013-07-09 22:57:24 +00:00
newConfig.extensions = [];
utils.getInputChecked("#input-markdownextra-fencedcodegfm") && newConfig.extensions.push("fenced_code_gfm");
utils.getInputChecked("#input-markdownextra-tables") && newConfig.extensions.push("tables");
utils.getInputChecked("#input-markdownextra-deflist") && newConfig.extensions.push("def_list");
utils.getInputChecked("#input-markdownextra-attrlist") && newConfig.extensions.push("attr_list");
utils.getInputChecked("#input-markdownextra-footnotes") && newConfig.extensions.push("footnotes");
2013-10-05 18:18:38 +00:00
utils.getInputChecked("#input-markdownextra-smartypants") && newConfig.extensions.push("smartypants");
utils.getInputChecked("#input-markdownextra-strikethrough") && newConfig.extensions.push("strikethrough");
utils.getInputChecked("#input-markdownextra-newlines") && newConfig.extensions.push("newlines");
newConfig.intraword = utils.getInputChecked("#input-markdownextra-intraword");
2014-01-21 23:48:21 +00:00
newConfig.comments = utils.getInputChecked("#input-markdownextra-comments");
2013-07-09 22:57:24 +00:00
newConfig.highlighter = utils.getInputValue("#input-markdownextra-highlighter");
2013-05-25 00:34:04 +00:00
};
2013-05-29 19:55:23 +00:00
2013-11-07 23:10:38 +00:00
var eventMgr;
markdownExtra.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
};
2013-09-09 23:32:24 +00:00
markdownExtra.onPagedownConfigure = function(editor) {
2013-05-29 19:55:23 +00:00
var converter = editor.getConverter();
if(markdownExtra.config.intraword === true) {
var converterOptions = {
_DoItalicsAndBold: function(text) {
text = text.replace(/([^\w*]|^)(\*\*|__)(?=\S)(.+?[*_]*)(?=\S)\2(?=[^\w*]|$)/g, "$1<strong>$3</strong>");
text = text.replace(/([^\w*]|^)(\*|_)(?=\S)(.+?)(?=\S)\2(?=[^\w*]|$)/g, "$1<em>$3</em>");
return text;
}
};
converter.setOptions(converterOptions);
}
2014-01-21 23:48:21 +00:00
if(markdownExtra.config.comments === true) {
converter.hooks.chain("postConversion", function(text) {
return text.replace(/<!--.*?-->/g, function(wholeMatch) {
return wholeMatch.replace(/^<!---(.+?)-?-->$/, ' <span class="comment label label-danger">$1</span> ');
});
});
}
var extraOptions = {
2013-07-09 22:57:24 +00:00
extensions: markdownExtra.config.extensions
};
if(markdownExtra.config.highlighter == "highlight") {
extraOptions.highlighter = "prettify";
2013-07-28 17:14:42 +00:00
var previewContentsElt = document.getElementById('preview-contents');
2013-07-09 22:57:24 +00:00
editor.hooks.chain("onPreviewRefresh", function() {
2013-11-14 08:12:59 +00:00
_.each(previewContentsElt.querySelectorAll('.prettyprint > code'), function(elt) {
2013-07-28 17:14:42 +00:00
hljs.highlightBlock(elt);
2013-07-09 22:57:24 +00:00
});
});
}
else if(markdownExtra.config.highlighter == "prettify") {
extraOptions.highlighter = "prettify";
2013-11-09 00:09:42 +00:00
editor.hooks.chain("onPreviewRefresh", prettify.prettyPrint);
2013-05-29 19:55:23 +00:00
}
Markdown.Extra.init(converter, extraOptions);
2013-05-29 19:55:23 +00:00
};
2013-05-25 00:34:04 +00:00
return markdownExtra;
});