Skip to content

Commit c5296a1

Browse files
committed
chore: add more tests with different size configs, refactor styling block
1 parent bbf59b6 commit c5296a1

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/transform/plugins/imsize/plugin.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,23 @@ export const imageWithSize = (md: MarkdownIt): ParserInline.RuleInline => {
207207
token.attrs.push([ImsizeAttr.Height, height]);
208208
}
209209

210-
let style: string | undefined;
210+
let style: string | undefined = '';
211211

212-
if (width !== '') {
213-
const widthWithPercent = width.includes('%');
214-
const heightWithPercent = height.includes('%');
212+
const widthWithPercent = width.includes('%');
213+
const heightWithPercent = height.includes('%');
215214

215+
if (width !== '') {
216216
const widthString = widthWithPercent ? width : `${width}px`;
217-
style = `width: ${widthString};`;
217+
style += `width: ${widthString};`;
218+
}
218219

219-
if (height !== '' && !heightWithPercent && !widthWithPercent) {
220-
style += ` aspect-ratio: ${width} / ${height}; height: auto;`;
220+
if (height !== '') {
221+
if (width !== '' && !heightWithPercent && !widthWithPercent) {
222+
style += `aspect-ratio: ${width} / ${height};height: auto;`;
223+
} else {
224+
const heightString = heightWithPercent ? height : `${height}px`;
225+
style += `height: ${heightString};`;
221226
}
222-
} else if (height !== '') {
223-
const heightString = height.includes('%') ? height : `${height}px`;
224-
style = `height: ${heightString};`;
225227
}
226228

227229
if (style) {

test/data/imsize/imsize-fixtures.txt

+24-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ Coverage. Image
226226
.
227227
![test]( x =100x200)
228228
.
229-
<p><img src="x" alt="test" width="100" height="200" style="width: 100px; aspect-ratio: 100 / 200; height: auto;"></p>
229+
<p><img src="x" alt="test" width="100" height="200" style="width: 100px;aspect-ratio: 100 / 200;height: auto;"></p>
230230
.
231231
.
232232
![test]( x =x)
@@ -246,7 +246,7 @@ Coverage. Image
246246
.
247247
![test]( x "title" =100x200)
248248
.
249-
<p><img src="x" alt="test" title="title" width="100" height="200" style="width: 100px; aspect-ratio: 100 / 200; height: auto;"></p>
249+
<p><img src="x" alt="test" title="title" width="100" height="200" style="width: 100px;aspect-ratio: 100 / 200;height: auto;"></p>
250250
.
251251
.
252252
![test]( x =WxH )
@@ -266,7 +266,7 @@ Coverage. Image
266266
.
267267
![test](http://this.is.test.jpg =100x200)
268268
.
269-
<p><img src="http://this.is.test.jpg" alt="test" width="100" height="200" style="width: 100px; aspect-ratio: 100 / 200; height: auto;"></p>
269+
<p><img src="http://this.is.test.jpg" alt="test" width="100" height="200" style="width: 100px;aspect-ratio: 100 / 200;height: auto;"></p>
270270
.
271271
.
272272
![test](<x =100x200)
@@ -276,13 +276,33 @@ Coverage. Image
276276
.
277277
![test](<x> =100x200)
278278
.
279-
<p><img src="x" alt="test" width="100" height="200" style="width: 100px; aspect-ratio: 100 / 200; height: auto;"></p>
279+
<p><img src="x" alt="test" width="100" height="200" style="width: 100px;aspect-ratio: 100 / 200;height: auto;"></p>
280280
.
281281
.
282282
![test](test =100%x)
283283
.
284284
<p><img src="test" alt="test" width="100%" style="width: 100%;"></p>
285285
.
286+
.
287+
![test](test =x100%)
288+
.
289+
<p><img src="test" alt="test" height="100%" style="height: 100%;"></p>
290+
.
291+
.
292+
![test](test =100%x100%)
293+
.
294+
<p><img src="test" alt="test" width="100%" height="100%" style="width: 100%;height: 100%;"></p>
295+
.
296+
.
297+
![test](test =100%x200)
298+
.
299+
<p><img src="test" alt="test" width="100%" height="200" style="width: 100%;height: 200px;"></p>
300+
.
301+
.
302+
![test](test =100x100%)
303+
.
304+
<p><img src="test" alt="test" width="100" height="100%" style="width: 100px;height: 100%;"></p>
305+
.
286306

287307
Coverage. Link
288308
.

0 commit comments

Comments
 (0)