Skip to content

Commit bbb7fac

Browse files
committed
feat(image): optionally add image captions from alt tags
1 parent 6679891 commit bbb7fac

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

src/options.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ function getDefaultOpts (simple) {
4646
describe: 'Turn on/off image dimension parsing',
4747
type: 'boolean'
4848
},
49+
extractImageCaptions: {
50+
defaultValue: false,
51+
describe: 'Extract image alt tag as a caption for the image',
52+
type: 'boolean'
53+
},
4954
simplifiedAutoLink: {
5055
defaultValue: false,
5156
describe: 'Turn on/off GFM autolink style',

src/subParsers/makehtml/images.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ showdown.subParser('makehtml.images', function (text, options, globals) {
5959
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
6060
//url = showdown.helper.escapeCharacters(url, '*_', false);
6161
url = url.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
62-
var result = '<img src="' + url + '" alt="' + altText + '"';
62+
var result = '';
63+
64+
if (options.extractImageCaptions && altText) {
65+
result += '<figure>';
66+
}
67+
68+
result += '<img src="' + url + '" alt="' + altText + '"';
6369

6470
if (title && showdown.helper.isString(title)) {
6571
title = title
@@ -79,6 +85,11 @@ showdown.subParser('makehtml.images', function (text, options, globals) {
7985

8086
result += ' />';
8187

88+
if (options.extractImageCaptions && altText) {
89+
result += '<figcaption>' + altText + '</figcaption>';
90+
result += '</figure>';
91+
}
92+
8293
return result;
8394
}
8495

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p><figure><img src="./image.png" alt="This is a caption" /><figcaption>This is a caption</figcaption></figure></p>
2+
<p><img src="./no-caption.png" alt="" /></p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
![This is a caption](./image.png)
2+
3+
![](./no-caption.png)

test/functional/makehtml/testsuite.features.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ describe('makeHtml() features testsuite', function () {
9595
converter = new showdown.Converter({openLinksInNewWindow: true});
9696
} else if (testsuite[i].name === '#355.simplifiedAutoLink-URLs-inside-parenthesis-followed-by-another-character-are-not-parsed-correctly') {
9797
converter = new showdown.Converter({simplifiedAutoLink: true});
98+
} else if (testsuite[i].name === 'extractImageCaptions') {
99+
converter = new showdown.Converter({extractImageCaptions: true});
98100
} else {
99101
converter = new showdown.Converter();
100102
}

0 commit comments

Comments
 (0)