From 948ce9160261f364f48569cbb7af17cfec1805dd Mon Sep 17 00:00:00 2001 From: Xenira <1288524+Xenira@users.noreply.github.com> Date: Thu, 1 Feb 2024 01:43:47 +0100 Subject: [PATCH] feat: add support for link attribute --- lib/index.js | 4 +++- test/downdoc-test.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index dc62a6c..d3772e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -311,7 +311,9 @@ function hardbreak (str, mark, force, len = str.length) { } function image (_, target, attrlist, _idx, _str, alt = attrlist.split(',')[0] || /(.*\/)?(.*?)($|\.)/.exec(target)[2]) { - return '![' + alt + '](' + (this.get('imagesdir') ? this.get('imagesdir') + '/' : '') + target + ')' + const link = attrlist.split(',').map((it) => /link="{0,1}(.+?)"{0,1}$/.exec(it)?.[1]).find((it) => it) + const image = '![' + alt + '](' + (this.get('imagesdir') ? this.get('imagesdir') + '/' : '') + target + ')' + return link ? '[' + image + '](' + link + ')' : image } function isAnyListItem (chr0, str, mode = 'test', match = chr0 in LIST_MARKERS && ListItemRx[mode](str)) { diff --git a/test/downdoc-test.js b/test/downdoc-test.js index bed19ae..b76720b 100644 --- a/test/downdoc-test.js +++ b/test/downdoc-test.js @@ -4311,6 +4311,22 @@ describe('downdoc()', () => { ` expect(downdoc(input)).to.equal(expected) }) + + it('should wrap image in link if link attribute is set', () => { + const input = heredoc` + = Title + + image::images/screenshot.png[Screenshot,link=https://example.com] + image::images/screenshot.png[Screenshot,link="https://example.com"] + ` + const expected = heredoc` + # Title + + [![Screenshot](images/screenshot.png)](https://example.com) + [![Screenshot](images/screenshot.png)](https://example.com) + ` + expect(downdoc(input)).to.equal(expected) + }) }) describe('blockquotes', () => {