Stackedit/public/res/extensions/markdownExtra.js

86 lines
3.9 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
],
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"));
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");
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();
2013-07-09 22:57:24 +00:00
var options = {
extensions: markdownExtra.config.extensions
};
if(markdownExtra.config.highlighter == "highlight") {
options.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") {
2013-05-29 19:55:23 +00:00
options.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, options);
};
2013-05-25 00:34:04 +00:00
return markdownExtra;
});