From 4c99acea1b8abbf1465abe54831392cfcfca9aa0 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 29 Aug 2018 16:21:31 +0000 Subject: [PATCH] FIX: Use tight list format for GDocs html to markdown --- .../discourse/lib/to-markdown.js.es6 | 12 ++++++++++++ test/javascripts/lib/to-markdown-test.js.es6 | 17 +++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 index 560c065730..9b08fee72f 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -109,6 +109,12 @@ export class Tag { } decorate(text) { + const parent = this.element.parent; + + if (this.name === "p" && parent && parent.name === "li") { // fix for google docs + this.gap = ""; + } + return `${this.gap}${this.prefix}${text}${this.suffix}${this.gap}`; } }; @@ -400,6 +406,12 @@ export class Tag { return class extends Tag.block(name) { decorate(text) { let smallGap = ""; + const parent = this.element.parent; + + if (parent && parent.name === "ul") { + this.gap = ""; + this.suffix = "\n"; + } if (this.element.filterParentNames(["li"]).length) { this.gap = ""; diff --git a/test/javascripts/lib/to-markdown-test.js.es6 b/test/javascripts/lib/to-markdown-test.js.es6 index 0ab96fa43a..57a437a4a5 100644 --- a/test/javascripts/lib/to-markdown-test.js.es6 +++ b/test/javascripts/lib/to-markdown-test.js.es6 @@ -70,7 +70,7 @@ QUnit.test("converts heading tags", assert => { }); QUnit.test("converts ul list tag", assert => { - const html = ` + let html = ` `; - 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`; + let markdown = `* Item 1\n* Item 2\n * Sub Item 1\n * Sub Item 2\n * Sub Item 3\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n* Item 3`; + assert.equal(toMarkdown(html), markdown); + + html = ` + `; + markdown = `* Bullets at level 1 +* Bullets at level 1 + * Bullets at level 2 + * Bullets at level 2 + * Bullets at level 3 + * Bullets at level 2 +* Bullets at level 1`; assert.equal(toMarkdown(html), markdown); });