diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-02-07 23:25:06 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-02-07 23:25:06 +0000 |
| commit | b6da8bd438fcd3897f9667cf772acf6277f9776f (patch) | |
| tree | c7cad9aac41033613b949d400409488e233ebc4c /lib/writers | |
| parent | 80a3827820d5d49d2bd0210e651f061d5920d863 (diff) | |
Remove regex munging
Diffstat (limited to 'lib/writers')
| -rw-r--r-- | lib/writers/markdown-writer.js | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js index 4d11e00..7ea3ed4 100644 --- a/lib/writers/markdown-writer.js +++ b/lib/writers/markdown-writer.js @@ -1,3 +1,6 @@ +var _ = require("underscore"); + + function symmetricMarkdownElement(end) { return markdownElement(end, end); } @@ -41,20 +44,27 @@ function markdownList(options) { }; } -function markdownListItem(attributes, list) { +function markdownListItem(attributes, list, listItem) { list = list || {indent: 0, isOrdered: false, count: 0}; list.count++; + listItem.hasClosed = false; + var start = repeatString("\t", list.indent) + (list.isOrdered ? list.count + "." : "-") + " "; - return {start: start, end: "\n"}; + return { + start: start, + end: function() { + if (!listItem.hasClosed) { + listItem.hasClosed = true; + return "\n"; + } + } + }; } var htmlToMarkdown = { - // paragraphs elements use three line feeds to close - // this prevents them being caught by collapsing list elements in `asString` - // while still allowing them to be collapsed when multiple empty lines are removed - "p": markdownElement("", "\n\n\n"), + "p": markdownElement("", "\n\n"), "br": markdownElement("", " \n"), "ul": markdownList({isOrdered: false}), "ol": markdownList({isOrdered: true}), @@ -67,7 +77,7 @@ var htmlToMarkdown = { (function() { for (var i = 1; i <= 6; i++) { - htmlToMarkdown["h" + i] = markdownElement(repeatString("#", i) + " ", "\n\n\n"); + htmlToMarkdown["h" + i] = markdownElement(repeatString("#", i) + " ", "\n\n"); } })(); @@ -79,6 +89,7 @@ function markdownWriter() { var fragments = []; var elementStack = []; var list = null; + var listItem = {}; function open(tagName, attributes) { attributes = attributes || {}; @@ -86,7 +97,7 @@ function markdownWriter() { var idHasBeenWritten = false; var createElement = htmlToMarkdown[tagName] || function() { return {}; }; - var element = createElement(attributes, list); + var element = createElement(attributes, list, listItem); elementStack.push({end: element.end, list: list}); if (element.list) { @@ -113,7 +124,8 @@ function markdownWriter() { function close(tagName) { var element = elementStack.pop(); list = element.list; - fragments.push(element.end || ""); + var end = _.isFunction(element.end) ? element.end() : element.end; + fragments.push(end || ""); } function selfClosing(tagName, attributes) { @@ -130,8 +142,7 @@ function markdownWriter() { } function asString() { - // massage the output to collapse mulitple empty lines and empty lines between nested lists - return fragments.join("").replace(/\n{2}(\t*)([2-9]\d*\.|- )/g, "\n$1$2").replace(/\n{3,}/g, "\n\n"); + return fragments.join(""); } return { |
