diff --git a/controllers/DocumentController.go b/controllers/DocumentController.go index 4f8e7b97..f0ba9b74 100644 --- a/controllers/DocumentController.go +++ b/controllers/DocumentController.go @@ -186,11 +186,13 @@ func (c *DocumentController) Read() { Body string `json:"body"` Title string `json:"title"` DocInfo string `json:"doc_info"` + Version int64 `json:"version"` } data.DocTitle = doc.DocumentName data.Body = doc.Release data.Title = doc.DocumentName + " - Powered by MinDoc" data.DocInfo = docInfo + data.Version = doc.Version c.JsonResult(0, "ok", data) } diff --git a/models/DocumentTree.go b/models/DocumentTree.go index 8af4d089..e3c40c7f 100644 --- a/models/DocumentTree.go +++ b/models/DocumentTree.go @@ -4,10 +4,9 @@ import ( "bytes" "html/template" "math" - "strconv" - "github.com/astaxie/beego/orm" "github.com/lifei6671/mindoc/conf" + "fmt" ) type DocumentTree struct { @@ -116,11 +115,7 @@ func getDocumentTree(array []*DocumentTree, parentId int, selectedId int, select if item.DocumentId == selectedParentId || (item.State != nil && item.State.Opened) { selectedLi = ` class="jstree-open"` } - buf.WriteString("
  • ") - buf.WriteString(template.HTMLEscapeString(item.DocumentName) + "") + buf.WriteString(fmt.Sprintf("\" title=\"%s\"",template.HTMLEscapeString(item.DocumentName))) + buf.WriteString(fmt.Sprintf(" data-version=\"%d\"%s>%s",item.Version,selected,template.HTMLEscapeString(item.DocumentName))) + for _, sub := range array { if p, ok := sub.ParentId.(int); ok && p == item.DocumentId { diff --git a/static/editor.md/editormd.js b/static/editor.md/editormd.js index 7ab3364b..31f12679 100644 --- a/static/editor.md/editormd.js +++ b/static/editor.md/editormd.js @@ -12,10 +12,10 @@ ;(function(factory) { "use strict"; - + // CommonJS/Node.js if (typeof require === "function" && typeof exports === "object" && typeof module === "object") - { + { module.exports = factory; } else if (typeof define === "function") // AMD/CMD/Sea.js @@ -23,60 +23,60 @@ if (define.amd) // for Require.js { /* Require.js define replace */ - } - else + } + else { define(["jquery"], factory); // for Sea.js } - } + } else - { + { window.editormd = factory(); } - -}(function() { + +}(function() { /* Require.js assignment replace */ - + "use strict"; - + var $ = (typeof (jQuery) !== "undefined") ? jQuery : Zepto; if (typeof ($) === "undefined") { return ; } - + /** * editormd - * + * * @param {String} id 编辑器的ID * @param {Object} options 配置选项 Key/Value * @returns {Object} editormd 返回editormd对象 */ - + var editormd = function (id, options) { return new editormd.fn.init(id, options); }; - + editormd.title = editormd.$name = "Editor.md"; editormd.version = "1.5.0"; editormd.homePage = "https://pandao.github.io/editor.md/"; editormd.classPrefix = "editormd-"; - + editormd.toolbarModes = { full : [ - "undo", "redo", "|", - "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", - "h1", "h2", "h3", "h4", "h5", "h6", "|", + "undo", "redo", "|", + "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", + "h1", "h2", "h3", "h4", "h5", "h6", "|", "list-ul", "list-ol", "hr", "|", "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|", "goto-line", "watch", "preview", "fullscreen", "clear", "search", "|", "help", "info" ], simple : [ - "undo", "redo", "|", - "bold", "del", "italic", "quote", "uppercase", "lowercase", "|", - "h1", "h2", "h3", "h4", "h5", "h6", "|", + "undo", "redo", "|", + "bold", "del", "italic", "quote", "uppercase", "lowercase", "|", + "h1", "h2", "h3", "h4", "h5", "h6", "|", "list-ul", "list-ol", "hr", "|", "watch", "preview", "fullscreen", "|", "help", "info" @@ -87,7 +87,7 @@ "help", "info" ] }; - + editormd.defaults = { debug : false, mode : "gfm", //gfm or markdown @@ -133,7 +133,7 @@ fontSize : "13px", saveHTMLToTextarea : false, disabledKeyMaps : [], - + onload : function() {}, onresize : function() {}, onchange : function() {}, @@ -145,20 +145,20 @@ onfullscreenExit : function() {}, onscroll : function() {}, onpreviewscroll : function() {}, - + imageUpload : false, imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"], imageUploadURL : "", crossDomainUpload : false, uploadCallbackURL : "", - + toc : true, // Table of contents tocm : false, // Using [TOCM], auto create ToC dropdown menu tocTitle : "", // for ToC dropdown menu btn tocDropdown : false, tocContainer : "", tocStartLevel : 1, // Said from H1 to create ToC - htmlDecode : false, // Open the HTML tag identification + htmlDecode : false, // Open the HTML tag identification pageBreak : true, // Enable parse page break [========] atLink : true, // for @link emailLink : true, // for email address auto link @@ -171,7 +171,7 @@ sequenceDiagram : false, // sequenceDiagram.js only support IE9+ mermaid : true, previewCodeHighlight : true, - + toolbar : true, // show/hide toolbar toolbarAutoFixed : true, // on window scroll auto fixed position toolbarIcons : "full", @@ -187,7 +187,7 @@ toolbarCustomIcons : { // using html tag create toolbar icon, unused default tag. lowercase : "a", "ucwords" : "Aa" - }, + }, toolbarIconsClass : { undo : "fa-undo", redo : "fa-repeat", @@ -225,9 +225,9 @@ clear : "fa-eraser", help : "fa-question-circle", info : "fa-info-circle" - }, + }, toolbarIconTexts : {}, - + lang : { name : "zh-cn", description : "开源在线Markdown编辑器
    Open source online Markdown editor.", @@ -305,11 +305,11 @@ formatNotAllowed : "错误:只允许上传图片文件,允许上传的图片文件格式有:" }, preformattedText : { - title : "添加预格式文本或代码块", + title : "添加预格式文本或代码块", emptyAlert : "错误:请填写预格式文本或代码的内容。" }, codeBlock : { - title : "添加代码块", + title : "添加代码块", selectLabel : "代码语言:", selectDefaultText : "请选择代码语言", otherLanguage : "其他语言", @@ -325,18 +325,18 @@ } } }; - + editormd.classNames = { tex : editormd.classPrefix + "tex" }; editormd.dialogZindex = 99999; - + editormd.$katex = null; editormd.$marked = null; editormd.$CodeMirror = null; editormd.$prettyPrint = null; - + var timer, flowchartTimer; editormd.prototype = editormd.fn = { @@ -346,76 +346,76 @@ preview : false, fullscreen : false }, - + /** * 构造函数/实例初始化 * Constructor / instance initialization - * + * * @param {String} id 编辑器的ID * @param {Object} [options={}] 配置选项 Key/Value * @returns {editormd} 返回editormd的实例对象 */ - + init : function (id, options) { - + options = options || {}; - + if (typeof id === "object") { options = id; } - + var _this = this; - var classPrefix = this.classPrefix = editormd.classPrefix; + var classPrefix = this.classPrefix = editormd.classPrefix; var settings = this.settings = $.extend(true, editormd.defaults, options); - + id = (typeof id === "object") ? settings.id : id; - + var editor = this.editor = $("#" + id); - + this.id = id; this.lang = settings.lang; - + var classNames = this.classNames = { textarea : { html : classPrefix + "html-textarea", markdown : classPrefix + "markdown-textarea" } }; - - settings.pluginPath = (settings.pluginPath === "") ? settings.path + "../plugins/" : settings.pluginPath; - + + settings.pluginPath = (settings.pluginPath === "") ? settings.path + "../plugins/" : settings.pluginPath; + this.state.watching = (settings.watch) ? true : false; - + if ( !editor.hasClass("editormd") ) { editor.addClass("editormd"); } - + editor.css({ width : (typeof settings.width === "number") ? settings.width + "px" : settings.width, height : (typeof settings.height === "number") ? settings.height + "px" : settings.height }); - + if (settings.autoHeight) { editor.css("height", "auto"); } - + var markdownTextarea = this.markdownTextarea = editor.children("textarea"); - + if (markdownTextarea.length < 1) { editor.append(""); markdownTextarea = this.markdownTextarea = editor.children("textarea"); } - + markdownTextarea.addClass(classNames.textarea.markdown).attr("placeholder", settings.placeholder); - + if (typeof markdownTextarea.attr("name") === "undefined" || markdownTextarea.attr("name") === "") { markdownTextarea.attr("name", (settings.name !== "") ? settings.name : id + "-markdown-doc"); } - + var appendElements = [ (!settings.readOnly) ? "" : "", ( (settings.saveHTMLToTextarea) ? "" : "" ), @@ -423,90 +423,93 @@ "
    ", "
    " ].join("\n"); - + editor.append(appendElements).addClass(classPrefix + "vertical"); - - if (settings.theme !== "") + + if (settings.theme !== "") { editor.addClass(classPrefix + "theme-" + settings.theme); } - - this.mask = editor.children("." + classPrefix + "mask"); + + this.mask = editor.children("." + classPrefix + "mask"); this.containerMask = editor.children("." + classPrefix + "container-mask"); - + if (settings.markdown !== "") { markdownTextarea.val(settings.markdown); } - + if (settings.appendMarkdown !== "") { markdownTextarea.val(markdownTextarea.val() + settings.appendMarkdown); } - - this.htmlTextarea = editor.children("." + classNames.textarea.html); + + this.htmlTextarea = editor.children("." + classNames.textarea.html); this.preview = editor.children("." + classPrefix + "preview"); this.previewContainer = this.preview.children("." + classPrefix + "preview-container"); - - if (settings.previewTheme !== "") + + if (settings.previewTheme !== "") { this.preview.addClass(classPrefix + "preview-theme-" + settings.previewTheme); } - + if (typeof define === "function" && define.amd) { - if (typeof katex !== "undefined") + if (typeof katex !== "undefined") { editormd.$katex = katex; } - - if (settings.searchReplace && !settings.readOnly) + + if (settings.searchReplace && !settings.readOnly) { editormd.loadCSS(settings.path + "codemirror/addon/dialog/dialog"); editormd.loadCSS(settings.path + "codemirror/addon/search/matchesonscrollbar"); } } - + if ((typeof define === "function" && define.amd) || !settings.autoLoadModules) { if (typeof CodeMirror !== "undefined") { editormd.$CodeMirror = CodeMirror; } - + if (typeof marked !== "undefined") { editormd.$marked = marked; } - + this.setCodeMirror().setToolbar().loadedDisplay(); - } - else + } + else { this.loadQueues(); } return this; }, - + /** * 所需组件加载队列 * Required components loading queue - * + * * @returns {editormd} 返回editormd的实例对象 */ - + loadQueues : function() { - var _this = this; - var settings = this.settings; - var loadPath = settings.path; - + var _this = this; + var settings = this.settings; + var loadPath = settings.path; + var isLoadedDisplay = false; + var loadFlowChartOrSequenceDiagram = function() { - - if (editormd.isIE8) + + if (editormd.isIE8) { _this.loadedDisplay(); - + return ; } + + if (settings.mermaid) { editormd.loadCSS(loadPath + "mermaid/mermaid", function () { editormd.loadScript(loadPath + "mermaid/mermaid.min", function () { @@ -544,7 +547,11 @@ return d.getFullYear(); }]] }; - _this.loadedDisplay(); + + if (!isLoadedDisplay){ + isLoadedDisplay = true; + _this.loadedDisplay(); + } }); }); } @@ -553,7 +560,10 @@ editormd.loadScript(loadPath + "raphael.min", function() { editormd.loadScript(loadPath + "flowchart.min", function() { editormd.loadScript(loadPath + "jquery.flowchart.min", function() { - _this.loadedDisplay(); + if (!isLoadedDisplay){ + isLoadedDisplay = true; + _this.loadedDisplay(); + } }); }); }); @@ -565,7 +575,10 @@ editormd.loadScript(loadPath + "sequence/snap.svg-min", function() { editormd.loadScript(loadPath + "sequence/underscore-min", function() { editormd.loadScript(loadPath + "sequence/sequence-diagram-min", function() { - _this.loadedDisplay(); + if (!isLoadedDisplay){ + isLoadedDisplay = true; + _this.loadedDisplay(); + } }); }); }); @@ -576,37 +589,37 @@ { _this.loadedDisplay(); } - }; + }; editormd.loadCSS(loadPath + "codemirror/codemirror.min"); - + if (settings.searchReplace && !settings.readOnly) { editormd.loadCSS(loadPath + "codemirror/addon/dialog/dialog"); editormd.loadCSS(loadPath + "codemirror/addon/search/matchesonscrollbar"); } - + if (settings.codeFold) { - editormd.loadCSS(loadPath + "codemirror/addon/fold/foldgutter"); + editormd.loadCSS(loadPath + "codemirror/addon/fold/foldgutter"); } - + editormd.loadScript(loadPath + "codemirror/codemirror.min", function() { editormd.$CodeMirror = CodeMirror; - + editormd.loadScript(loadPath + "codemirror/modes.min", function() { - + editormd.loadScript(loadPath + "codemirror/addons.min", function() { - + _this.setCodeMirror(); - - if (settings.mode !== "gfm" && settings.mode !== "markdown") + + if (settings.mode !== "gfm" && settings.mode !== "markdown") { _this.loadedDisplay(); - + return false; } - + _this.setToolbar(); editormd.loadScript(loadPath + "marked", function() { @@ -616,116 +629,116 @@ if(!settings.highlightStyle){ settings.highlightStyle = "github"; } - if (settings.previewCodeHighlight) + if (settings.previewCodeHighlight) { editormd.loadCSS(loadPath + "highlight/styles/" + settings.highlightStyle); editormd.loadScript(loadPath + "highlight/highlight", function() { loadFlowChartOrSequenceDiagram(); }); - } + } else - { + { loadFlowChartOrSequenceDiagram(); } }); - + }); - + }); - + }); return this; }, - + /** * 设置 Editor.md 的整体主题,主要是工具栏 * Setting Editor.md theme - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setTheme : function(theme) { var editor = this.editor; var oldTheme = this.settings.theme; var themePrefix = this.classPrefix + "theme-"; - + editor.removeClass(themePrefix + oldTheme).addClass(themePrefix + theme); - + this.settings.theme = theme; - + return this; }, - + /** * 设置 CodeMirror(编辑区)的主题 * Setting CodeMirror (Editor area) theme - * + * * @returns {editormd} 返回editormd的实例对象 */ - - setEditorTheme : function(theme) { - var settings = this.settings; - settings.editorTheme = theme; - + + setEditorTheme : function(theme) { + var settings = this.settings; + settings.editorTheme = theme; + if (theme !== "default") { editormd.loadCSS(settings.path + "codemirror/theme/" + settings.editorTheme); } - + this.cm.setOption("theme", theme); - + return this; }, - + /** * setEditorTheme() 的别名 * setEditorTheme() alias - * + * * @returns {editormd} 返回editormd的实例对象 */ - - setCodeMirrorTheme : function (theme) { + + setCodeMirrorTheme : function (theme) { this.setEditorTheme(theme); - + return this; }, - + /** * 设置 Editor.md 的主题 * Setting Editor.md theme - * + * * @returns {editormd} 返回editormd的实例对象 */ - - setPreviewTheme : function(theme) { + + setPreviewTheme : function(theme) { var preview = this.preview; var oldTheme = this.settings.previewTheme; var themePrefix = this.classPrefix + "preview-theme-"; - + preview.removeClass(themePrefix + oldTheme).addClass(themePrefix + theme); - + this.settings.previewTheme = theme; - + return this; }, - + /** * 配置和初始化CodeMirror组件 * CodeMirror initialization - * + * * @returns {editormd} 返回editormd的实例对象 */ - - setCodeMirror : function() { + + setCodeMirror : function() { var settings = this.settings; var editor = this.editor; - + if (settings.editorTheme !== "default") { editormd.loadCSS(settings.path + "codemirror/theme/" + settings.editorTheme); } - + var codeMirrorConfig = { mode : settings.mode, theme : settings.editorTheme, @@ -738,8 +751,8 @@ lineNumbers : settings.lineNumbers, lineWrapping : settings.lineWrapping, extraKeys : { - "Ctrl-Q": function(cm) { - cm.foldCode(cm.getCursor()); + "Ctrl-Q": function(cm) { + cm.foldCode(cm.getCursor()); } }, foldGutter : settings.codeFold, @@ -752,10 +765,10 @@ showTrailingSpace : settings.showTrailingSpace, highlightSelectionMatches : ( (!settings.matchWordHighlight) ? false : { showToken: (settings.matchWordHighlight === "onselected") ? false : /\w/ } ) }; - + this.codeEditor = this.cm = editormd.$CodeMirror.fromTextArea(this.markdownTextarea[0], codeMirrorConfig); this.codeMirror = this.cmElement = editor.children(".CodeMirror"); - + if (settings.value !== "") { this.cm.setValue(settings.value); @@ -765,13 +778,13 @@ fontSize : settings.fontSize, width : (!settings.watch) ? "100%" : "50%" }); - + if (settings.autoHeight) { this.codeMirror.css("height", "auto"); this.cm.setOption("viewportMargin", Infinity); } - + if (!settings.lineNumbers) { this.codeMirror.find(".CodeMirror-gutters").css("border-right", "none"); @@ -779,149 +792,149 @@ return this; }, - + /** * 获取CodeMirror的配置选项 * Get CodeMirror setting options - * + * * @returns {Mixed} return CodeMirror setting option value */ - - getCodeMirrorOption : function(key) { + + getCodeMirrorOption : function(key) { return this.cm.getOption(key); }, - + /** * 配置和重配置CodeMirror的选项 * CodeMirror setting options / resettings - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setCodeMirrorOption : function(key, value) { - + this.cm.setOption(key, value); - + return this; }, - + /** * 添加 CodeMirror 键盘快捷键 * Add CodeMirror keyboard shortcuts key map - * + * * @returns {editormd} 返回editormd的实例对象 */ - + addKeyMap : function(map, bottom) { this.cm.addKeyMap(map, bottom); - + return this; }, - + /** * 移除 CodeMirror 键盘快捷键 * Remove CodeMirror keyboard shortcuts key map - * + * * @returns {editormd} 返回editormd的实例对象 */ - + removeKeyMap : function(map) { this.cm.removeKeyMap(map); - + return this; }, - + /** * 跳转到指定的行 * Goto CodeMirror line - * + * * @param {String|Intiger} line line number or "first"|"last" * @returns {editormd} 返回editormd的实例对象 */ - + gotoLine : function (line) { - + var settings = this.settings; - + if (!settings.gotoLine) { return this; } - + var cm = this.cm; var editor = this.editor; var count = cm.lineCount(); var preview = this.preview; - + if (typeof line === "string") { if(line === "last") { line = count; } - + if (line === "first") { line = 1; } } - - if (typeof line !== "number") - { + + if (typeof line !== "number") + { alert("Error: The line number must be an integer."); return this; } - + line = parseInt(line) - 1; - + if (line > count) { alert("Error: The line number range 1-" + count); - + return this; } - + cm.setCursor( {line : line, ch : 0} ); - + var scrollInfo = cm.getScrollInfo(); - var clientHeight = scrollInfo.clientHeight; + var clientHeight = scrollInfo.clientHeight; var coords = cm.charCoords({line : line, ch : 0}, "local"); - + cm.scrollTo(null, (coords.top + coords.bottom - clientHeight) / 2); - + if (settings.watch) - { + { var cmScroll = this.codeMirror.find(".CodeMirror-scroll")[0]; - var height = $(cmScroll).height(); - var scrollTop = cmScroll.scrollTop; + var height = $(cmScroll).height(); + var scrollTop = cmScroll.scrollTop; var percent = (scrollTop / cmScroll.scrollHeight); if (scrollTop === 0) { preview.scrollTop(0); - } + } else if (scrollTop + height >= cmScroll.scrollHeight - 16) - { - preview.scrollTop(preview[0].scrollHeight); - } + { + preview.scrollTop(preview[0].scrollHeight); + } else - { + { preview.scrollTop(preview[0].scrollHeight * percent); } } cm.focus(); - + return this; }, - + /** * 扩展当前实例对象,可同时设置多个或者只设置一个 * Extend editormd instance object, can mutil setting. - * + * * @returns {editormd} this(editormd instance object.) */ - + extend : function() { if (typeof arguments[1] !== "undefined") { @@ -932,7 +945,7 @@ this[arguments[0]] = arguments[1]; } - + if (typeof arguments[0] === "object" && typeof arguments[0].length === "undefined") { $.extend(true, this, arguments[0]); @@ -940,168 +953,168 @@ return this; }, - + /** * 设置或扩展当前实例对象,单个设置 * Extend editormd instance object, one by one - * + * * @param {String|Object} key option key * @param {String|Object} value option value * @returns {editormd} this(editormd instance object.) */ - + set : function (key, value) { - + if (typeof value !== "undefined" && typeof value === "function") { value = $.proxy(value, this); } - + this[key] = value; return this; }, - + /** * 重新配置 * Resetting editor options - * + * * @param {String|Object} key option key * @param {String|Object} value option value * @returns {editormd} this(editormd instance object.) */ - + config : function(key, value) { var settings = this.settings; - + if (typeof key === "object") { settings = $.extend(true, settings, key); } - + if (typeof key === "string") { settings[key] = value; } - + this.settings = settings; this.recreate(); - + return this; }, - + /** * 注册事件处理方法 * Bind editor event handle - * + * * @param {String} eventType event type * @param {Function} callback 回调函数 * @returns {editormd} this(editormd instance object.) */ - + on : function(eventType, callback) { var settings = this.settings; - - if (typeof settings["on" + eventType] !== "undefined") - { - settings["on" + eventType] = $.proxy(callback, this); + + if (typeof settings["on" + eventType] !== "undefined") + { + settings["on" + eventType] = $.proxy(callback, this); } return this; }, - + /** * 解除事件处理方法 * Unbind editor event handle - * + * * @param {String} eventType event type * @returns {editormd} this(editormd instance object.) */ - + off : function(eventType) { var settings = this.settings; - - if (typeof settings["on" + eventType] !== "undefined") + + if (typeof settings["on" + eventType] !== "undefined") { settings["on" + eventType] = function(){}; } - + return this; }, - + /** * 显示工具栏 * Display toolbar - * + * * @param {Function} [callback=function(){}] 回调函数 * @returns {editormd} 返回editormd的实例对象 */ - + showToolbar : function(callback) { var settings = this.settings; - + if(settings.readOnly) { return this; } - + if (settings.toolbar && (this.toolbar.length < 1 || this.toolbar.find("." + this.classPrefix + "menu").html() === "") ) { this.setToolbar(); } - - settings.toolbar = true; - + + settings.toolbar = true; + this.toolbar.show(); this.resize(); - + $.proxy(callback || function(){}, this)(); return this; }, - + /** * 隐藏工具栏 * Hide toolbar - * + * * @param {Function} [callback=function(){}] 回调函数 * @returns {editormd} this(editormd instance object.) */ - - hideToolbar : function(callback) { + + hideToolbar : function(callback) { var settings = this.settings; - - settings.toolbar = false; + + settings.toolbar = false; this.toolbar.hide(); this.resize(); - + $.proxy(callback || function(){}, this)(); return this; }, - + /** * 页面滚动时工具栏的固定定位 * Set toolbar in window scroll auto fixed position - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setToolbarAutoFixed : function(fixed) { - + var state = this.state; var editor = this.editor; var toolbar = this.toolbar; var settings = this.settings; - + if (typeof fixed !== "undefined") { settings.toolbarAutoFixed = fixed; } - + var autoFixedHandle = function(){ var $window = $(window); var top = $window.scrollTop(); - + if (!settings.toolbarAutoFixed) { return false; @@ -1124,7 +1137,7 @@ }); } }; - + if (!state.fullscreen && !state.preview && settings.toolbar && settings.toolbarAutoFixed) { $(window).bind("scroll", autoFixedHandle); @@ -1132,58 +1145,58 @@ return this; }, - + /** * 配置和初始化工具栏 * Set toolbar and Initialization - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setToolbar : function() { - var settings = this.settings; - + var settings = this.settings; + if(settings.readOnly) { return this; } - + var editor = this.editor; var preview = this.preview; var classPrefix = this.classPrefix; - + var toolbar = this.toolbar = editor.children("." + classPrefix + "toolbar"); - + if (settings.toolbar && toolbar.length < 1) - { + { var toolbarHTML = "
      "; - + editor.append(toolbarHTML); toolbar = this.toolbar = editor.children("." + classPrefix + "toolbar"); } - - if (!settings.toolbar) + + if (!settings.toolbar) { toolbar.hide(); - + return this; } - + toolbar.show(); - - var icons = (typeof settings.toolbarIcons === "function") ? settings.toolbarIcons() + + var icons = (typeof settings.toolbarIcons === "function") ? settings.toolbarIcons() : ((typeof settings.toolbarIcons === "string") ? editormd.toolbarModes[settings.toolbarIcons] : settings.toolbarIcons); - + var toolbarMenu = toolbar.find("." + this.classPrefix + "menu"), menu = ""; var pullRight = false; - + for (var i = 0, len = icons.length; i < len; i++) { var name = icons[i]; - if (name === "||") - { + if (name === "||") + { pullRight = true; - } + } else if (name === "|") { menu += "
    • |
    • "; @@ -1192,26 +1205,26 @@ { var isHeader = (/h(\d)/.test(name)); var index = name; - + if (name === "watch" && !settings.watch) { index = "unwatch"; } - + var title = settings.lang.toolbar[index]; var iconTexts = settings.toolbarIconTexts[index]; var iconClass = settings.toolbarIconsClass[index]; - + title = (typeof title === "undefined") ? "" : title; iconTexts = (typeof iconTexts === "undefined") ? "" : iconTexts; iconClass = (typeof iconClass === "undefined") ? "" : iconClass; var menuItem = pullRight ? "
    • " : "
    • "; - + if (typeof settings.toolbarCustomIcons[name] !== "undefined" && typeof settings.toolbarCustomIcons[name] !== "function") { menuItem += settings.toolbarCustomIcons[name]; } - else + else { menuItem += ""; menuItem += ""+((isHeader) ? name.toUpperCase() : ( (iconClass === "") ? iconTexts : "") ) + ""; @@ -1225,64 +1238,64 @@ } toolbarMenu.html(menu); - + toolbarMenu.find("[title=\"Lowercase\"]").attr("title", settings.lang.toolbar.lowercase); toolbarMenu.find("[title=\"ucwords\"]").attr("title", settings.lang.toolbar.ucwords); - + this.setToolbarHandler(); this.setToolbarAutoFixed(); return this; }, - + /** * 工具栏图标事件处理对象序列 * Get toolbar icons event handlers - * + * * @param {Object} cm CodeMirror的实例对象 * @param {String} name 要获取的事件处理器名称 * @returns {Object} 返回处理对象序列 */ - + dialogLockScreen : function() { $.proxy(editormd.dialogLockScreen, this)(); - + return this; }, dialogShowMask : function(dialog) { $.proxy(editormd.dialogShowMask, this)(dialog); - + return this; }, - - getToolbarHandles : function(name) { + + getToolbarHandles : function(name) { var toolbarHandlers = this.toolbarHandlers = editormd.toolbarHandlers; - + return (name && typeof toolbarIconHandlers[name] !== "undefined") ? toolbarHandlers[name] : toolbarHandlers; }, - + /** * 工具栏图标事件处理器 * Bind toolbar icons event handle - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setToolbarHandler : function() { var _this = this; var settings = this.settings; - + if (!settings.toolbar || settings.readOnly) { return this; } - + var toolbar = this.toolbar; var cm = this.cm; - var classPrefix = this.classPrefix; - var toolbarIcons = this.toolbarIcons = toolbar.find("." + classPrefix + "menu > li > a"); - var toolbarIconHandlers = this.getToolbarHandles(); - + var classPrefix = this.classPrefix; + var toolbarIcons = this.toolbarIcons = toolbar.find("." + classPrefix + "menu > li > a"); + var toolbarIconHandlers = this.getToolbarHandles(); + toolbarIcons.bind(editormd.mouseOrTouch("click", "touchend"), function(event) { var icon = $(this).children(".fa"); @@ -1293,23 +1306,23 @@ if (name === "") { return ; } - + _this.activeIcon = icon; - if (typeof toolbarIconHandlers[name] !== "undefined") + if (typeof toolbarIconHandlers[name] !== "undefined") { $.proxy(toolbarIconHandlers[name], _this)(cm); } - else + else { - if (typeof settings.toolbarHandlers[name] !== "undefined") + if (typeof settings.toolbarHandlers[name] !== "undefined") { $.proxy(settings.toolbarHandlers[name], _this)(cm, icon, cursor, selection); } } - - if (name !== "link" && name !== "reference-link" && name !== "image" && name !== "code-block" && - name !== "preformatted-text" && name !== "watch" && name !== "preview" && name !== "search" && name !== "fullscreen" && name !== "info") + + if (name !== "link" && name !== "reference-link" && name !== "image" && name !== "code-block" && + name !== "preformatted-text" && name !== "watch" && name !== "preview" && name !== "search" && name !== "fullscreen" && name !== "info") { cm.focus(); } @@ -1320,31 +1333,31 @@ return this; }, - + /** * 动态创建对话框 * Creating custom dialogs - * + * * @param {Object} options 配置项键值对 Key/Value * @returns {dialog} 返回创建的dialog的jQuery实例对象 */ - - createDialog : function(options) { + + createDialog : function(options) { return $.proxy(editormd.createDialog, this)(options); }, - + /** * 创建关于Editor.md的对话框 * Create about Editor.md dialog - * + * * @returns {editormd} 返回editormd的实例对象 */ - + createInfoDialog : function() { var _this = this; var editor = this.editor; - var classPrefix = this.classPrefix; - + var classPrefix = this.classPrefix; + var infoDialogHTML = [ "
      ", "
      ", @@ -1358,30 +1371,30 @@ ].join("\n"); editor.append(infoDialogHTML); - + var infoDialog = this.infoDialog = editor.children("." + classPrefix + "dialog-info"); infoDialog.find("." + classPrefix + "dialog-close").bind(editormd.mouseOrTouch("click", "touchend"), function() { _this.hideInfoDialog(); }); - + infoDialog.css("border", (editormd.isIE8) ? "1px solid #ddd" : "").css("z-index", editormd.dialogZindex).show(); - + this.infoDialogPosition(); return this; }, - + /** * 关于Editor.md对话居中定位 * Editor.md dialog position handle - * + * * @returns {editormd} 返回editormd的实例对象 */ - + infoDialogPosition : function() { var infoDialog = this.infoDialog; - + var _infoDialogPosition = function() { infoDialog.css({ top : ($(window).height() - infoDialog.height()) / 2 + "px", @@ -1392,33 +1405,33 @@ _infoDialogPosition(); $(window).resize(_infoDialogPosition); - + return this; }, - + /** * 显示关于Editor.md * Display about Editor.md dialog - * + * * @returns {editormd} 返回editormd的实例对象 */ - + showInfoDialog : function() { $("html,body").css("overflow-x", "hidden"); - + var _this = this; var editor = this.editor; - var settings = this.settings; + var settings = this.settings; var infoDialog = this.infoDialog = editor.children("." + this.classPrefix + "dialog-info"); - + if (infoDialog.length < 1) { this.createInfoDialog(); } - + this.lockScreen(true); - + this.mask.css({ opacity : settings.dialogMaskOpacity, backgroundColor : settings.dialogMaskBgColor @@ -1430,15 +1443,15 @@ return this; }, - + /** * 隐藏关于Editor.md * Hide about Editor.md dialog - * + * * @returns {editormd} 返回editormd的实例对象 */ - - hideInfoDialog : function() { + + hideInfoDialog : function() { $("html,body").css("overflow-x", ""); this.infoDialog.hide(); this.mask.hide(); @@ -1446,68 +1459,68 @@ return this; }, - + /** * 锁屏 * lock screen - * + * * @param {Boolean} lock Boolean 布尔值,是否锁屏 * @returns {editormd} 返回editormd的实例对象 */ - + lockScreen : function(lock) { editormd.lockScreen(lock); this.resize(); return this; }, - + /** * 编辑器界面重建,用于动态语言包或模块加载等 * Recreate editor - * + * * @returns {editormd} 返回editormd的实例对象 */ - + recreate : function() { var _this = this; var editor = this.editor; var settings = this.settings; - + this.codeMirror.remove(); - + this.setCodeMirror(); - if (!settings.readOnly) + if (!settings.readOnly) { if (editor.find(".editormd-dialog").length > 0) { editor.find(".editormd-dialog").remove(); } - - if (settings.toolbar) - { - this.getToolbarHandles(); + + if (settings.toolbar) + { + this.getToolbarHandles(); this.setToolbar(); } } - + this.loadedDisplay(true); return this; }, - + /** * 高亮预览HTML的pre代码部分 * highlight of preview codes - * + * * @returns {editormd} 返回editormd的实例对象 */ - - previewCodeHighlight : function() { + + previewCodeHighlight : function() { var settings = this.settings; var previewContainer = this.previewContainer; - - if (settings.previewCodeHighlight) + + if (settings.previewCodeHighlight) { // previewContainer.find("pre").addClass("prettyprint"); // @@ -1522,43 +1535,43 @@ return this; }, - + /** * 解析TeX(KaTeX)科学公式 * TeX(KaTeX) Renderer - * + * * @returns {editormd} 返回editormd的实例对象 */ - + katexRender : function() { - + if (timer === null) { return this; } - + this.previewContainer.find("." + editormd.classNames.tex).each(function(){ var tex = $(this); editormd.$katex.render(tex.text(), tex[0]); - + tex.find(".katex").css("font-size", "1.6em"); - }); + }); return this; }, - + /** * 解析和渲染流程图及时序图 * FlowChart and SequenceDiagram Renderer - * + * * @returns {editormd} 返回editormd的实例对象 */ - + flowChartAndSequenceDiagramRender : function() { var $this = this; var settings = this.settings; var previewContainer = this.previewContainer; - + if (editormd.isIE8) { return this; } @@ -1591,13 +1604,13 @@ console.log(e); } } - + var preview = $this.preview; var codeMirror = $this.codeMirror; var codeView = codeMirror.find(".CodeMirror-scroll"); var height = codeView.height(); - var scrollTop = codeView.scrollTop(); + var scrollTop = codeView.scrollTop(); var percent = (scrollTop / codeView[0].scrollHeight); var tocHeight = 0; @@ -1605,43 +1618,43 @@ tocHeight += $(this).height(); }); - var tocMenuHeight = preview.find(".editormd-toc-menu").height(); + var tocMenuHeight = preview.find(".editormd-toc-menu").height(); tocMenuHeight = (!tocMenuHeight) ? 0 : tocMenuHeight; - if (scrollTop === 0) + if (scrollTop === 0) { preview.scrollTop(0); - } + } else if (scrollTop + height >= codeView[0].scrollHeight - 16) - { - preview.scrollTop(preview[0].scrollHeight); - } + { + preview.scrollTop(preview[0].scrollHeight); + } else - { + { preview.scrollTop((preview[0].scrollHeight + tocHeight + tocMenuHeight) * percent); } return this; }, - + /** * 注册键盘快捷键处理 * Register CodeMirror keyMaps (keyboard shortcuts). - * + * * @param {Object} keyMap KeyMap key/value {"(Ctrl/Shift/Alt)-Key" : function(){}} * @returns {editormd} return this */ - + registerKeyMaps : function(keyMap) { - + var _this = this; var cm = this.cm; var settings = this.settings; var toolbarHandlers = editormd.toolbarHandlers; var disabledKeyMaps = settings.disabledKeyMaps; - + keyMap = keyMap || null; - + if (keyMap) { for (var i in keyMap) @@ -1661,7 +1674,7 @@ { var _keyMap = editormd.keyMaps[k]; var handle = (typeof _keyMap === "string") ? $.proxy(toolbarHandlers[_keyMap], _this) : $.proxy(_keyMap, _this); - + if ($.inArray(k, ["F9", "F10", "F11"]) < 0 && $.inArray(k, disabledKeyMaps) < 0) { var _map = {}; @@ -1670,15 +1683,15 @@ cm.addKeyMap(_map); } } - + $(window).keydown(function(event) { - + var keymaps = { "120" : "F9", "121" : "F10", "122" : "F11" }; - + if ( $.inArray(keymaps[event.keyCode], disabledKeyMaps) < 0 ) { switch (event.keyCode) @@ -1687,17 +1700,17 @@ $.proxy(toolbarHandlers["watch"], _this)(); return false; break; - + case 121: $.proxy(toolbarHandlers["preview"], _this)(); return false; break; - + case 122: - $.proxy(toolbarHandlers["fullscreen"], _this)(); + $.proxy(toolbarHandlers["fullscreen"], _this)(); return false; break; - + default: break; } @@ -1707,53 +1720,53 @@ return this; }, - + /** * 绑定同步滚动 - * + * * @returns {editormd} return this */ - + bindScrollEvent : function() { - + var _this = this; var preview = this.preview; var settings = this.settings; var codeMirror = this.codeMirror; var mouseOrTouch = editormd.mouseOrTouch; - + if (!settings.syncScrolling) { return this; } - - var cmBindScroll = function() { + + var cmBindScroll = function() { codeMirror.find(".CodeMirror-scroll").bind(mouseOrTouch("scroll", "touchmove"), function(event) { var height = $(this).height(); - var scrollTop = $(this).scrollTop(); + var scrollTop = $(this).scrollTop(); var percent = (scrollTop / $(this)[0].scrollHeight); - + var tocHeight = 0; - + preview.find(".markdown-toc-list").each(function(){ tocHeight += $(this).height(); }); - + var tocMenuHeight = preview.find(".editormd-toc-menu").height(); tocMenuHeight = (!tocMenuHeight) ? 0 : tocMenuHeight; - if (scrollTop === 0) + if (scrollTop === 0) { preview.scrollTop(0); - } + } else if (scrollTop + height >= $(this)[0].scrollHeight - 16) - { - preview.scrollTop(preview[0].scrollHeight); - } + { + preview.scrollTop(preview[0].scrollHeight); + } else { preview.scrollTop((preview[0].scrollHeight + tocHeight + tocMenuHeight) * percent); } - + $.proxy(settings.onscroll, _this)(event); }); }; @@ -1763,26 +1776,26 @@ }; var previewBindScroll = function() { - + preview.bind(mouseOrTouch("scroll", "touchmove"), function(event) { var height = $(this).height(); - var scrollTop = $(this).scrollTop(); + var scrollTop = $(this).scrollTop(); var percent = (scrollTop / $(this)[0].scrollHeight); var codeView = codeMirror.find(".CodeMirror-scroll"); - if(scrollTop === 0) + if(scrollTop === 0) { codeView.scrollTop(0); } else if (scrollTop + height >= $(this)[0].scrollHeight) { - codeView.scrollTop(codeView[0].scrollHeight); + codeView.scrollTop(codeView[0].scrollHeight); } - else + else { codeView.scrollTop(codeView[0].scrollHeight * percent); } - + $.proxy(settings.onpreviewscroll, _this)(event); }); @@ -1790,7 +1803,7 @@ var previewUnbindScroll = function() { preview.unbind(mouseOrTouch("scroll", "touchmove")); - }; + }; codeMirror.bind({ mouseover : cmBindScroll, @@ -1798,11 +1811,11 @@ touchstart : cmBindScroll, touchend : cmUnbindScroll }); - + if (settings.syncScrolling === "single") { return this; } - + preview.bind({ mouseover : previewBindScroll, mouseout : previewUnbindScroll, @@ -1812,24 +1825,24 @@ return this; }, - + bindChangeEvent : function() { - + var _this = this; var cm = this.cm; var settings = this.settings; - + if (!settings.syncScrolling) { return this; } - + cm.on("change", function(_cm, changeObj) { - + if (settings.watch) { _this.previewContainer.css("padding", settings.autoHeight ? "20px 20px 50px 40px" : "20px"); } - + timer = setTimeout(function() { clearTimeout(timer); _this.save(); @@ -1839,210 +1852,210 @@ return this; }, - + /** * 加载队列完成之后的显示处理 * Display handle of the module queues loaded after. - * + * * @param {Boolean} recreate 是否为重建编辑器 * @returns {editormd} 返回editormd的实例对象 */ - + loadedDisplay : function(recreate) { - + recreate = recreate || false; - + var _this = this; var editor = this.editor; var preview = this.preview; var settings = this.settings; - + this.containerMask.hide(); - + this.save(); - + if (settings.watch) { preview.show(); } - + editor.data("oldWidth", editor.width()).data("oldHeight", editor.height()); // 为了兼容Zepto - + this.resize(); this.registerKeyMaps(); - + $(window).resize(function(){ _this.resize(); }); - + this.bindScrollEvent().bindChangeEvent(); - + if (!recreate) { $.proxy(settings.onload, this)(); } - + this.state.loaded = true; return this; }, - + /** * 设置编辑器的宽度 * Set editor width - * + * * @param {Number|String} width 编辑器宽度值 * @returns {editormd} 返回editormd的实例对象 */ - + width : function(width) { - - this.editor.css("width", (typeof width === "number") ? width + "px" : width); + + this.editor.css("width", (typeof width === "number") ? width + "px" : width); this.resize(); - + return this; }, - + /** * 设置编辑器的高度 * Set editor height - * + * * @param {Number|String} height 编辑器高度值 * @returns {editormd} 返回editormd的实例对象 */ - + height : function(height) { - - this.editor.css("height", (typeof height === "number") ? height + "px" : height); + + this.editor.css("height", (typeof height === "number") ? height + "px" : height); this.resize(); - + return this; }, - + /** * 调整编辑器的尺寸和布局 * Resize editor layout - * + * * @param {Number|String} [width=null] 编辑器宽度值 * @param {Number|String} [height=null] 编辑器高度值 * @returns {editormd} 返回editormd的实例对象 */ - + resize : function(width, height) { - + width = width || null; height = height || null; - + var state = this.state; var editor = this.editor; var preview = this.preview; var toolbar = this.toolbar; var settings = this.settings; var codeMirror = this.codeMirror; - + if (width) { editor.css("width", (typeof width === "number") ? width + "px" : width); } - + if (settings.autoHeight && !state.fullscreen && !state.preview) { editor.css("height", "auto"); codeMirror.css("height", "auto"); - } - else + } + else { - if (height) + if (height) { editor.css("height", (typeof height === "number") ? height + "px" : height); } - + if (state.fullscreen) { editor.height($(window).height()); } - if (settings.toolbar && !settings.readOnly) + if (settings.toolbar && !settings.readOnly) { codeMirror.css("margin-top", toolbar.height() + 1).height(editor.height() - toolbar.height()); - } + } else { codeMirror.css("margin-top", 0).height(editor.height()); } } - - if(settings.watch) + + if(settings.watch) { codeMirror.width(editor.width() / 2); preview.width((!state.preview) ? editor.width() / 2 : editor.width()); - + this.previewContainer.css("padding", settings.autoHeight ? "20px 20px 50px 40px" : "20px"); - - if (settings.toolbar && !settings.readOnly) + + if (settings.toolbar && !settings.readOnly) { preview.css("top", toolbar.height() + 1); - } - else + } + else { preview.css("top", 0); } - + if (settings.autoHeight && !state.fullscreen && !state.preview) { preview.height(""); } else - { + { var previewHeight = (settings.toolbar && !settings.readOnly) ? editor.height() - toolbar.height() : editor.height(); - + preview.height(previewHeight); } - } - else + } + else { codeMirror.width(editor.width()); preview.hide(); } - - if (state.loaded) + + if (state.loaded) { $.proxy(settings.onresize, this)(); } return this; }, - + /** * 解析和保存Markdown代码 * Parse & Saving Markdown source code - * + * * @returns {editormd} 返回editormd的实例对象 */ - + save : function() { - + if (timer === null) { return this; } - + var _this = this; var state = this.state; var settings = this.settings; - var cm = this.cm; + var cm = this.cm; var cmValue = cm.getValue(); var previewContainer = this.previewContainer; - if (settings.mode !== "gfm" && settings.mode !== "markdown") + if (settings.mode !== "gfm" && settings.mode !== "markdown") { this.markdownTextarea.val(cmValue); - + return this; } - + var marked = editormd.$marked; - var markdownToC = this.markdownToC = []; - var rendererOptions = this.markedRendererOptions = { + var markdownToC = this.markdownToC = []; + var rendererOptions = this.markedRendererOptions = { toc : settings.toc, tocm : settings.tocm, tocStartLevel : settings.tocStartLevel, @@ -2057,7 +2070,7 @@ previewCodeHighlight : settings.previewCodeHighlight, mermaid : settings.mermaid, }; - + var markedOptions = this.markedOptions = { renderer : editormd.markedRenderer(markdownToC, rendererOptions), gfm : true, @@ -2068,75 +2081,75 @@ smartLists : true, smartypants : true }; - + marked.setOptions(markedOptions); - + var newMarkdownDoc = editormd.$marked(cmValue, markedOptions); if(settings.debug) { console.info("cmValue", cmValue, newMarkdownDoc); } newMarkdownDoc = editormd.filterHTMLTags(newMarkdownDoc, settings.htmlDecode); - + //console.error("cmValue", cmValue, newMarkdownDoc); - + this.markdownTextarea.text(cmValue); - + cm.save(); - - if (settings.saveHTMLToTextarea) + + if (settings.saveHTMLToTextarea) { this.htmlTextarea.text(newMarkdownDoc); } - + if(settings.watch || (!settings.watch && state.preview)) { previewContainer.html(newMarkdownDoc); this.previewCodeHighlight(); - - if (settings.toc) + + if (settings.toc) { var tocContainer = (settings.tocContainer === "") ? previewContainer : $(settings.tocContainer); var tocMenu = tocContainer.find("." + this.classPrefix + "toc-menu"); - + tocContainer.attr("previewContainer", (settings.tocContainer === "") ? "true" : "false"); - + if (settings.tocContainer !== "" && tocMenu.length > 0) { tocMenu.remove(); } - + editormd.markdownToCRenderer(markdownToC, tocContainer, settings.tocDropdown, settings.tocStartLevel); - + if (settings.tocDropdown || tocContainer.find("." + this.classPrefix + "toc-menu").length > 0) { editormd.tocDropdownMenu(tocContainer, (settings.tocTitle !== "") ? settings.tocTitle : this.lang.tocTitle); } - + if (settings.tocContainer !== "") { previewContainer.find(".markdown-toc").css("border", "none"); } } - + if (settings.tex) { - if (!editormd.kaTeXLoaded && settings.autoLoadModules) + if (!editormd.kaTeXLoaded && settings.autoLoadModules) { editormd.loadKaTeX(function() { editormd.$katex = katex; editormd.kaTeXLoaded = true; _this.katexRender(); }); - } - else + } + else { editormd.$katex = katex; this.katexRender(); } - } - + } + if (settings.flowChart || settings.sequenceDiagram || settings.mermaid) { flowchartTimer = setTimeout(function(){ @@ -2146,7 +2159,7 @@ }, 10); } - if (state.loaded) + if (state.loaded) { $.proxy(settings.onchange, this)(); } @@ -2154,215 +2167,215 @@ return this; }, - + /** * 聚焦光标位置 * Focusing the cursor position - * + * * @returns {editormd} 返回editormd的实例对象 */ - + focus : function() { this.cm.focus(); return this; }, - + /** * 设置光标的位置 * Set cursor position - * + * * @param {Object} cursor 要设置的光标位置键值对象,例:{line:1, ch:0} * @returns {editormd} 返回editormd的实例对象 */ - + setCursor : function(cursor) { this.cm.setCursor(cursor); return this; }, - + /** * 获取当前光标的位置 * Get the current position of the cursor - * + * * @returns {Cursor} 返回一个光标Cursor对象 */ - + getCursor : function() { return this.cm.getCursor(); }, - + /** * 设置光标选中的范围 * Set cursor selected ranges - * + * * @param {Object} from 开始位置的光标键值对象,例:{line:1, ch:0} * @param {Object} to 结束位置的光标键值对象,例:{line:1, ch:0} * @returns {editormd} 返回editormd的实例对象 */ - + setSelection : function(from, to) { - + this.cm.setSelection(from, to); - + return this; }, - + /** * 获取光标选中的文本 * Get the texts from cursor selected - * + * * @returns {String} 返回选中文本的字符串形式 */ - + getSelection : function() { return this.cm.getSelection(); }, - + /** * 设置光标选中的文本范围 * Set the cursor selection ranges - * + * * @param {Array} ranges cursor selection ranges array * @returns {Array} return this */ - + setSelections : function(ranges) { this.cm.setSelections(ranges); - + return this; }, - + /** * 获取光标选中的文本范围 * Get the cursor selection ranges - * + * * @returns {Array} return selection ranges array */ - + getSelections : function() { return this.cm.getSelections(); }, - + /** * 替换当前光标选中的文本或在当前光标处插入新字符 * Replace the text at the current cursor selected or insert a new character at the current cursor position - * + * * @param {String} value 要插入的字符值 * @returns {editormd} 返回editormd的实例对象 */ - + replaceSelection : function(value) { this.cm.replaceSelection(value); return this; }, - + /** * 在当前光标处插入新字符 * Insert a new character at the current cursor position * * 同replaceSelection()方法 * With the replaceSelection() method - * + * * @param {String} value 要插入的字符值 * @returns {editormd} 返回editormd的实例对象 */ - + insertValue : function(value) { this.replaceSelection(value); return this; }, - + /** * 追加markdown * append Markdown to editor - * + * * @param {String} md 要追加的markdown源文档 * @returns {editormd} 返回editormd的实例对象 */ - + appendMarkdown : function(md) { var settings = this.settings; var cm = this.cm; - + cm.setValue(cm.getValue() + md); - + return this; }, - + /** * 设置和传入编辑器的markdown源文档 * Set Markdown source document - * + * * @param {String} md 要传入的markdown源文档 * @returns {editormd} 返回editormd的实例对象 */ - + setMarkdown : function(md) { this.cm.setValue(md || this.settings.markdown); - + return this; }, - + /** * 获取编辑器的markdown源文档 * Set Editor.md markdown/CodeMirror value - * + * * @returns {editormd} 返回editormd的实例对象 */ - + getMarkdown : function() { return this.cm.getValue(); }, - + /** * 获取编辑器的源文档 * Get CodeMirror value - * + * * @returns {editormd} 返回editormd的实例对象 */ - + getValue : function() { return this.cm.getValue(); }, - + /** * 设置编辑器的源文档 * Set CodeMirror value - * + * * @param {String} value set code/value/string/text * @returns {editormd} 返回editormd的实例对象 */ - + setValue : function(value) { this.cm.setValue(value); - + return this; }, - + /** * 清空编辑器 * Empty CodeMirror editor container - * + * * @returns {editormd} 返回editormd的实例对象 */ - + clear : function() { this.cm.setValue(""); - + return this; }, - + /** * 获取解析后存放在Textarea的HTML源码 * Get parsed html code from Textarea - * + * * @returns {String} 返回HTML源码 */ - + getHTML : function() { if (!this.settings.saveHTMLToTextarea) { @@ -2370,28 +2383,28 @@ return false; } - + return this.htmlTextarea.val(); }, - + /** * getHTML()的别名 * getHTML (alias) - * + * * @returns {String} Return html code 返回HTML源码 */ - + getTextareaSavedHTML : function() { return this.getHTML(); }, - + /** * 获取预览窗口的HTML源码 * Get html from preview container - * + * * @returns {editormd} 返回editormd的实例对象 */ - + getPreviewedHTML : function() { if (!this.settings.watch) { @@ -2399,137 +2412,137 @@ return false; } - + return this.previewContainer.html(); }, - + /** * 开启实时预览 * Enable real-time watching - * + * * @returns {editormd} 返回editormd的实例对象 */ - - watch : function(callback) { + + watch : function(callback) { var settings = this.settings; - + if ($.inArray(settings.mode, ["gfm", "markdown"]) < 0) { return this; } - + this.state.watching = settings.watch = true; this.preview.show(); - + if (this.toolbar) { var watchIcon = settings.toolbarIconsClass.watch; var unWatchIcon = settings.toolbarIconsClass.unwatch; - + var icon = this.toolbar.find(".fa[name=watch]"); icon.parent().attr("title", settings.lang.toolbar.watch); icon.removeClass(unWatchIcon).addClass(watchIcon); } - - this.codeMirror.css("border-right", "1px solid #ddd").width(this.editor.width() / 2); - + + this.codeMirror.css("border-right", "1px solid #ddd").width(this.editor.width() / 2); + timer = 0; - + this.save().resize(); - + if (!settings.onwatch) { settings.onwatch = callback || function() {}; } - + $.proxy(settings.onwatch, this)(); - + return this; }, - + /** * 关闭实时预览 * Disable real-time watching - * + * * @returns {editormd} 返回editormd的实例对象 */ - + unwatch : function(callback) { var settings = this.settings; this.state.watching = settings.watch = false; this.preview.hide(); - - if (this.toolbar) + + if (this.toolbar) { var watchIcon = settings.toolbarIconsClass.watch; var unWatchIcon = settings.toolbarIconsClass.unwatch; - + var icon = this.toolbar.find(".fa[name=watch]"); icon.parent().attr("title", settings.lang.toolbar.unwatch); icon.removeClass(watchIcon).addClass(unWatchIcon); } - + this.codeMirror.css("border-right", "none").width(this.editor.width()); - + this.resize(); - + if (!settings.onunwatch) { settings.onunwatch = callback || function() {}; } - + $.proxy(settings.onunwatch, this)(); - + return this; }, - + /** * 显示编辑器 * Show editor - * + * * @param {Function} [callback=function()] 回调函数 * @returns {editormd} 返回editormd的实例对象 */ - + show : function(callback) { callback = callback || function() {}; - + var _this = this; this.editor.show(0, function() { $.proxy(callback, _this)(); }); - + return this; }, - + /** * 隐藏编辑器 * Hide editor - * + * * @param {Function} [callback=function()] 回调函数 * @returns {editormd} 返回editormd的实例对象 */ - + hide : function(callback) { callback = callback || function() {}; - + var _this = this; this.editor.hide(0, function() { $.proxy(callback, _this)(); }); - + return this; }, - + /** * 隐藏编辑器部分,只预览HTML * Enter preview html state - * + * * @returns {editormd} 返回editormd的实例对象 */ - + previewing : function() { - + var _this = this; var editor = this.editor; var preview = this.preview; @@ -2537,18 +2550,18 @@ var settings = this.settings; var codeMirror = this.codeMirror; var previewContainer = this.previewContainer; - + if ($.inArray(settings.mode, ["gfm", "markdown"]) < 0) { return this; } - + if (settings.toolbar && toolbar) { toolbar.toggle(); toolbar.find(".fa[name=preview]").toggleClass("active"); } - + codeMirror.toggle(); - + var escHandle = function(event) { if (event.shiftKey && event.keyCode === 27) { _this.previewed(); @@ -2562,20 +2575,20 @@ if (this.state.fullscreen) { preview.css("background", "#fff"); } - + editor.find("." + this.classPrefix + "preview-close-btn").show().bind(editormd.mouseOrTouch("click", "touchend"), function(){ _this.previewed(); }); - + if (!settings.watch) { this.save(); - } - else + } + else { previewContainer.css("padding", ""); } - + previewContainer.addClass(this.classPrefix + "preview-active"); preview.show().css({ @@ -2584,30 +2597,30 @@ width : editor.width(), height : (settings.autoHeight && !this.state.fullscreen) ? "auto" : editor.height() }); - + if (this.state.loaded) { $.proxy(settings.onpreviewing, this)(); } $(window).bind("keyup", escHandle); - } - else + } + else { $(window).unbind("keyup", escHandle); this.previewed(); } }, - + /** * 显示编辑器部分,退出只预览HTML * Exit preview html state - * + * * @returns {editormd} 返回editormd的实例对象 */ - + previewed : function() { - + var editor = this.editor; var preview = this.preview; var toolbar = this.toolbar; @@ -2616,25 +2629,25 @@ var previewCloseBtn = editor.find("." + this.classPrefix + "preview-close-btn"); this.state.preview = false; - + this.codeMirror.show(); - + if (settings.toolbar) { toolbar.show(); } - + preview[(settings.watch) ? "show" : "hide"](); - + previewCloseBtn.hide().unbind(editormd.mouseOrTouch("click", "touchend")); - + previewContainer.removeClass(this.classPrefix + "preview-active"); - + if (settings.watch) { previewContainer.css("padding", "20px"); } - - preview.css({ + + preview.css({ background : null, position : "absolute", width : editor.width() / 2, @@ -2646,19 +2659,19 @@ { $.proxy(settings.onpreviewed, this)(); } - + return this; }, - + /** * 编辑器全屏显示 * Fullscreen show - * + * * @returns {editormd} 返回editormd的实例对象 */ - + fullscreen : function() { - + var _this = this; var state = this.state; var editor = this.editor; @@ -2666,13 +2679,13 @@ var toolbar = this.toolbar; var settings = this.settings; var fullscreenClass = this.classPrefix + "fullscreen"; - + if (toolbar) { - toolbar.find(".fa[name=fullscreen]").parent().toggleClass("active"); + toolbar.find(".fa[name=fullscreen]").parent().toggleClass("active"); } - + var escHandle = function(event) { - if (!event.shiftKey && event.keyCode === 27) + if (!event.shiftKey && event.keyCode === 27) { if (state.fullscreen) { @@ -2681,50 +2694,50 @@ } }; - if (!editor.hasClass(fullscreenClass)) + if (!editor.hasClass(fullscreenClass)) { state.fullscreen = true; $("html,body").css("overflow", "hidden"); - + editor.css({ width : $(window).width(), height : $(window).height() }).addClass(fullscreenClass); this.resize(); - + $.proxy(settings.onfullscreen, this)(); $(window).bind("keyup", escHandle); } else - { - $(window).unbind("keyup", escHandle); + { + $(window).unbind("keyup", escHandle); this.fullscreenExit(); } return this; }, - + /** * 编辑器退出全屏显示 * Exit fullscreen state - * + * * @returns {editormd} 返回editormd的实例对象 */ - + fullscreenExit : function() { - + var editor = this.editor; var settings = this.settings; var toolbar = this.toolbar; - var fullscreenClass = this.classPrefix + "fullscreen"; - + var fullscreenClass = this.classPrefix + "fullscreen"; + this.state.fullscreen = false; - + if (toolbar) { - toolbar.find(".fa[name=fullscreen]").parent().removeClass("active"); + toolbar.find(".fa[name=fullscreen]").parent().removeClass("active"); } $("html,body").css("overflow", ""); @@ -2735,43 +2748,43 @@ }).removeClass(fullscreenClass); this.resize(); - + $.proxy(settings.onfullscreenExit, this)(); return this; }, - + /** * 加载并执行插件 * Load and execute the plugin - * + * * @param {String} name plugin name / function name * @param {String} path plugin load path * @returns {editormd} 返回editormd的实例对象 */ - + executePlugin : function(name, path) { - + var _this = this; var cm = this.cm; var settings = this.settings; - + path = settings.pluginPath + path; - - if (typeof define === "function") - { + + if (typeof define === "function") + { if (typeof this[name] === "undefined") { alert("Error: " + name + " plugin is not found, you are not load this plugin."); - + return this; } - + this[name](cm); - + return this; } - + if ($.inArray(path, editormd.loadFiles.plugin) < 0) { editormd.loadPlugin(path, function() { @@ -2783,79 +2796,79 @@ { $.proxy(editormd.loadPlugins[name], this)(cm); } - + return this; }, - + /** * 搜索替换 * Search & replace - * + * * @param {String} command CodeMirror serach commands, "find, fintNext, fintPrev, clearSearch, replace, replaceAll" * @returns {editormd} return this */ - + search : function(command) { var settings = this.settings; - + if (!settings.searchReplace) { alert("Error: settings.searchReplace == false"); return this; } - + if (!settings.readOnly) { this.cm.execCommand(command || "find"); } - + return this; }, - - searchReplace : function() { + + searchReplace : function() { this.search("replace"); - + return this; }, - - searchReplaceAll : function() { + + searchReplaceAll : function() { this.search("replaceAll"); - + return this; } }; - - editormd.fn.init.prototype = editormd.fn; - + + editormd.fn.init.prototype = editormd.fn; + /** * 锁屏 * lock screen when dialog opening - * + * * @returns {void} */ editormd.dialogLockScreen = function() { var settings = this.settings || {dialogLockScreen : true}; - - if (settings.dialogLockScreen) - { + + if (settings.dialogLockScreen) + { $("html,body").css("overflow", "hidden"); this.resize(); } }; - + /** * 显示透明背景层 * Display mask layer when dialog opening - * + * * @param {Object} dialog dialog jQuery object * @returns {void} */ - + editormd.dialogShowMask = function(dialog) { var editor = this.editor; var settings = this.settings || {dialogShowMask : true}; - + dialog.css({ top : ($(window).height() - dialog.height()) / 2 + "px", left : ($(window).width() - dialog.width()) / 2 + "px" @@ -2870,11 +2883,11 @@ undo : function() { this.cm.undo(); }, - + redo : function() { this.cm.redo(); }, - + bold : function() { var cm = this.cm; var cursor = cm.getCursor(); @@ -2886,7 +2899,7 @@ cm.setCursor(cursor.line, cursor.ch + 2); } }, - + del : function() { var cm = this.cm; var cursor = cm.getCursor(); @@ -2930,7 +2943,7 @@ //cm.replaceSelection("> " + selection); //cm.setCursor(cursor.line, (selection === "") ? cursor.ch + 2 : cursor.ch + selection.length + 2); }, - + ucfirst : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2939,7 +2952,7 @@ cm.replaceSelection(editormd.firstUpperCase(selection)); cm.setSelections(selections); }, - + ucwords : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2948,7 +2961,7 @@ cm.replaceSelection(editormd.wordsFirstUpperCase(selection)); cm.setSelections(selections); }, - + uppercase : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2957,13 +2970,13 @@ cm.replaceSelection(selection.toUpperCase()); cm.setSelections(selections); }, - + lowercase : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); var selections = cm.listSelections(); - + cm.replaceSelection(selection.toLowerCase()); cm.setSelections(selections); }, @@ -3075,15 +3088,15 @@ var cursor = cm.getCursor(); var selection = cm.getSelection(); - if (selection === "") + if (selection === "") { cm.replaceSelection("- " + selection); - } - else + } + else { var selectionText = selection.split("\n"); - for (var i = 0, len = selectionText.length; i < len; i++) + for (var i = 0, len = selectionText.length; i < len; i++) { selectionText[i] = (selectionText[i] === "") ? "" : "- " + selectionText[i]; } @@ -3097,7 +3110,7 @@ var cursor = cm.getCursor(); var selection = cm.getSelection(); - if(selection === "") + if(selection === "") { cm.replaceSelection("1. " + selection); } @@ -3105,7 +3118,7 @@ { var selectionText = selection.split("\n"); - for (var i = 0, len = selectionText.length; i < len; i++) + for (var i = 0, len = selectionText.length; i < len; i++) { selectionText[i] = (selectionText[i] === "") ? "" : (i+1) + ". " + selectionText[i]; } @@ -3128,7 +3141,7 @@ alert("settings.tex === false"); return this; } - + var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); @@ -3154,7 +3167,7 @@ alert("settings.pageBreak === false"); return this; } - + var cm = this.cm; var selection = cm.getSelection(); @@ -3164,7 +3177,7 @@ image : function() { this.executePlugin("imageDialog", "image-dialog/image-dialog"); }, - + code : function() { var cm = this.cm; var cursor = cm.getCursor(); @@ -3178,17 +3191,17 @@ }, "code-block" : function() { - this.executePlugin("codeBlockDialog", "code-block-dialog/code-block-dialog"); + this.executePlugin("codeBlockDialog", "code-block-dialog/code-block-dialog"); }, "preformatted-text" : function() { this.executePlugin("preformattedTextDialog", "preformatted-text-dialog/preformatted-text-dialog"); }, - + table : function() { - this.executePlugin("tableDialog", "table-dialog/table-dialog"); + this.executePlugin("tableDialog", "table-dialog/table-dialog"); }, - + datetime : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -3198,20 +3211,20 @@ cm.replaceSelection(datefmt); }, - + emoji : function() { this.executePlugin("emojiDialog", "emoji-dialog/emoji-dialog"); }, - + "html-entities" : function() { this.executePlugin("htmlEntitiesDialog", "html-entities-dialog/html-entities-dialog"); }, - + "goto-line" : function() { this.executePlugin("gotoLineDialog", "goto-line-dialog/goto-line-dialog"); }, - watch : function() { + watch : function() { this[this.settings.watch ? "unwatch" : "watch"](); }, @@ -3226,7 +3239,7 @@ clear : function() { this.clear(); }, - + search : function() { this.search(); }, @@ -3239,7 +3252,7 @@ this.showInfoDialog(); } }; - + editormd.keyMaps = { "Ctrl-1" : "h1", "Ctrl-2" : "h2", @@ -3249,12 +3262,12 @@ "Ctrl-6" : "h6", "Ctrl-B" : "bold", // if this is string == editormd.toolbarHandlers.xxxx "Ctrl-D" : "datetime", - + "Ctrl-E" : function() { // emoji var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + if (!this.settings.emoji) { alert("Error: settings.emoji == false"); @@ -3271,12 +3284,12 @@ "Ctrl-H" : "hr", "Ctrl-I" : "italic", "Ctrl-K" : "code", - + "Ctrl-L" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + var title = (selection === "") ? "" : " \""+selection+"\""; cm.replaceSelection("[" + selection + "]("+title+")"); @@ -3286,12 +3299,12 @@ } }, "Ctrl-U" : "list-ul", - + "Shift-Ctrl-A" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + if (!this.settings.atLink) { alert("Error: settings.atLink == false"); @@ -3304,24 +3317,24 @@ cm.setCursor(cursor.line, cursor.ch + 1); } }, - + "Shift-Ctrl-C" : "code", "Shift-Ctrl-Q" : "quote", "Shift-Ctrl-S" : "del", "Shift-Ctrl-K" : "tex", // KaTeX - + "Shift-Alt-C" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + cm.replaceSelection(["```", selection, "```"].join("\n")); if (selection === "") { cm.setCursor(cursor.line, cursor.ch + 3); - } + } }, - + "Shift-Ctrl-Alt-C" : "code-block", "Shift-Ctrl-H" : "html-entities", "Shift-Alt-H" : "help", @@ -3330,12 +3343,12 @@ "Shift-Alt-U" : "ucwords", "Shift-Ctrl-Alt-U" : "ucfirst", "Shift-Alt-L" : "lowercase", - + "Shift-Ctrl-I" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + var title = (selection === "") ? "" : " \""+selection+"\""; cm.replaceSelection("![" + selection + "]("+title+")"); @@ -3344,7 +3357,7 @@ cm.setCursor(cursor.line, cursor.ch + 4); } }, - + "Shift-Ctrl-Alt-I" : "image", "Shift-Ctrl-L" : "link", "Shift-Ctrl-O" : "list-ol", @@ -3355,59 +3368,59 @@ "F10" : "preview", "F11" : "fullscreen", }; - + /** * 清除字符串两边的空格 * Clear the space of strings both sides. - * + * * @param {String} str string - * @returns {String} trimed string + * @returns {String} trimed string */ - + var trim = function(str) { return (!String.prototype.trim) ? str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "") : str.trim(); }; - + editormd.trim = trim; - + /** * 所有单词首字母大写 * Words first to uppercase - * + * * @param {String} str string * @returns {String} string */ - + var ucwords = function (str) { - return str.toLowerCase().replace(/\b(\w)|\s(\w)/g, function($1) { + return str.toLowerCase().replace(/\b(\w)|\s(\w)/g, function($1) { return $1.toUpperCase(); }); }; - + editormd.ucwords = editormd.wordsFirstUpperCase = ucwords; - + /** * 字符串首字母大写 * Only string first char to uppercase - * + * * @param {String} str string * @returns {String} string */ - - var firstUpperCase = function(str) { + + var firstUpperCase = function(str) { return str.toLowerCase().replace(/\b(\w)/, function($1){ return $1.toUpperCase(); }); }; - + var ucfirst = firstUpperCase; - + editormd.firstUpperCase = editormd.ucfirst = firstUpperCase; - + editormd.urls = { atLinkBase : "https://github.com/" }; - + editormd.regexs = { atLink : /@(\w+)/g, email : /(\w+)@(\w+)\.(\w+)\.?(\w+)?/g, @@ -3435,7 +3448,7 @@ /** * 自定义marked的解析器 * Custom Marked renderer rules - * + * * @param {Array} markdownToC 传入用于接收TOC的数组 * @returns {Renderer} markedRenderer 返回marked的Renderer自定义对象 */ @@ -3444,7 +3457,7 @@ var defaults = { toc : true, // Table of contents tocm : false, - tocStartLevel : 1, // Said from H1 to create ToC + tocStartLevel : 1, // Said from H1 to create ToC pageBreak : true, atLink : true, // for @link emailLink : true, // for mail address auto link @@ -3455,12 +3468,12 @@ sequenceDiagram : false, // sequenceDiagram.js only support IE9+ mermaid : true, }; - - var settings = $.extend(defaults, options || {}); + + var settings = $.extend(defaults, options || {}); var marked = editormd.$marked; var markedRenderer = new marked.Renderer(); - markdownToC = markdownToC || []; - + markdownToC = markdownToC || []; + var regexs = editormd.regexs; var atLinkReg = regexs.atLink; var emojiReg = regexs.emoji; @@ -3543,11 +3556,11 @@ }; markedRenderer.emoji = function(text) { - - text = text.replace(editormd.regexs.emojiDatetime, function($1) { + + text = text.replace(editormd.regexs.emojiDatetime, function($1) { return $1.replace(/:/g, ":"); }); - + var matchs = text.match(emojiReg); if (!matchs || !settings.emoji) { @@ -3555,7 +3568,7 @@ } for (var i = 0, len = matchs.length; i < len; i++) - { + { if (matchs[i] === ":+1:") { matchs[i] = ":\\+1:"; } @@ -3565,11 +3578,11 @@ var name = $1.replace(/:/g, ""); if (faMatchs) - { + { for (var fa = 0, len1 = faMatchs.length; fa < len1; fa++) { var faName = faMatchs[fa].replace(/:/g, ""); - + return ""; } } @@ -3578,15 +3591,15 @@ var emdlogoMathcs = $1.match(editormdLogoReg); var twemojiMatchs = $1.match(twemojiReg); - if (emdlogoMathcs) - { + if (emdlogoMathcs) + { for (var x = 0, len2 = emdlogoMathcs.length; x < len2; x++) { var logoName = emdlogoMathcs[x].replace(/:/g, ""); return ""; } } - else if (twemojiMatchs) + else if (twemojiMatchs) { for (var t = 0, len3 = twemojiMatchs.length; t < len3; t++) { @@ -3612,8 +3625,8 @@ markedRenderer.atLink = function(text) { if (atLinkReg.test(text)) - { - if (settings.atLink) + { + if (settings.atLink) { text = text.replace(emailReg, function($1, $2, $3, $4) { return $1.replace(/@/g, "_#_@_#_"); @@ -3623,7 +3636,7 @@ return "" + $1 + ""; }).replace(/_#_@_#_/g, "@"); } - + if (settings.emailLink) { text = text.replace(emailLinkReg, function($1, $2, $3, $4, $5) { @@ -3636,7 +3649,7 @@ return text; }; - + markedRenderer.link = function (href, title, text) { if (this.options.sanitize) { @@ -3658,14 +3671,14 @@ if(href.indexOf("http://") === 0 || href.indexOf("https://") === 0) { out += "target=\"_blank\""; } - + if (atLinkReg.test(title) || atLinkReg.test(text)) { if (title) { out += " title=\"" + title.replace(/@/g, "@"); } - + return out + "\">" + text.replace(/@/g, "@") + ""; } @@ -3677,14 +3690,14 @@ return out; }; - + markedRenderer.heading = function(text, level, raw) { - + var linkText = text; var hasLinkReg = /\s*\]*)\>(.*)\<\/a\>\s*/; var getLinkTextReg = /\s*\]+)\>([^\>]*)\<\/a\>\s*/g; - if (hasLinkReg.test(text)) + if (hasLinkReg.test(text)) { var tempText = []; text = text.split(/\]+)\>([^\>]*)\<\/a\>/); @@ -3696,23 +3709,23 @@ text = tempText.join(" "); } - + text = trim(text); - + var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-"); var toc = { text : text, level : level, slug : escapedText }; - + var isChinese = /^[\u4e00-\u9fa5]+$/.test(text); var id = (isChinese) ? escape(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-"); markdownToC.push(toc); - + var headingHTML = ""; - + headingHTML += ""; headingHTML += ""; headingHTML += (hasLinkReg) ? this.atLink(this.emoji(linkText)) : this.atLink(this.emoji(text)); @@ -3720,13 +3733,13 @@ return headingHTML; }; - + markedRenderer.pageBreak = function(text) { if (pageBreakReg.test(text) && settings.pageBreak) { text = "
      "; } - + return text; }; @@ -3736,25 +3749,25 @@ var isTeXAddClass = (isTeXLine) ? " class=\"" + editormd.classNames.tex + "\"" : " class=\"line\""; var isToC = (settings.tocm) ? /^(\[TOC\]|\[TOCM\])$/.test(text) : /^\[TOC\]$/.test(text); var isToCMenu = /^\[TOCM\]$/.test(text); - - if (!isTeXLine && isTeXInline) + + if (!isTeXLine && isTeXInline) { text = text.replace(/(\$\$([^\$]*)\$\$)+/g, function($1, $2) { return "" + $2.replace(/\$/g, "") + ""; }); - } - else + } + else { text = (isTeXLine) ? text.replace(/\$/g, "") : text; } - + var tocHTML = "
      " + text + "
      "; - + return (isToC) ? ( (isToCMenu) ? "
      " + tocHTML + "

      " : tocHTML ) : ( (pageBreakReg.test(text)) ? this.pageBreak(text) : "" + this.atLink(this.emoji(text)) + "

      \n" ); }; - markedRenderer.code = function (code, lang, escaped) { + markedRenderer.code = function (code, lang, escaped) { if (lang === "seq" || lang === "sequence") { @@ -3774,12 +3787,12 @@ else if ( lang === "flow") { return "
      " + code + "
      "; - } + } else if ( lang === "math" || lang === "latex" || lang === "katex") { return "

      " + code + "

      "; - } - else + } + else { return marked.Renderer.prototype.code.apply(this, arguments); @@ -3789,19 +3802,19 @@ markedRenderer.tablecell = function(content, flags) { var type = (flags.header) ? "th" : "td"; var tag = (flags.align) ? "<" + type +" style=\"text-align:" + flags.align + "\">" : "<" + type + ">"; - + return tag + this.atLink(this.emoji(content)) + "\n"; }; markedRenderer.listitem = function(text) { - if (settings.taskList && /^\s*\[[x\s]\]\s*/.test(text)) + if (settings.taskList && /^\s*\[[x\s]\]\s*/.test(text)) { text = text.replace(/^\s*\[\s\]\s*/, " ") .replace(/^\s*\[x\]\s*/, " "); return "
    • " + this.atLink(this.emoji(text)) + "
    • "; } - else + else { return "
    • " + this.atLink(this.emoji(text)) + "
    • "; } @@ -3809,47 +3822,47 @@ markedRenderer.checkbox = function(checked){ return checked ? ' ' : ' '; }; - + return markedRenderer; }; - + /** * * 生成TOC(Table of Contents) * Creating ToC (Table of Contents) - * + * * @param {Array} toc 从marked获取的TOC数组列表 * @param {Element} container 插入TOC的容器元素 * @param {Integer} startLevel Hx 起始层级 * @returns {Object} tocContainer 返回ToC列表容器层的jQuery对象元素 */ - + editormd.markdownToCRenderer = function(toc, container, tocDropdown, startLevel) { - - var html = ""; + + var html = ""; var lastLevel = 0; var classPrefix = this.classPrefix; - + startLevel = startLevel || 1; - - for (var i = 0, len = toc.length; i < len; i++) + + for (var i = 0, len = toc.length; i < len; i++) { var text = toc[i].text; var level = toc[i].level; - + if (level < startLevel) { continue; } - - if (level > lastLevel) + + if (level > lastLevel) { html += ""; } - else if (level < lastLevel) + else if (level < lastLevel) { html += (new Array(lastLevel - level + 2)).join(""); - } - else + } + else { html += ""; } @@ -3857,44 +3870,44 @@ html += "
    • " + text + "