nodebb-plugin-link-preview 1.1.1 → 1.2.0

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
@@ -4,6 +4,7 @@
4
4
  'use strict';
5
5
 
6
6
  const winston = require.main.require('winston');
7
+ const nconf = require.main.require('nconf');
7
8
  const dns = require('dns');
8
9
 
9
10
  const { getLinkPreview } = require('link-preview-js');
@@ -53,13 +54,18 @@ async function process(content, opts) {
53
54
  const $anchor = $(anchor);
54
55
 
55
56
  // Skip if the anchor has link text, or has text on the same line.
56
- const url = $anchor.attr('href');
57
+ let url = $anchor.attr('href');
57
58
  const text = $anchor.text();
58
59
  const hasSiblings = !!anchor.prev || !!anchor.next;
59
60
  if (hasSiblings || url !== text) {
60
61
  continue;
61
62
  }
62
63
 
64
+ // Handle relative URLs
65
+ if (!url.startsWith('http')) {
66
+ url = `${nconf.get('url')}${url.startsWith('/') ? url : `/${url}`}`;
67
+ }
68
+
63
69
  const special = await handleSpecialEmbed(url, $anchor);
64
70
  if (special) {
65
71
  continue;
@@ -67,8 +73,9 @@ async function process(content, opts) {
67
73
 
68
74
  const cached = cache.get(`link-preview:${url}`);
69
75
  if (cached) {
70
- if (cached.contentType) {
71
- $anchor.replaceWith($(await render(cached)));
76
+ const html = await render(cached);
77
+ if (html) {
78
+ $anchor.replaceWith($(html));
72
79
  }
73
80
  continue;
74
81
  }
@@ -107,13 +114,18 @@ async function process(content, opts) {
107
114
  winston.verbose(`[link-preview] ${preview.url} (${preview.contentType}, cache: miss)`);
108
115
  cache.set(`link-preview:${url}`, preview);
109
116
 
117
+ const html = await render(preview);
118
+ if (!html) {
119
+ return;
120
+ }
121
+
110
122
  // bust posts cache item
111
123
  if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
112
124
  postsCache.del(String(opts.pid));
113
125
 
114
126
  // fire post edit event with mocked data
115
127
  if (opts.hasOwnProperty('tid') && await topics.exists(opts.tid)) {
116
- $anchor.replaceWith($(await render(preview)));
128
+ $anchor.replaceWith($(html));
117
129
  websockets.in(`topic_${opts.tid}`).emit('event:post_edited', {
118
130
  post: {
119
131
  tid: opts.tid,
@@ -166,7 +178,7 @@ async function render(preview) {
166
178
  async function handleSpecialEmbed(url, $anchor) {
167
179
  const { app } = require.main.require('./src/webserver');
168
180
  const { hostname, searchParams, pathname } = new URL(url);
169
- const { embedYoutube, embedVimeo } = await meta.settings.get('link-preview');
181
+ const { embedYoutube, embedVimeo, embedTiktok } = await meta.settings.get('link-preview');
170
182
 
171
183
  if (embedYoutube === 'on' && ['youtube.com', 'www.youtube.com', 'youtu.be'].some(x => hostname === x)) {
172
184
  let video;
@@ -189,6 +201,14 @@ async function handleSpecialEmbed(url, $anchor) {
189
201
  return true;
190
202
  }
191
203
 
204
+ if (embedTiktok === 'on' && ['tiktok.com', 'www.tiktok.com'].some(x => hostname === x)) {
205
+ const video = pathname.split('/')[3];
206
+ const html = await app.renderAsync('partials/link-preview/tiktok', { video });
207
+ $anchor.replaceWith(html);
208
+
209
+ return true;
210
+ }
211
+
192
212
  return false;
193
213
  }
194
214
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-link-preview",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -54,6 +54,11 @@
54
54
  <input type="checkbox" class="form-check-input" id="embedVimeo" name="embedVimeo">
55
55
  <label for="embedVimeo" class="form-check-label">Vimeo</label>
56
56
  </div>
57
+
58
+ <div class="form-check form-switch mb-3">
59
+ <input type="checkbox" class="form-check-input" id="embedTiktok" name="embedTiktok">
60
+ <label for="embedTiktok" class="form-check-label">Vimeo</label>
61
+ </div>
57
62
  </div>
58
63
  </form>
59
64
  </div>
@@ -0,0 +1,3 @@
1
+ <div class="ratio" style="--bs-aspect-ratio: 200%;">
2
+ <iframe src="https://www.tiktok.com/embed/v2/{video}" title="TikTok video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
3
+ </div>
@@ -1 +1,3 @@
1
- <iframe src="https://player.vimeo.com/video/{video}?h=65e20fad6f&title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>
1
+ <div class="ratio ratio-16x9">
2
+ <iframe src="https://player.vimeo.com/video/{video}?h=65e20fad6f&title=0&byline=0&portrait=0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>
3
+ </div>
@@ -1 +1,3 @@
1
- <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/{video}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
1
+ <div class="ratio ratio-16x9">
2
+ <iframe src="https://www.youtube-nocookie.com/embed/{video}" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
3
+ </div>