fix: properly handle a false value for the parseImgDimensions option

This commit is contained in:
Aviad Pineles 2023-07-11 17:37:39 +03:00
parent 95255984ad
commit 03b6f617de
8 changed files with 42 additions and 10 deletions

View File

@ -241,7 +241,7 @@ var defaultOptions = showdown.getDefaultOptions();
<h3>foo</h3> <h3>foo</h3>
``` ```
* **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: Examples:
``` ```
![foo](foo.jpg =100x80) simple, assumes units are in px ![foo](foo.jpg =100x80) simple, assumes units are in px

View File

@ -475,7 +475,7 @@ Open links in new windows.
Set image dimensions from within Markdown syntax. Set image dimensions from within Markdown syntax.
* type: `boolean` * type: `boolean`
* default value: `false` * default value: `true`
* introduced in: `1.1.0` * introduced in: `1.1.0`
=== "example" === "example"

View File

@ -42,7 +42,7 @@ function getDefaultOpts (simple) {
type: 'integer' type: 'integer'
}, },
parseImgDimensions: { parseImgDimensions: {
defaultValue: false, defaultValue: true,
describe: 'Turn on/off image dimension parsing', describe: 'Turn on/off image dimension parsing',
type: 'boolean' type: 'boolean'
}, },

View File

@ -6,11 +6,25 @@ showdown.subParser('makehtml.images', function (text, options, globals) {
text = globals.converter._dispatch('makehtml.images.before', text, options, globals).getText(); text = globals.converter._dispatch('makehtml.images.before', text, options, globals).getText();
var inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?([\S]+?(?:\([\S]*?\)[\S]*?)?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g, var inlineRegExp,
crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g, crazyRegExp,
base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g, base64RegExp,
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g, referenceRegExp,
refShortcutRegExp;
if (options.parseImgDimensions) {
inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?([\S]+?(?:\([\S]*?\)[\S]*?)?)>?(?: =([*\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]?<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g;
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g;
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g; refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;
} else {
inlineRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?([\S]+?(?:\([\S]*?\)[\S]*?)?)>?()()[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g;
crazyRegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<([^>]*)>()()[ \t]*(?:(?:(["'])([^"]*?)\6))?[ \t]?\)/g;
base64RegExp = /!\[([^\]]*?)][ \t]*()\([ \t]?<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?()()[ \t]*(?:(["'])([^"]*?)\6)?[ \t]?\)/g;
referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[([\s\S]*?)]()()()()()/g;
refShortcutRegExp = /!\[([^\[\]]+)]()()()()()/g;
}
function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) { function writeImageTagBase64 (wholeMatch, altText, linkId, url, width, height, m5, title) {
url = url.replace(/\s/g, ''); url = url.replace(/\s/g, '');

View File

@ -6,8 +6,16 @@
showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) { showdown.subParser('makehtml.stripLinkDefinitions', function (text, options, globals) {
'use strict'; '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, 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]*<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm; base64Regex = /^ {0,3}\[([^\]]+)]:[ \t]*\n?[ \t]*<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?(?: =([*\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]*<?(data:.+?\/.+?;base64,[A-Za-z0-9+/=\n]+?)>?()()[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n\n|(?=¨0)|(?=\n\[))/gm;
}
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '¨0'; text += '¨0';

View File

@ -0,0 +1,3 @@
<p>![my image](./pic/pic1_50.png =100pxx20px)</p>
<p>![my image2][1]</p>
<p>[1]: ./pic/pic1_50.png =100pxx20px</p>

View File

@ -0,0 +1,5 @@
![my image](./pic/pic1_50.png =100pxx20px)
![my image2][1]
[1]: ./pic/pic1_50.png =100pxx20px

View File

@ -32,6 +32,8 @@ describe('makeHtml() features testsuite', function () {
var converter; var converter;
if (testsuite[i].name === '#143.support-image-dimensions') { if (testsuite[i].name === '#143.support-image-dimensions') {
converter = new showdown.Converter({parseImgDimensions: true}); 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') { } else if (testsuite[i].name === '#69.header-level-start') {
converter = new showdown.Converter({headerLevelStart: 3}); 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') { } else if (testsuite[i].name === '#164.1.simple-autolink' || testsuite[i].name === '#204.certain-links-with-at-and-dot-break-url') {