From 03b6f617ded3d4941000abfe3a9993350961c2df Mon Sep 17 00:00:00 2001 From: Aviad Pineles Date: Tue, 11 Jul 2023 17:37:39 +0300 Subject: [PATCH 1/2] fix: properly handle a false value for the parseImgDimensions option --- README.md | 2 +- docs/available-options.md | 2 +- src/options.js | 2 +- src/subParsers/makehtml/images.js | 24 +++++++++++++++---- .../makehtml/stripLinkDefinitions.js | 12 ++++++++-- .../#143.not.support-image-dimensions.html | 3 +++ .../#143.not.support-image-dimensions.md | 5 ++++ .../functional/makehtml/testsuite.features.js | 2 ++ 8 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html create mode 100644 test/functional/makehtml/cases/features/#143.not.support-image-dimensions.md diff --git a/README.md b/README.md index 4d858ae09..568384af4 100644 --- a/README.md +++ b/README.md @@ -241,7 +241,7 @@ var defaultOptions = showdown.getDefaultOptions();

foo

``` - * **parseImgDimensions**: (boolean) [default false] Enable support for setting image dimensions from within markdown syntax. + * **parseImgDimensions**: (boolean) [default true] Enable support for setting image dimensions from within markdown syntax. Examples: ``` ![foo](foo.jpg =100x80) simple, assumes units are in px diff --git a/docs/available-options.md b/docs/available-options.md index 9cbc3c050..71ced37ca 100644 --- a/docs/available-options.md +++ b/docs/available-options.md @@ -475,7 +475,7 @@ Open links in new windows. Set image dimensions from within Markdown syntax. * type: `boolean` -* default value: `false` +* default value: `true` * introduced in: `1.1.0` === "example" diff --git a/src/options.js b/src/options.js index 19deb6672..addd830cc 100644 --- a/src/options.js +++ b/src/options.js @@ -42,7 +42,7 @@ function getDefaultOpts (simple) { type: 'integer' }, parseImgDimensions: { - defaultValue: false, + defaultValue: true, describe: 'Turn on/off image dimension parsing', type: 'boolean' }, diff --git a/src/subParsers/makehtml/images.js b/src/subParsers/makehtml/images.js index e7fbbc1d6..d87b3a8e3 100644 --- a/src/subParsers/makehtml/images.js +++ b/src/subParsers/makehtml/images.js @@ -6,11 +6,25 @@ showdown.subParser('makehtml.images', function (text, options, globals) { text = globals.converter._dispatch('makehtml.images.before', text, options, globals).getText(); - var inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g, - crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g, - base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g, - referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g, - refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; + var inlineRegExp, + crazyRegExp, + base64RegExp, + referenceRegExp, + refShortcutRegExp; + + if (options.parseImgDimensions) { + inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; + crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g; + base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; + referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g; + refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; + } else { + inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??()()[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; + crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>()()[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g; + base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??()()[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; + referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g; + refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; + } function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) { url = url.replace(/\s/g, ''); diff --git a/src/subParsers/makehtml/stripLinkDefinitions.js b/src/subParsers/makehtml/stripLinkDefinitions.js index 894c8d97f..4e092753b 100644 --- a/src/subParsers/makehtml/stripLinkDefinitions.js +++ b/src/subParsers/makehtml/stripLinkDefinitions.js @@ -6,8 +6,16 @@ showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) { 'use strict'; - var regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm, - base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; + var regex, + base64Regex; + + if (options.parseImgDimensions) { + regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm; + base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; + } else { + regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*\s]+)>?()()[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm; + base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*?()()[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; + } // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug text += '¨0'; diff --git a/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html b/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html new file mode 100644 index 000000000..ba26b507d --- /dev/null +++ b/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html @@ -0,0 +1,3 @@ +

![my image](./pic/pic1_50.png =100pxx20px)

+

![my image2][1]

+

[1]: ./pic/pic1_50.png =100pxx20px

diff --git a/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.md b/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.md new file mode 100644 index 000000000..ecbd02f5e --- /dev/null +++ b/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.md @@ -0,0 +1,5 @@ +![my image](./pic/pic1_50.png =100pxx20px) + +![my image2][1] + +[1]: ./pic/pic1_50.png =100pxx20px diff --git a/test/functional/makehtml/testsuite.features.js b/test/functional/makehtml/testsuite.features.js index 2ef6c38ea..62d89bc0e 100644 --- a/test/functional/makehtml/testsuite.features.js +++ b/test/functional/makehtml/testsuite.features.js @@ -32,6 +32,8 @@ describe('makeHtml() features testsuite', function () { var converter; if (testsuite[i].name === '#143.support-image-dimensions') { converter = new showdown.Converter({parseImgDimensions: true}); + } else if (testsuite[i].name === '#143.not.support-image-dimensions') { + converter = new showdown.Converter({parseImgDimensions: false}); } else if (testsuite[i].name === '#69.header-level-start') { converter = new showdown.Converter({headerLevelStart: 3}); } else if (testsuite[i].name === '#164.1.simple-autolink' || testsuite[i].name === '#204.certain-links-with-at-and-dot-break-url') { From 456a6f712d11b1bc89fa2fe6c49587767886bb0c Mon Sep 17 00:00:00 2001 From: Aviad Pineles Date: Tue, 18 Jul 2023 19:21:02 +0300 Subject: [PATCH 2/2] when parseImgDimensions is false, don't reject markdown, simply ignore dimensions --- src/subParsers/makehtml/images.js | 26 +++++-------------- .../makehtml/stripLinkDefinitions.js | 12 ++------- .../#143.not.support-image-dimensions.html | 5 ++-- 3 files changed, 10 insertions(+), 33 deletions(-) diff --git a/src/subParsers/makehtml/images.js b/src/subParsers/makehtml/images.js index d87b3a8e3..8b28537de 100644 --- a/src/subParsers/makehtml/images.js +++ b/src/subParsers/makehtml/images.js @@ -6,25 +6,11 @@ showdown.subParser('makehtml.images', function (text, options, globals) { text = globals.converter._dispatch('makehtml.images.before', text, options, globals).getText(); - var inlineRegExp, - crazyRegExp, - base64RegExp, - referenceRegExp, - refShortcutRegExp; - - if (options.parseImgDimensions) { - inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; - crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g; - base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; - referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g; - refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; - } else { - inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??()()[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; - crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>()()[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g; - base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??()()[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g; - referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g; - refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; - } + var inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g, + crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g, + base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]??(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g, + referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g, + refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) { url = url.replace(/\s/g, ''); @@ -89,7 +75,7 @@ showdown.subParser('makehtml.images', function (text, options, globals) { result += ' title="' + title + '"'; } - if (width && height) { + if (options.parseImgDimensions && width && height) { width = (width === '*') ? 'auto' : width; height = (height === '*') ? 'auto' : height; diff --git a/src/subParsers/makehtml/stripLinkDefinitions.js b/src/subParsers/makehtml/stripLinkDefinitions.js index 4e092753b..894c8d97f 100644 --- a/src/subParsers/makehtml/stripLinkDefinitions.js +++ b/src/subParsers/makehtml/stripLinkDefinitions.js @@ -6,16 +6,8 @@ showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) { 'use strict'; - var regex, - base64Regex; - - if (options.parseImgDimensions) { - regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm; - base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; - } else { - regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*\s]+)>?()()[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm; - base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*?()()[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; - } + var regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*\s]+)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=¨0))/gm, + base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug text += '¨0'; diff --git a/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html b/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html index ba26b507d..0bc6a8a37 100644 --- a/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html +++ b/test/functional/makehtml/cases/features/#143.not.support-image-dimensions.html @@ -1,3 +1,2 @@ -

![my image](./pic/pic1_50.png =100pxx20px)

-

![my image2][1]

-

[1]: ./pic/pic1_50.png =100pxx20px

+

my image

+

my image2