From d0d61e4118951aaa464b8654f25a252a1e60e6c8 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 2 Oct 2020 10:56:40 +0530 Subject: [PATCH] FIX: remove whitespaces around inline HTML tags next to text. (#10803) --- .../javascripts/discourse/app/lib/to-markdown.js | 11 ++++++++++- test/javascripts/unit/lib/to-markdown-test.js | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/to-markdown.js b/app/assets/javascripts/discourse/app/lib/to-markdown.js index 78e16d59015..2989d36e39f 100644 --- a/app/assets/javascripts/discourse/app/lib/to-markdown.js +++ b/app/assets/javascripts/discourse/app/lib/to-markdown.js @@ -25,7 +25,16 @@ export class Tag { } if (this.inline) { - text = " " + text + " "; + const prev = this.element.prev; + const next = this.element.next; + + if (prev && prev.name !== "#text") { + text = " " + text; + } + + if (next && next.name !== "#text") { + text = text + " "; + } } return text; diff --git a/test/javascripts/unit/lib/to-markdown-test.js b/test/javascripts/unit/lib/to-markdown-test.js index 88f5464b2e7..2faddc81e22 100644 --- a/test/javascripts/unit/lib/to-markdown-test.js +++ b/test/javascripts/unit/lib/to-markdown-test.js @@ -8,6 +8,7 @@ QUnit.test("converts styles between normal words", (assert) => { assert.equal(toMarkdown(html), markdown); assert.equal(toMarkdown("A bold word"), "A **bold** word"); + assert.equal(toMarkdown("A bold, word"), "A **bold**, word"); }); QUnit.test("converts inline nested styles", (assert) => { @@ -239,7 +240,7 @@ helloWorld(); return; } helloWorld();consectetur.`; - output = `Lorem ipsum dolor sit amet, \`var helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\` consectetur.`; + output = `Lorem ipsum dolor sit amet, \`var helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\`consectetur.`; assert.equal(toMarkdown(html), output); });