From 1bb258ab497c25eb502a04112b746e98860968e5 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 5 Jul 2019 10:06:41 +0530 Subject: [PATCH] DEV: use upload short-url in html to markdown conversion if 'base62-sha1' data attr available. --- .../javascripts/discourse/lib/to-markdown.js.es6 | 10 +++++++++- test/javascripts/lib/to-markdown-test.js.es6 | 11 ++++++++++- 2 files changed, 19 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 72ae8aaa920..4a3c4862513 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -206,8 +206,16 @@ export class Tag { ["lightbox", "d-lazyload"].includes(attr.class) && hasChild(e, "img") ) { + let href = attr.href; + const img = (e.children || []).find(c => c.name === "img"); + const base62SHA1 = img.attributes["data-base62-sha1"]; text = attr.title || ""; - return "![" + text + "](" + attr.href + ")"; + + if (base62SHA1) { + href = `upload://${base62SHA1}`; + } + + return "![" + text + "](" + 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 12f5a0f91ce..e6fc113ce79 100644 --- a/test/javascripts/lib/to-markdown-test.js.es6 +++ b/test/javascripts/lib/to-markdown-test.js.es6 @@ -369,11 +369,20 @@ QUnit.test("converts image lightboxes to markdown", assert => { sherlock3_sig.jpg5496×3664 2 MB `; - const markdown = `![sherlock3_sig.jpg](https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg)`; + let markdown = `![sherlock3_sig.jpg](https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg)`; assert.equal(toMarkdown(html), markdown); html = `sherlock3_sig`; assert.equal(toMarkdown(html), markdown); + + html = ` + sherlock3_sig
+ sherlock3_sig.jpg5496×3664 2 MB +
+ `; + markdown = `![sherlock3_sig.jpg](upload://1frsimI7TOtFJyD2LLyKSHM8JWe)`; + + assert.equal(toMarkdown(html), markdown); });