Sha256: 1ef7018ceabf1d35f94682837a05ff1e1eff57b67d244f1214a1af0006441dc9

Contents?: true

Size: 1.98 KB

Versions: 9

Compression:

Stored size: 1.98 KB

Contents

/*global
  marked
*/

(function() {
  
  marked.setOptions({
    gfm: true,
    pedantic: false,
    sanitize: false
  });
  
  function includeForMarkdown(element) {
    // Include text
    if(element.nodeType === 3) {
      return true;
    } else if(element.nodeType === 1 && element.tagName.match(/^(b|big|i|small|tt|abbr|acronym|cite|code|dfn|em|kbd|strong|samp|var|a|bdo|br|img|map|object|q|script|span|sub|sup|button|input|label|select|textarea)$/i)) {
      return true;
    } else {
      return false;
    }
  }

  function mergeTextAndInlineNodes(nodes) {
    var newArray = [];
    $(nodes).each(function(index, element) {
      var last = newArray.length-1;
      if(includeForMarkdown(element)) {
        var data = (element.nodeType === 3) ? element.data : $("<div></div>").append(element).html();

        if(typeof(newArray[last]) === 'string') {
          newArray[last] = newArray[last] + data;
        } else {
          newArray.push(data);
        }
      } else {
        newArray.push(element);
      }
    });
    return newArray;
  }

  function unwrapImagesFromParagraphs(container) {
    container.find("p").each(function() {
      if($(this).children().length === $(this).find('img').length) {
        $(this).children().unwrap();
      }
    });
    
    return container;
  }

  function toHtml(markdown) {

    var elements = [];

    $.each(mergeTextAndInlineNodes($("<div></div>").append(markdown).contents()), function(index, value) {
      if(typeof(value) === "string" && value.match(/^\s+$/)) {
        elements.push(value);
      } else if(typeof(value) === "string") {
        var html = marked(value);
        if(html.startsWith("<")) {
            elements.push("\n\n");
        }
        elements.push(html);
        if(html.endsWith(">\n")) {
            elements.push("\n");
        }
      } else {
        elements.push(value);
      }
    });
    
    return unwrapImagesFromParagraphs($("<div></div>").append(elements)).html();
  }
  
  window.markdown2Html = toHtml;
  
}());

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
burp_cms-1.7.1 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.7.0 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.6.0 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.5.13 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.5.12 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.5.11 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.5.10 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.5.9 app/assets/packages/burp/editing/js/markdown-fix.js
burp_cms-1.5.8 app/assets/packages/burp/editing/js/markdown-fix.js