aboutsummaryrefslogtreecommitdiff
path: root/lib/writers/markdown-writer.js
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-02-04 21:56:08 +0000
committerMichael Williamson <mike@zwobble.org>2015-02-04 21:56:08 +0000
commitee35844a13ee4835bde8ba1969f0fd2ec042f23c (patch)
tree746cea62aa9664e613b2446680eb67c8d4ee4889 /lib/writers/markdown-writer.js
parentd656dba638cd0755f8043b60d7ab2c84b9953e30 (diff)
Push list state update into list element mapper
Diffstat (limited to 'lib/writers/markdown-writer.js')
-rw-r--r--lib/writers/markdown-writer.js43
1 files changed, 20 insertions, 23 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js
index 5a6f5e0..6fed677 100644
--- a/lib/writers/markdown-writer.js
+++ b/lib/writers/markdown-writer.js
@@ -27,17 +27,24 @@ function markdownImage(attributes) {
}
}
-function markdownList(attributes, list) {
- return {
- start: list.indent > 0 ? "\n" : "",
- end: list.indent > 0 ? "" : "\n"
+function markdownList(options) {
+ return function(attributes, list) {
+ return {
+ start: list.indent > -1 ? "\n" : "",
+ end: list.indent > -1 ? "" : "\n",
+ list: {
+ isOrdered: options.isOrdered,
+ indent: list.indent + 1,
+ count: 0
+ }
+ };
};
}
function markdownListItem(attributes, list) {
list.count++;
var start = repeatString("\t", Math.max(list.indent, 0)) +
- (list.type === "ul" ? "-" : list.count + ".") + " ";
+ (list.isOrdered ? list.count + "." : "-") + " ";
return {start: start, end: "\n"};
}
@@ -49,8 +56,8 @@ var htmlmarkdown = {
// while still allowing them to be collapsed when multiple empty lines are removed
"p": markdownElement("", "\n\n\n"),
"br": markdownElement("", " \n"),
- "ul": markdownList,
- "ol": markdownList,
+ "ul": markdownList({isOrdered: false}),
+ "ol": markdownList({isOrdered: true}),
"li": markdownListItem,
"strong": markdownElement("**"),
"em": markdownElement("*"),
@@ -73,32 +80,22 @@ function markdownWriter() {
var elementStack = [];
var list = {
indent: -1,
- type: "ul",
+ isOrdered: false,
count: 0
};
- function isList(tagName) {
- return tagName === "ul" || tagName === "ol";
- }
-
function open(tagName, attributes) {
attributes = attributes || {};
- var parentElementList = list;
-
- if (isList(tagName)) {
- list = {
- type: tagName,
- indent: list.indent + 1,
- count: 0
- };
- }
-
var idHasBeenWritten = false;
var createElement = htmlmarkdown[tagName] || function() { return {}; };
var element = createElement(attributes, list);
- elementStack.push({end: element.end, list: parentElementList});
+ elementStack.push({end: element.end, list: list});
+
+ if (element.list) {
+ list = element.list;
+ }
if (element.anchorPosition === "before") {
writeAnchor(attributes);