From ac1e93e82a4cb4444782a25b0820a6adab4092d7 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 22 Dec 2017 19:05:40 +0530 Subject: [PATCH] Minor fix to correctly trim spaces in HTML to Markdown conversion --- .../discourse/lib/to-markdown.js.es6 | 23 ++++++++++++------- test/javascripts/lib/to-markdown-test.js.es6 | 4 ++-- 2 files changed, 17 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 22eb2b56d6..2f236392fe 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -4,15 +4,20 @@ const trimLeft = text => text.replace(/^\s+/,""); const trimRight = text => text.replace(/\s+$/,""); class Tag { - constructor(name, prefix = "", suffix = "") { + constructor(name, prefix = "", suffix = "", inline = false) { this.name = name; this.prefix = prefix; this.suffix = suffix; + this.inline = inline; } decorate(text) { if (this.prefix || this.suffix) { - return [this.prefix, text, this.suffix].join(""); + text = [this.prefix, text, this.suffix].join(""); + } + + if (this.inline) { + text = " " + text + " "; } return text; @@ -69,7 +74,7 @@ class Tag { static emphasis(name, decorator) { return class extends Tag { constructor() { - super(name, decorator, decorator); + super(name, decorator, decorator, true); } decorate(text) { @@ -112,7 +117,7 @@ class Tag { static link() { return class extends Tag { constructor() { - super("a"); + super("a", "", "", true); } decorate(text) { @@ -131,7 +136,7 @@ class Tag { static image() { return class extends Tag { constructor() { - super("img"); + super("img", "", "", true); } toMarkdown() { @@ -193,7 +198,7 @@ class Tag { static li() { return class extends Tag.slice("li", "\n") { decorate(text) { - const indent = this.element.filterParentNames(["ol", "ul"]).slice(1).map(() => " ").join(""); + const indent = this.element.filterParentNames(["ol", "ul"]).slice(1).map(() => "\t").join(""); return super.decorate(`${indent}* ${trimLeft(text)}`); } }; @@ -209,6 +214,8 @@ class Tag { if (this.element.parentNames.includes("pre")) { this.prefix = '\n\n```\n'; this.suffix = '\n```\n\n'; + } else { + this.inline = true; } text = $('