nodebb-plugin-link-preview 2.0.16 → 2.1.1

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/library.js CHANGED
@@ -90,16 +90,8 @@ async function process(content, { type, pid, tid, attachments }) {
90
90
  }
91
91
 
92
92
  const requests = new Map();
93
- if (pid && Array.isArray(attachments) && attachments.length) {
94
- const attachmentData = await posts.attachments.getAttachments(attachments);
95
- attachmentData.filter(Boolean).forEach(({ url, _type }) => {
96
- const isInlineImage = new RegExp(`<img.+?src="${url}".+?>`).test(content);
97
- if (!isInlineImage) {
98
- const type = _type || 'attachment';
99
- requests.set(url, { type });
100
- }
101
- });
102
- }
93
+ let attachmentHtml = '';
94
+ let placeholderHtml = '';
103
95
 
104
96
  // Parse inline urls
105
97
  const $ = load(content, null, false);
@@ -108,7 +100,7 @@ async function process(content, { type, pid, tid, attachments }) {
108
100
 
109
101
  // Skip if the anchor has link text, or has text on the same line.
110
102
  let url = $anchor.attr('href');
111
- url = decodeURI(url);
103
+ url = decodeURI(url);
112
104
  const text = $anchor.text();
113
105
  const hasSiblings = !!anchor.prev || !!anchor.next;
114
106
  if (hasSiblings || url !== text || anchor.parent.name !== 'p') {
@@ -135,9 +127,36 @@ async function process(content, { type, pid, tid, attachments }) {
135
127
  });
136
128
  }
137
129
 
130
+ // Post attachments
131
+ if (pid && Array.isArray(attachments) && attachments.length) {
132
+ const attachmentData = await posts.attachments.getAttachments(attachments);
133
+ await Promise.all(attachmentData.filter(Boolean).map(async (attachment) => {
134
+ const { url, _type } = attachment;
135
+ const isInlineImage = new RegExp(`<img.+?src="${url}".+?>`).test(content);
136
+ if (isInlineImage) {
137
+ return;
138
+ }
139
+
140
+ const special = await handleSpecialEmbed(url);
141
+ if (special) {
142
+ attachmentHtml += special;
143
+ return;
144
+ }
145
+
146
+ if (attachment.hasOwnProperty('mediaType') && attachment.mediaType.startsWith('video/')) { // ActivityPub
147
+ cache.set(`link-preview:${url}`, {
148
+ ...attachment,
149
+ contentType: attachment.mediaType,
150
+ mediaType: 'video',
151
+ });
152
+ }
153
+
154
+ const type = _type || 'attachment';
155
+ requests.set(url, { type });
156
+ }));
157
+ }
158
+
138
159
  // Render cache hits immediately
139
- let attachmentHtml = '';
140
- let placeholderHtml = '';
141
160
  const cold = new Set();
142
161
  await Promise.all(Array.from(requests.keys()).map(async (url) => {
143
162
  const options = requests.get(url);
@@ -227,8 +246,8 @@ async function process(content, { type, pid, tid, attachments }) {
227
246
  }
228
247
 
229
248
  content = $.html();
230
- content += attachmentHtml ? `\n\n<div class="row"><div class="col-12">${attachmentHtml}</div></div>` : '';
231
- content += placeholderHtml ? `\n\n<div class="row"><div class="col-12">${placeholderHtml}</div></div>` : '';
249
+ content += attachmentHtml ? `\n\n<div class="row"><div class="col-12 mt-3">${attachmentHtml}</div></div>` : '';
250
+ content += placeholderHtml ? `\n\n<div class="row"><div class="col-12 mt-3">${placeholderHtml}</div></div>` : '';
232
251
  return content;
233
252
  }
234
253
 
@@ -280,25 +299,37 @@ async function handleSpecialEmbed(url, $anchor) {
280
299
  video = searchParams.get('v');
281
300
  }
282
301
  const html = await app.renderAsync(short ? 'partials/link-preview/youtube-short' : 'partials/link-preview/youtube', { video });
283
- $anchor.replaceWith(html);
284
302
 
285
- return true;
303
+ if ($anchor) {
304
+ $anchor.replaceWith(html);
305
+ return true;
306
+ }
307
+
308
+ return html;
286
309
  }
287
310
 
288
311
  if (embedVimeo === 'on' && hostname === 'vimeo.com') {
289
312
  const video = pathname.slice(1);
290
313
  const html = await app.renderAsync('partials/link-preview/vimeo', { video });
291
- $anchor.replaceWith(html);
292
314
 
293
- return true;
315
+ if ($anchor) {
316
+ $anchor.replaceWith(html);
317
+ return true;
318
+ }
319
+
320
+ return html;
294
321
  }
295
322
 
296
323
  if (embedTiktok === 'on' && ['tiktok.com', 'www.tiktok.com'].some(x => hostname === x)) {
297
324
  const video = pathname.split('/')[3];
298
325
  const html = await app.renderAsync('partials/link-preview/tiktok', { video });
299
- $anchor.replaceWith(html);
300
326
 
301
- return true;
327
+ if ($anchor) {
328
+ $anchor.replaceWith(html);
329
+ return true;
330
+ }
331
+
332
+ return html;
302
333
  }
303
334
 
304
335
  return false;
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "nodebb-plugin-link-preview",
3
- "version": "2.0.16",
3
+ "version": "2.1.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "nodebb-plugin-link-preview",
9
- "version": "2.0.16",
9
+ "version": "2.1.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "cheerio": "^1.0.0-rc.12",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-link-preview",
3
- "version": "2.0.16",
3
+ "version": "2.1.1",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -1,5 +1,5 @@
1
1
  <div class="ratio ratio-16x9">
2
- <video controls>
2
+ <video controls {{{ if width }}}width="{width}"{{{ end }}} {{{ if height }}}height="{height}"{{{ end }}}>
3
3
  <source src="{url}" type="{contentType}">
4
4
  </video>
5
5
  </div>