From 5e83432c4e15d82a008be38c7241d235073bf035 Mon Sep 17 00:00:00 2001 From: Thomas Hunter II Date: Mon, 12 Nov 2018 22:14:03 -0800 Subject: [PATCH] feat(image): optionally add image captions from alt tags --- src/options.js | 5 +++++ src/subParsers/makehtml/images.js | 13 ++++++++++++- .../cases/features/extractImageCaptions.html | 1 + .../makehtml/cases/features/extractImageCaptions.md | 1 + test/functional/makehtml/testsuite.features.js | 2 ++ 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/functional/makehtml/cases/features/extractImageCaptions.html create mode 100644 test/functional/makehtml/cases/features/extractImageCaptions.md diff --git a/src/options.js b/src/options.js index afefab0d..0f6cb95b 100644 --- a/src/options.js +++ b/src/options.js @@ -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', diff --git a/src/subParsers/makehtml/images.js b/src/subParsers/makehtml/images.js index 63d9f09f..ff58cc2a 100644 --- a/src/subParsers/makehtml/images.js +++ b/src/subParsers/makehtml/images.js @@ -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 = '' + altText + ''; + result += ''; + } + return result; } diff --git a/test/functional/makehtml/cases/features/extractImageCaptions.html b/test/functional/makehtml/cases/features/extractImageCaptions.html new file mode 100644 index 00000000..704164be --- /dev/null +++ b/test/functional/makehtml/cases/features/extractImageCaptions.html @@ -0,0 +1 @@ +

This is a caption
This is a caption

diff --git a/test/functional/makehtml/cases/features/extractImageCaptions.md b/test/functional/makehtml/cases/features/extractImageCaptions.md new file mode 100644 index 00000000..8c429a22 --- /dev/null +++ b/test/functional/makehtml/cases/features/extractImageCaptions.md @@ -0,0 +1 @@ +![This is a caption](./image.png) diff --git a/test/functional/makehtml/testsuite.features.js b/test/functional/makehtml/testsuite.features.js index 4affe1c9..082a300c 100644 --- a/test/functional/makehtml/testsuite.features.js +++ b/test/functional/makehtml/testsuite.features.js @@ -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(); }