function symmetricMarkdownElement(end) { return markdownElement(end, end); } function markdownElement(start, end) { return function() { return {start: start, end: end}; }; } function markdownLink(attributes) { var href = attributes.href || ""; return { start: "[", end: "](" + href + ")", anchorPosition: "before" }; } function markdownImage(attributes) { var src = attributes.src || ""; var altText = attributes.alt || ""; if (src || altText) { return {start: "![" + altText + "](" + src + ")"}; } else { return {}; } } function markdownList(options) { return function(attributes, list) { return { start: list ? "\n" : "", end: list ? "" : "\n", list: { isOrdered: options.isOrdered, indent: list ? list.indent + 1 : 0, count: 0 } }; }; } function markdownListItem(attributes, list) { list = list || {indent: 0, isOrdered: false, count: 0}; list.count++; var start = repeatString("\t", list.indent) + (list.isOrdered ? list.count + "." : "-") + " "; return {start: start, end: "\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"), "br": markdownElement("", " \n"), "ul": markdownList({isOrdered: false}), "ol": markdownList({isOrdered: true}), "li": markdownListItem, "strong": symmetricMarkdownElement("__"), "em": symmetricMarkdownElement("*"), "a": markdownLink, "img": markdownImage, }; (function() { for (var i = 1; i <= 6; i++) { htmlToMarkdown["h" + i] = markdownElement(repeatString("#", i) + " ", "\n\n\n"); } })(); function repeatString(value, count) { return new Array(count + 1).join(value); } function markdownWriter() { var fragments = []; var elementStack = []; var list = null; function open(tagName, attributes) { attributes = attributes || {}; var idHasBeenWritten = false; var createElement = htmlToMarkdown[tagName] || function() { return {}; }; var element = createElement(attributes, list); elementStack.push({end: element.end, list: list}); if (element.list) { list = element.list; } if (element.anchorPosition === "before") { writeAnchor(attributes); idHasBeenWritten = true; } fragments.push(element.start || ""); if (!idHasBeenWritten) { writeAnchor(attributes); } } function writeAnchor(attributes) { if (attributes.id) { fragments.push(''); } } function close(tagName) { var element = elementStack.pop(); list = element.list; fragments.push(element.end || ""); } function selfClosing(tagName, attributes) { open(tagName, attributes); close(tagName); } function text(value) { fragments.push(escapeMarkdown(value)); } function append(html) { fragments.push(html); } 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 { asString: asString, open: open, close: close, text: text, selfClosing: selfClosing, append: append }; } exports.writer = markdownWriter; function escapeMarkdown(value) { return value .replace(/\\/g, '\\\\') .replace(/([\`\*_\{\}\[\]\(\)\#\+\-\.\!])/g, '\\$1'); }