nodebb-plugin-markdown 13.2.8 → 13.2.9

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 +43 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -3,11 +3,13 @@
3
3
  const MarkdownIt = require('markdown-it');
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
+ const crypto = require('crypto');
6
7
 
7
8
  const nconf = nodebb.require('nconf');
8
9
  const winston = nodebb.require('winston');
9
10
  const meta = nodebb.require('./src/meta');
10
11
  const activitypub = nodebb.require('./src/activitypub');
12
+ const db = nodebb.require('./src/database');
11
13
  const plugins = nodebb.require('./src/plugins');
12
14
 
13
15
  const SocketPlugins = nodebb.require('./src/socket.io/plugins');
@@ -21,6 +23,9 @@ function isRTL(text) {
21
23
  return !!match && rtlCharRegex.test(match[0]);
22
24
  }
23
25
 
26
+ const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
27
+ const resizedSuffix = /-resized(\.[\w]+)?$/;
28
+
24
29
  let parser;
25
30
  let app;
26
31
  const Markdown = {
@@ -168,7 +173,7 @@ const Markdown = {
168
173
  let env = {
169
174
  parse: true,
170
175
  type: data.type,
171
- images: new Map(), // is this still used?
176
+ images: new Map(),
172
177
  };
173
178
 
174
179
  ({ env } = await plugins.hooks.fire('filter:markdown.beforeParse', { env, data: Object.freeze({ ...data }) }));
@@ -186,9 +191,32 @@ const Markdown = {
186
191
  }
187
192
  }
188
193
 
194
+ if (env.parse && data.postData) {
195
+ await Markdown.addUploadSizes(env.images, data.postData.uploads);
196
+ }
197
+
189
198
  return env;
190
199
  },
191
200
 
201
+ // Adds the sizes of the post's own uploads to `images`, keyed by filename.
202
+ // They are recorded on upload by posts.uploads.saveSize, so this is a single
203
+ // read and never touches the image itself. Entries already present (added by
204
+ // filter:markdown.beforeParse) are left alone.
205
+ addUploadSizes: async (images, uploads) => {
206
+ if (!Array.isArray(uploads) || !uploads.length) {
207
+ return;
208
+ }
209
+
210
+ const sizes = await db.getObjects(uploads.map(filePath => `upload:${md5(filePath)}`));
211
+ uploads.forEach((filePath, idx) => {
212
+ const size = sizes[idx];
213
+ const filename = path.basename(filePath);
214
+ if (size && size.width && size.height && !images.has(filename)) {
215
+ images.set(filename, { width: size.width, height: size.height });
216
+ }
217
+ });
218
+ },
219
+
192
220
  afterParse: function (payload) {
193
221
  if (!payload) {
194
222
  return payload;
@@ -321,6 +349,20 @@ const Markdown = {
321
349
 
322
350
  token.attrSet('class', `${token.attrGet('class') || ''} img-fluid img-markdown`);
323
351
 
352
+ // Give the image its intrinsic size so it reserves the space it will
353
+ // take before it loads, otherwise images finishing above the viewport
354
+ // shift the content below them. A resized variant is not tracked
355
+ // separately, but it keeps the original's ratio, which is all that
356
+ // 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
+ }
364
+ }
365
+
324
366
  return renderImage(tokens, idx, options, env, self);
325
367
  };
326
368
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-markdown",
3
- "version": "13.2.8",
3
+ "version": "13.2.9",
4
4
  "description": "A Markdown parser for NodeBB",
5
5
  "main": "index.js",
6
6
  "repository": {