From 3ae9d91d86d4ab23ee49e44c1c1b3ba505f4309a Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 22 Dec 2017 21:34:55 +0530 Subject: [PATCH] UX: Use tight list by default in HTML to Markdown conversion --- .../discourse/lib/to-markdown.js.es6 | 26 +++++++++++++++---- test/javascripts/lib/to-markdown-test.js.es6 | 9 +++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 index 2f236392fe9..918d4dac707 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -35,7 +35,7 @@ class Tag { static blocks() { return ["address", "article", "aside", "dd", "div", "dl", "dt", "fieldset", "figcaption", "figure", - "footer", "form", "header", "hgroup", "hr", "main", "nav", "p", "pre", "section", "ul"]; + "footer", "form", "header", "hgroup", "hr", "main", "nav", "p", "pre", "section"]; } static headings() { @@ -51,17 +51,18 @@ class Tag { } static trimmable() { - return [...Tag.blocks(), ...Tag.headings(), ...Tag.slices(), "li", "td", "th", "br", "hr", "blockquote", "table", "ol", "tr"]; + return [...Tag.blocks(), ...Tag.headings(), ...Tag.slices(), "li", "td", "th", "br", "hr", "blockquote", "table", "ol", "tr", "ul"]; } static block(name, prefix, suffix) { return class extends Tag { constructor() { super(name, prefix, suffix); + this.gap = "\n\n"; } decorate(text) { - return `\n\n${this.prefix}${text}${this.suffix}\n\n`; + return `${this.gap}${this.prefix}${text}${this.suffix}${this.gap}`; } }; } @@ -259,8 +260,23 @@ class Tag { }; } + static list(name) { + return class extends Tag.block(name) { + decorate(text) { + let smallGap = ""; + + if (this.element.filterParentNames(["li"]).length) { + this.gap = ""; + smallGap = "\n"; + } + + return smallGap + super.decorate(trimRight(text)); + } + }; + } + static ol() { - return class extends Tag.block("ol") { + return class extends Tag.list("ol") { decorate(text) { text = "\n" + text; const bullet = text.match(/\n\t*\*/)[0]; @@ -295,7 +311,7 @@ const tags = [ Tag.cell("td"), Tag.cell("th"), Tag.replace("br", "\n"), Tag.replace("hr", "\n---\n"), Tag.replace("head", ""), Tag.keep("ins"), Tag.keep("del"), Tag.keep("small"), Tag.keep("big"), - Tag.li(), Tag.link(), Tag.image(), Tag.code(), Tag.blockquote(), Tag.table(), Tag.ol(), Tag.tr(), + Tag.li(), Tag.link(), Tag.image(), Tag.code(), Tag.blockquote(), Tag.table(), Tag.tr(), Tag.ol(), Tag.list("ul"), ]; class Element { diff --git a/test/javascripts/lib/to-markdown-test.js.es6 b/test/javascripts/lib/to-markdown-test.js.es6 index 24fe2a103d4..698dcd7570b 100644 --- a/test/javascripts/lib/to-markdown-test.js.es6 +++ b/test/javascripts/lib/to-markdown-test.js.es6 @@ -77,14 +77,14 @@ QUnit.test("converts ul list tag", assert => { Item 2
  • Item 3
  • `; - const markdown = `* Item 1\n* Item 2\n\n * Sub Item 1\n * Sub Item 2\n\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n\n* Item 3`; + const markdown = `* Item 1\n* Item 2\n * Sub Item 1\n * Sub Item 2\n\n * Sub Item 3\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n* Item 3`; assert.equal(toMarkdown(html), markdown); }); @@ -221,12 +221,11 @@ QUnit.test("converts ol list tag", assert => {
    1. Sub Item 1
    2. Sub Item 2
    3. -
  • Item 3
  • `; - const markdown = `Testing\n\n1. Item 1\n2. Item 2\n\n 100. Sub Item 1\n 101. Sub Item 2\n\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n\n3. Item 3`; + const markdown = `Testing\n\n1. Item 1\n2. Item 2\n 100. Sub Item 1\n 101. Sub Item 2\n3. Item 3`; assert.equal(toMarkdown(html), markdown); });