nodebb-plugin-markdown 13.2.11 → 13.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +28 -8
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -23,6 +23,19 @@ function isRTL(text) {
|
|
|
23
23
|
return !!match && rtlCharRegex.test(match[0]);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
function getImageDimension(value) {
|
|
27
|
+
if (!value) {
|
|
28
|
+
return '';
|
|
29
|
+
}
|
|
30
|
+
if (/^(?:\d+(?:\.\d+)?)%$/.test(value)) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
if (/^\d+$/.test(value)) {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
|
|
26
39
|
const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
|
|
27
40
|
const resizedSuffix = /-resized(\.[\w]+)?$/;
|
|
28
41
|
|
|
@@ -352,20 +365,27 @@ const Markdown = {
|
|
|
352
365
|
// Validate the url
|
|
353
366
|
if (!Markdown.isUrlValid(attributes.get('src'))) { return ''; }
|
|
354
367
|
|
|
355
|
-
|
|
368
|
+
const imageUrl = new URL(attributes.get('src'), nconf.get('url'));
|
|
369
|
+
const { searchParams } = imageUrl;
|
|
370
|
+
const hasSizeParams = searchParams.has('w') || searchParams.has('h');
|
|
371
|
+
token.attrSet('class', `img-markdown ${token.attrGet('class') || ''} ${hasSizeParams ? 'not-responsive' : 'img-fluid'}`);
|
|
356
372
|
|
|
357
373
|
// Give the image its intrinsic size so it reserves the space it will
|
|
358
374
|
// take before it loads, otherwise images finishing above the viewport
|
|
359
375
|
// shift the content below them. A resized variant is not tracked
|
|
360
376
|
// separately, but it keeps the original's ratio, which is all that
|
|
361
377
|
// matters while these are rendered with `height: auto`
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
378
|
+
const filename = path.basename(imageUrl.pathname);
|
|
379
|
+
const size = !hasSizeParams && (env.images?.get(filename) || env.images?.get(filename.replace(resizedSuffix, '$1')));
|
|
380
|
+
const widthParam = getImageDimension(searchParams.get('w'));
|
|
381
|
+
const heightParam = getImageDimension(searchParams.get('h'));
|
|
382
|
+
const width = widthParam || size?.width || '';
|
|
383
|
+
const height = heightParam || size?.height || '';
|
|
384
|
+
if (width) {
|
|
385
|
+
token.attrSet('width', width);
|
|
386
|
+
}
|
|
387
|
+
if (height) {
|
|
388
|
+
token.attrSet('height', height);
|
|
369
389
|
}
|
|
370
390
|
|
|
371
391
|
return renderImage(tokens, idx, options, env, self);
|