nodebb-plugin-markdown 13.2.10 → 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.
Files changed (2) hide show
  1. package/index.js +35 -10
  2. 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
 
@@ -340,27 +353,39 @@ const Markdown = {
340
353
  const token = tokens[idx];
341
354
  const attributes = new Map(token.attrs);
342
355
  if (env.type === 'plaintext') {
343
- const filename = path.basename(attributes.get('src'));
344
- return `[image: ${filename}]`;
356
+ // The alt text is what the author wrote, so prefer it over the stored
357
+ // filename, which carries an upload timestamp (and a uuid, for pasted
358
+ // uploads) that reads as noise in teasers and other plaintext contexts.
359
+ // token.content is the raw alt text, so it is escaped here the way the
360
+ // text rule escapes the rest of the plaintext output
361
+ const label = token.content || path.basename(attributes.get('src'));
362
+ return `[image: ${parser.utils.escapeHtml(label)}]`;
345
363
  }
346
364
 
347
365
  // Validate the url
348
366
  if (!Markdown.isUrlValid(attributes.get('src'))) { return ''; }
349
367
 
350
- token.attrSet('class', `${token.attrGet('class') || ''} img-fluid img-markdown`);
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'}`);
351
372
 
352
373
  // Give the image its intrinsic size so it reserves the space it will
353
374
  // take before it loads, otherwise images finishing above the viewport
354
375
  // shift the content below them. A resized variant is not tracked
355
376
  // separately, but it keeps the original's ratio, which is all that
356
377
  // matters while these are rendered with `height: auto`
357
- if (env.images && env.images.size) {
358
- const filename = path.basename(new URL(attributes.get('src'), nconf.get('url')).pathname);
359
- const size = env.images.get(filename) || env.images.get(filename.replace(resizedSuffix, '$1'));
360
- if (size) {
361
- token.attrSet('width', size.width);
362
- token.attrSet('height', size.height);
363
- }
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);
364
389
  }
365
390
 
366
391
  return renderImage(tokens, idx, options, env, self);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-markdown",
3
- "version": "13.2.10",
3
+ "version": "13.2.12",
4
4
  "description": "A Markdown parser for NodeBB",
5
5
  "main": "index.js",
6
6
  "repository": {