818
Du
dustymarkdown / files
dustymarkdown / node_modules / node-markdown / lib / markdown.js
4.0 KB
1/* node-markdown is based on Showdown parser (see vendor/showdown) */ 2/* usage: html = require("markdown").Markdown(markdown_string); */ 3 4// import Showdown parser 5var Showdown = new (require("./vendor/showdown/src/showdown.js").Showdown.converter)(); 6 7/** 8 * Markdown(text, stripUnwanted, allowedtags, allowedAttribs) -> String 9 * - text (String): Markdown syntax to be parsed 10 * - stripUnwanted (Boolean): if TRUE strip all unwanted tags and attributes 11 * - allowedTags (String): allowed HTML tags in the form of "tag1|tag2|tag3" 12 * - allowedAttributes (Object): allowed attributes for specific tags 13 * format: {"tag1":"attrib1|attrib2|attrib3", "tag2":...} 14 * wildcard for all tags: "*" 15 * 16 * Converts a markdown text into a HTML 17 **/ 18this.Markdown = function(text, stripUnwanted, allowedTags, allowedAttributes){ 19 var md = Showdown.makeHtml(text); 20 if(stripUnwanted) 21 return stripUnwantedHTML(md, allowedTags, allowedAttributes); 22 else 23 return md; 24} 25 26/** 27 * stripUnwantedHTML(html, allowedtags, allowedAttribs) -> String 28 * - html (String): HTML code to be parsed 29 * - allowedTags (String): allowed HTML tags in the form of "tag1|tag2|tag3" 30 * - allowedAttributes (Object): allowed attributes for specific tags 31 * format: {"tag1":"attrib1|attrib2|attrib3", "tag2":...} 32 * wildcard for all tags: "*" 33 * 34 * Removes unwanted tags and attributes from HTML string 35 **/ 36var stripUnwantedHTML = function(html /*, allowedTags, allowedAttributes */){ 37 var allowedTags = arguments[1] || 38 'a|b|blockquote|code|del|dd|dl|dt|em|h1|h2|h3|'+ 39 'i|img|li|ol|p|pre|sup|sub|strong|strike|ul|br|hr', 40 allowedAttributes = arguments[2] || { 41 'img': 'src|width|height|alt', 42 'a': 'href', 43 '*': 'title' 44 } 45 testAllowed = new RegExp('^('+allowedTags.toLowerCase()+')$'), 46 findTags = /<(\/?)\s*([\w:\-]+)([^>]*)>/g, 47 findAttribs = /(\s*)([\w:-]+)\s*=\s*(["'])([^\3]+?)(?:\3)/g; 48 49 // convert all strings patterns into regexp objects 50 for(var i in allowedAttributes){ 51 if(allowedAttributes.hasOwnProperty(i)){ 52 allowedAttributes[i] = new RegExp('^('+ 53 allowedAttributes[i].toLowerCase()+')$'); 54 } 55 } 56 57 // find and match html tags 58 return html.replace(findTags, function(original, lslash, tag, params){ 59 var tagAttr, wildcardAttr, 60 rslash = params.substr(-1)=="/" && "/" || ""; 61 62 tag = tag.toLowerCase(); 63 64 // tag is not allowed, return empty string 65 if(!tag.match(testAllowed)) 66 return ""; 67 68 // tag is allowed 69 else{ 70 // regexp objects for a particular tag 71 tagAttr = tag in allowedAttributes && allowedAttributes[tag]; 72 wildcardAttr = "*" in allowedAttributes && allowedAttributes["*"]; 73 74 // if no attribs are allowed 75 if(!tagAttr && !wildcardAttr) 76 return "<"+lslash+tag+rslash+">"; 77 78 // remove trailing slash if any 79 params = params.trim(); 80 if(rslash){ 81 params = params.substr(0, params.length-1); 82 } 83 84 // find and remove unwanted attributes 85 params = params.replace(findAttribs, function(original, space, 86 name, quot, value){ 87 name = name.toLowerCase(); 88 89 // force javascript: links to # 90 if(name=="href" && value.trim().substr(0, 91 "javascript:".length)=="javascript:"){ 92 value = "#"; 93 } 94 95 if((wildcardAttr && name.match(wildcardAttr)) || 96 (tagAttr && name.match(tagAttr))){ 97 return space+name+"="+quot+value+quot; 98 }else 99 return ""; 100 }); 101 102 return "<"+lslash+tag+(params?" "+params:"")+rslash+">"; 103 } 104 105 }); 106}