Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): optionally add image captions from alt tags #618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
145 changes: 81 additions & 64 deletions dist/showdown.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/options.js
Expand Up @@ -46,6 +46,11 @@ function getDefaultOpts (simple) {
describe: 'Turn on/off image dimension parsing',
type: 'boolean'
},
extractImageCaptions: {
defaultValue: false,
describe: 'Extract image alt tag as a caption for the image',
type: 'boolean'
},
simplifiedAutoLink: {
defaultValue: false,
describe: 'Turn on/off GFM autolink style',
Expand Down
13 changes: 12 additions & 1 deletion src/subParsers/makehtml/images.js
Expand Up @@ -59,7 +59,13 @@ showdown.subParser('makehtml.images', function (text, options, globals) {
.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
//url = showdown.helper.escapeCharacters(url, '*_', false);
url = url.replace(showdown.helper.regexes.asteriskDashTildeAndColon, showdown.helper.escapeCharactersCallback);
var result = '<img src="' + url + '" alt="' + altText + '"';
var result = '';

if (options.extractImageCaptions && altText) {
result += '<figure>';
}

result += '<img src="' + url + '" alt="' + altText + '"';

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

result += ' />';

if (options.extractImageCaptions && altText) {
result += '<figcaption>' + altText + '</figcaption>';
result += '</figure>';
}

return result;
}

Expand Down
@@ -0,0 +1,2 @@
<p><figure><img src="./image.png" alt="This is a caption" /><figcaption>This is a caption</figcaption></figure></p>
<p><img src="./no-caption.png" alt="" /></p>
@@ -0,0 +1,3 @@
![This is a caption](./image.png)

![](./no-caption.png)
2 changes: 2 additions & 0 deletions test/functional/makehtml/testsuite.features.js
Expand Up @@ -95,6 +95,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({openLinksInNewWindow: true});
} else if (testsuite[i].name === '#355.simplifiedAutoLink-URLs-inside-parenthesis-followed-by-another-character-are-not-parsed-correctly') {
converter = new showdown.Converter({simplifiedAutoLink: true});
} else if (testsuite[i].name === 'extractImageCaptions') {
converter = new showdown.Converter({extractImageCaptions: true});
} else {
converter = new showdown.Converter();
}
Expand Down