Skip to content

Commit

Permalink
Merge pull request #34 from noah227/master
Browse files Browse the repository at this point in the history
feat: support for resized image
  • Loading branch information
patorjk authored Oct 12, 2024
2 parents d0244a5 + 6936718 commit d3b8609
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions xbbcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,33 @@ var XBBCODE = (function() {
},
"img": {
openTag: function(params,content) {

params = params?.trim() || "";
var cssList = [];

if(params) {
// e.g. [img={width}x{height}]{url}[/img]
var execFullSize = /^=\s*(\d+)\s*(x\s*(\d+))?/.exec(params)
if (execFullSize) {
var [width, height] = [execFullSize[1], execFullSize[3]];
cssList.push(`width: ${width}px`, `height: ${height || width}px`);
}
// e.g. [img width={width} height={height}]{url}[/img]
else {
var execWidth = /width\s*=\s*(\d+)/.exec(params);
if (execWidth) cssList.push(`width: ${execWidth[1]}px`);
var execHeight = /height\s*=\s*(\d+)/.exec(params);
if (execHeight) cssList.push(`height: ${execHeight[1]}px`);
}
}

var myUrl = content;

urlPattern.lastIndex = 0;
if ( !urlPattern.test( myUrl ) ) {
myUrl = "";
}

return '<img src="' + myUrl + '" />';
return `<img src="${myUrl}" style="${cssList.join(";")}"/>`;
},
closeTag: function(params,content) {
return '';
Expand Down

0 comments on commit d3b8609

Please sign in to comment.