Permalink
Browse files

initial import

  • Loading branch information...
andris9 committed Sep 2, 2010
0 parents commit 9f7ffa1ab0fe3cb9d93047657c4dd627fae4a740
38 LICENSE
@@ -0,0 +1,38 @@
+Copyright (c) 2010, Andris Reinman
+<http://www.andrisreinman.com/>
+All rights reserved.
+
+Original Showdown copyright (c) 2007, John Fraser
+<http://www.attacklab.net/>
+All rights reserved.
+
+Original Markdown copyright (c) 2004, John Gruber
+<http://daringfireball.net/>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+* Neither the name "Markdown" nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+This software is provided by the copyright holders and contributors "as
+is" and any express or implied warranties, including, but not limited
+to, the implied warranties of merchantability and fitness for a
+particular purpose are disclaimed. In no event shall the copyright owner
+or contributors be liable for any direct, indirect, incidental, special,
+exemplary, or consequential damages (including, but not limited to,
+procurement of substitute goods or services; loss of use, data, or
+profits; or business interruption) however caused and on any theory of
+liability, whether in contract, strict liability, or tort (including
+negligence or otherwise) arising in any way out of the use of this
+software, even if advised of the possibility of such damage.
@@ -0,0 +1,25 @@
+node-markdown
+=============
+
+node-markdown is based on [Showdown parser](http://attacklab.net/showdown/) and meant to parse [Markdown](http://daringfireball.net/projects/markdown/) syntax into HTML code.
+
+Usage
+-----
+
+ var md_text = "**bold** *italic* [link](http://www.neti.ee) `code block`",
+ md_parser = require("../lib/markdown.js").Markdown;
+
+ // simple
+ console.log(md_parser(md_text));
+
+ // limit HTML tags and attributes
+ console.log(md_parser(md_text, true, 'h1|p|span'));
+
+ // limit HTML tags and keep attributes for allowed tags
+ var allowedTags = 'a|img';
+ allowedAttributes = {
+ 'a':'href|style',
+ 'img': 'src',
+ '*': 'title'
+ }
+ console.log(md_parser(md_text, true, allowedTags, allowedAttributes));
@@ -0,0 +1,12 @@
+var md = require("../lib/markdown.js").Markdown;
+
+var md_text = "**bold** *italic* [link](http://www.neti.ee) `code block`";
+
+console.log("--all");
+console.log(md(md_text));
+
+console.log("--only <strong> and <code>")
+console.log(md(md_text, true, 'strong|code'));
+
+console.log("--only <a> with _href_")
+console.log(md(md_text, true, 'a', {a:'href'}));
@@ -0,0 +1,106 @@
+/* node-markdown is based on Showdown parser (see vendor/showdown) */
+/* usage: html = require("markdown").Markdown(markdown_string); */
+
+// import Showdown parser
+var Showdown = new (require("./vendor/showdown/src/showdown.js").Showdown.converter)();
+
+/**
+ * Markdown(text, stripUnwanted, allowedtags, allowedAttribs) -> String
+ * - text (String): Markdown syntax to be parsed
+ * - stripUnwanted (Boolean): if TRUE strip all unwanted tags and attributes
+ * - allowedTags (String): allowed HTML tags in the form of "tag1|tag2|tag3"
+ * - allowedAttributes (Object): allowed attributes for specific tags
+ * format: {"tag1":"attrib1|attrib2|attrib3", "tag2":...}
+ * wildcard for all tags: "*"
+ *
+ * Converts a markdown text into a HTML
+ **/
+this.Markdown = function(text, stripUnwanted, allowedTags, allowedAttributes){
+ var md = Showdown.makeHtml(text);
+ if(stripUnwanted)
+ return stripUnwantedHTML(md, allowedTags, allowedAttributes);
+ else
+ return md;
+}
+
+/**
+ * stripUnwantedHTML(html, allowedtags, allowedAttribs) -> String
+ * - html (String): HTML code to be parsed
+ * - allowedTags (String): allowed HTML tags in the form of "tag1|tag2|tag3"
+ * - allowedAttributes (Object): allowed attributes for specific tags
+ * format: {"tag1":"attrib1|attrib2|attrib3", "tag2":...}
+ * wildcard for all tags: "*"
+ *
+ * Removes unwanted tags and attributes from HTML string
+ **/
+var stripUnwantedHTML = function(html /*, allowedTags, allowedAttributes */){
+ var allowedTags = arguments[1] ||
+ 'a|b|blockquote|code|del|dd|dl|dt|em|h1|h2|h3|'+
+ 'i|img|li|ol|p|pre|sup|sub|strong|strike|ul|br|hr',
+ allowedAttributes = arguments[2] || {
+ 'img': 'src|width|height|alt',
+ 'a': 'href',
+ '*': 'title'
+ }
+ testAllowed = new RegExp('^('+allowedTags.toLowerCase()+')$'),
+ findTags = /<(\/?)\s*([\w:\-]+)([^>]*)>/g,
+ findAttribs = /(\s*)([\w:-]+)\s*=\s*(["'])([^\3]+?)(?:\3)/g;
+
+ // convert all strings patterns into regexp objects
+ for(var i in allowedAttributes){
+ if(allowedAttributes.hasOwnProperty(i)){
+ allowedAttributes[i] = new RegExp('^('+
+ allowedAttributes[i].toLowerCase()+')$');
+ }
+ }
+
+ // find and match html tags
+ return html.replace(findTags, function(original, lslash, tag, params){
+ var tagAttr, wildcardAttr,
+ rslash = params.substr(-1)=="/" && "/" || "";
+
+ tag = tag.toLowerCase();
+
+ // tag is not allowed, return empty string
+ if(!tag.match(testAllowed))
+ return "";
+
+ // tag is allowed
+ else{
+ // regexp objects for a particular tag
+ tagAttr = tag in allowedAttributes && allowedAttributes[tag];
+ wildcardAttr = "*" in allowedAttributes && allowedAttributes["*"];
+
+ // if no attribs are allowed
+ if(!tagAttr && !wildcardAttr)
+ return "<"+lslash+tag+rslash+">";
+
+ // remove trailing slash if any
+ params = params.trim();
+ if(rslash){
+ params = params.substr(0, params.length-1);
+ }
+
+ // find and remove unwanted attributes
+ params = params.replace(findAttribs, function(original, space,
+ name, quot, value){
+ name = name.toLowerCase();
+
+ // force javascript: links to #
+ if(name=="href" && value.trim().substr(0,
+ "javascript:".length)=="javascript:"){
+ value = "#";
+ }
+
+ if((wildcardAttr && name.match(wildcardAttr)) ||
+ (tagAttr && name.match(tagAttr))){
+ return space+name+"="+quot+value+quot;
+ }else
+ return "";
+ });
+
+ return "<"+lslash+tag+(params?" "+params:"")+rslash+">";
+ }
+
+ });
+}
Oops, something went wrong.

0 comments on commit 9f7ffa1

Please sign in to comment.