FIX: Convert lightbox html into proper image markdown

FIX: Convert lightbox html into proper image markdown

diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
index 22fbd80..ee3bd1e 100644
--- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6
+++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
@@ -8,6 +8,9 @@ const msoListClasses = [
   "MsoListParagraphCxSpMiddle",
   "MsoListParagraphCxSpLast"
 ];
+const hasChild = (e, n) => {
+  return (e.children || []).some(c => c.name === n);
+};
 
 export class Tag {
   constructor(name, prefix = "", suffix = "", inline = false) {
@@ -194,14 +197,19 @@ export class Tag {
       }
 
       decorate(text) {
-        const attr = this.element.attributes;
+        const e = this.element;
+        const attr = e.attributes;
 
         if (/^mention/.test(attr.class) && "@" === text[0]) {
           return text;
-        }
-
-        if ("hashtag" === attr.class && "#" === text[0]) {
+        } else if ("hashtag" === attr.class && "#" === text[0]) {
           return text;
+        } else if (
+          ["lightbox", "d-lazyload"].includes(attr.class) &&
+          hasChild(e, "img")
+        ) {
+          text = attr.title || "";
+          return "![" + text + "](" + attr.href + ")";
         }
 
         if (attr.href && text !== attr.href) {
diff --git a/test/javascripts/lib/to-markdown-test.js.es6 b/test/javascripts/lib/to-markdown-test.js.es6
index c79249c..f63a004 100644
--- a/test/javascripts/lib/to-markdown-test.js.es6
+++ b/test/javascripts/lib/to-markdown-test.js.es6
@@ -352,3 +352,18 @@ QUnit.test("keeps emoji syntax for custom emoji", assert => {
 
   assert.equal(toMarkdown(html), markdown);
 });
+
+QUnit.test("converts image lightboxes to markdown", assert => {
+  let html = `
+  <a class="lightbox" href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg" data-download-href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba" title="sherlock3_sig.jpg" rel="nofollow noopener"><img src="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg" alt="sherlock3_sig" width="689" height="459" class="d-lazyload" srcset="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1033x688.jpeg 1.5x, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1378x918.jpeg 2x"><div class="meta">
+  <span class="filename">sherlock3_sig.jpg</span><span class="informations">5496Γ—3664 2 MB</span><span class="expand"></span>
+  </div></a>
+  `;
+  const markdown = `![sherlock3_sig.jpg](https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg)`;
+
+  assert.equal(toMarkdown(html), markdown);
+
+  html = `<a class="lightbox" href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg" data-download-href="https://d11a6trkgmumsb.cloudfront.net/uploads/default/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba" title="sherlock3_sig.jpg" rel="nofollow noopener"><img src="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg" alt="sherlock3_sig" width="689" height="459" class="d-lazyload" srcset="https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_689x459.jpeg, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1033x688.jpeg 1.5x, https://d11a6trkgmumsb.cloudfront.net/uploads/default/optimized/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba_2_1378x918.jpeg 2x"></a>`;
+
+  assert.equal(toMarkdown(html), markdown);
+});

GitHub sha: 7e4edcfa

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: