nodebb-plugin-link-preview 2.0.2 → 2.0.4
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 +31 -21
- package/package.json +1 -1
package/library.js
CHANGED
|
@@ -82,7 +82,9 @@ async function preview(url) {
|
|
|
82
82
|
});
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
async function process(content,
|
|
85
|
+
async function process(content, { type, pid, tid }) {
|
|
86
|
+
const inlineTypes = ['default', 'activitypub.article'];
|
|
87
|
+
const processInline = inlineTypes.includes(type);
|
|
86
88
|
const { embedHtml, embedImage, embedAudio, embedVideo } = await meta.settings.get('link-preview');
|
|
87
89
|
if (![embedHtml, embedImage, embedAudio, embedVideo].some(prop => prop === 'on')) {
|
|
88
90
|
return content;
|
|
@@ -91,8 +93,8 @@ async function process(content, opts) {
|
|
|
91
93
|
const requests = new Map();
|
|
92
94
|
|
|
93
95
|
// Retrieve attachments
|
|
94
|
-
if (
|
|
95
|
-
const attachments = await posts.attachments.get(
|
|
96
|
+
if (pid && await posts.exists(pid)) {
|
|
97
|
+
const attachments = await posts.attachments.get(pid);
|
|
96
98
|
attachments.forEach(({ url, _type }) => {
|
|
97
99
|
const type = _type || 'attachment';
|
|
98
100
|
requests.set(url, { type });
|
|
@@ -117,9 +119,11 @@ async function process(content, opts) {
|
|
|
117
119
|
url = `${nconf.get('url')}${url.startsWith('/') ? url : `/${url}`}`;
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
if (!processInline) {
|
|
123
|
+
const special = await handleSpecialEmbed(url, $anchor);
|
|
124
|
+
if (special) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
123
127
|
}
|
|
124
128
|
|
|
125
129
|
// Inline url takes precedence over attachment
|
|
@@ -140,13 +144,15 @@ async function process(content, opts) {
|
|
|
140
144
|
if (html) {
|
|
141
145
|
switch (options.type) {
|
|
142
146
|
case 'inline': {
|
|
143
|
-
|
|
144
|
-
|
|
147
|
+
if (processInline) {
|
|
148
|
+
const $anchor = options.target;
|
|
149
|
+
$anchor.replaceWith($(html));
|
|
150
|
+
}
|
|
145
151
|
break;
|
|
146
152
|
}
|
|
147
153
|
|
|
148
154
|
case 'attachment': {
|
|
149
|
-
attachmentHtml +=
|
|
155
|
+
attachmentHtml += html;
|
|
150
156
|
break;
|
|
151
157
|
}
|
|
152
158
|
}
|
|
@@ -174,13 +180,15 @@ async function process(content, opts) {
|
|
|
174
180
|
if (html) {
|
|
175
181
|
switch (options.type) {
|
|
176
182
|
case 'inline': {
|
|
177
|
-
|
|
178
|
-
|
|
183
|
+
if (processInline) {
|
|
184
|
+
const $anchor = options.target;
|
|
185
|
+
$anchor.replaceWith($(html));
|
|
186
|
+
}
|
|
179
187
|
break;
|
|
180
188
|
}
|
|
181
189
|
|
|
182
190
|
case 'attachment': {
|
|
183
|
-
attachmentHtml +=
|
|
191
|
+
attachmentHtml += html;
|
|
184
192
|
break;
|
|
185
193
|
}
|
|
186
194
|
}
|
|
@@ -191,15 +199,15 @@ async function process(content, opts) {
|
|
|
191
199
|
content += attachmentHtml ? `\n\n<div class="row">${attachmentHtml}</div>` : '';
|
|
192
200
|
|
|
193
201
|
// bust posts cache item
|
|
194
|
-
if (
|
|
195
|
-
|
|
202
|
+
if (pid && await posts.exists(pid)) {
|
|
203
|
+
posts.clearCachedPost(pid);
|
|
196
204
|
|
|
197
205
|
// fire post edit event with mocked data
|
|
198
|
-
if (
|
|
199
|
-
websockets.in(`topic_${
|
|
206
|
+
if (tid && await topics.exists(tid)) {
|
|
207
|
+
websockets.in(`topic_${tid}`).emit('event:post_edited', {
|
|
200
208
|
post: {
|
|
201
|
-
tid
|
|
202
|
-
pid
|
|
209
|
+
tid,
|
|
210
|
+
pid,
|
|
203
211
|
changed: true,
|
|
204
212
|
content,
|
|
205
213
|
},
|
|
@@ -211,7 +219,7 @@ async function process(content, opts) {
|
|
|
211
219
|
}
|
|
212
220
|
|
|
213
221
|
content = $.html();
|
|
214
|
-
content += attachmentHtml ? `\n\n<div class="row">${attachmentHtml}</div>` : '';
|
|
222
|
+
content += attachmentHtml ? `\n\n<div class="row"><div class="col-12">${attachmentHtml}</div></div>` : '';
|
|
215
223
|
return content;
|
|
216
224
|
}
|
|
217
225
|
|
|
@@ -289,10 +297,12 @@ async function handleSpecialEmbed(url, $anchor) {
|
|
|
289
297
|
|
|
290
298
|
plugin.onParse = async (payload) => {
|
|
291
299
|
if (typeof payload === 'string') { // raw
|
|
292
|
-
|
|
300
|
+
const type = 'default';
|
|
301
|
+
payload = await process(payload, { type });
|
|
293
302
|
} else if (payload && payload.postData && payload.postData.content) { // post
|
|
294
303
|
const { content, pid, tid } = payload.postData;
|
|
295
|
-
|
|
304
|
+
const { type } = payload;
|
|
305
|
+
payload.postData.content = await process(content, { type, pid, tid });
|
|
296
306
|
}
|
|
297
307
|
|
|
298
308
|
return payload;
|