FIX: Convert lightbox html into proper image markdown

This commit is contained in:
Vinoth Kannan 2019-01-27 01:21:56 +05:30
parent a72ed6278b
commit 7e4edcfae8
2 changed files with 27 additions and 4 deletions

View File

@ -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) {

View File

@ -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);
});