FEATURE: Rich text pasting support

FEATURE: Rich text pasting support

diff --git a/assets/javascripts/initializers/spoiler-alert.js.es6 b/assets/javascripts/initializers/spoiler-alert.js.es6
index a5995bd..06ae47e 100644
--- a/assets/javascripts/initializers/spoiler-alert.js.es6
+++ b/assets/javascripts/initializers/spoiler-alert.js.es6
@@ -1,4 +1,5 @@
 import { withPluginApi, decorateCooked } from 'discourse/lib/plugin-api';
+import { Tag } from 'discourse/lib/to-markdown';
 import ComposerController from 'discourse/controllers/composer';
 
 function spoil($elem) {
@@ -28,6 +29,44 @@ function initializeSpoiler(api) {
       }
     }
   });
+
+  Tag.prototype.decorate = function(text) {
+    const attr = this.element.attributes;
+    if (attr.class === "spoiled") {
+      this.prefix = "[spoiler]";
+      this.suffix = "[/spoiler]";
+    }
+
+    if (this.prefix || this.suffix) {
+      text = [this.prefix, text, this.suffix].join("");
+    }
+
+    if (this.inline) {
+      text = " " + text + " ";
+    }
+
+    return text;
+  };
+
+  Tag.block = function(name, prefix, suffix) {
+    return class extends Tag {
+      constructor() {
+        super(name, prefix, suffix);
+        this.gap = "\n\n";
+      }
+
+      decorate(text) {
+        const attr = this.element.attributes;
+        if (this.name === "div" && attr.class === "spoiled") {
+          this.prefix = "[spoiler]";
+          this.suffix = "[/spoiler]";
+          text = text.trim();
+        }
+
+        return `${this.gap}${this.prefix}${text}${this.suffix}${this.gap}`;
+      }
+    };
+  };
 }
 
 export default {
diff --git a/test/javascripts/acceptance/to-markdown-test.js.es6 b/test/javascripts/acceptance/to-markdown-test.js.es6
new file mode 100644
index 0000000..e0bd984
--- /dev/null
+++ b/test/javascripts/acceptance/to-markdown-test.js.es6
@@ -0,0 +1,26 @@
+import { acceptance } from "helpers/qunit-helpers";
+import toMarkdown from "discourse/lib/to-markdown";
+
+acceptance("To Markdown", {
+  settings: { spoiler_enabled: true, enable_rich_text_paste: true }
+});
+
+test("to-markdown", async assert => {
+  await visit("/");
+
+  let html = `<div>Text with a</div><div class="spoiled">spoiled</div><div>word.</div>`;
+  let markdown = `Text with a\n\n[spoiler]spoiled[/spoiler]\n\nword.`;
+  assert.equal(
+    toMarkdown(html),
+    markdown,
+    "it should create spoiler tag"
+  );
+
+  html = `Inline <span class="spoiled">spoiled</span> word.`;
+  markdown = `Inline [spoiler]spoiled[/spoiler] word.`;
+  assert.equal(
+    toMarkdown(html),
+    markdown,
+    "it should inline create spoiler tag"
+  );
+});

GitHub
sha: cf6c1577