diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-02-04 19:15:10 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-02-04 19:15:10 +0000 |
| commit | aa5f0390a119506eb7af750979aa4e98c0749247 (patch) | |
| tree | 14e566c14291ad00b562adb357e21d021f2692e5 /lib | |
| parent | 87b5c312309c517030f2130f3deb24082814a67f (diff) | |
Separate writers for markdown images and links
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/writers/markdown-writer.js | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js index 02bef16..5a75db7 100644 --- a/lib/writers/markdown-writer.js +++ b/lib/writers/markdown-writer.js @@ -19,17 +19,33 @@ function markdownElement(start, end) { }; } -function markdownLink(link, text) { - var attrs = {}; +function markdownLink() { + var href; this.open = function(attributes) { - attrs = attributes || {}; - + href = attributes.href; return "["; }; this.close = function() { - return (attrs[text] || "") + "](" + (attrs[link] || "") + ")"; + return "](" + (href || "") + ")"; + }; + + this.empty = function(attrs) { + // don't display empty content (this is mainly to remove images with no src data) + return this.open(attrs) + this.close(); + }; +} + +function markdownImage() { + this.open = function(attributes) { + var src = attributes.src || ""; + var altText = attributes.alt || ""; + return "[" + altText + "](" + src + ")"; + }; + + this.close = function() { + return ""; }; this.empty = function(attrs) { @@ -96,8 +112,8 @@ var htmlmarkdown = { "li": new markdownListItem(), "strong": new markdownElement("**"), "em": new markdownElement("*"), - "a": new markdownLink("href"), - "img": new markdownLink("src", "alt"), + "a": new markdownLink(), + "img": new markdownImage(), }; function markdownWriter() { @@ -114,9 +130,9 @@ function markdownWriter() { } function open(tagName, attributes) { - if (htmlmarkdown[tagName]) { - attributes = attributes || {}; + attributes = attributes || {}; + if (htmlmarkdown[tagName]) { if (tagName === "li") { attributes.indent = list.indent; attributes.type = list.type; @@ -148,6 +164,8 @@ function markdownWriter() { } function selfClosing(tagName, attributes) { + attributes = attributes || {}; + if (htmlmarkdown[tagName]) { fragments.push(htmlmarkdown[tagName].empty(attributes)); } |
