nodebb-plugin-link-preview 1.3.3 → 2.0.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.
Files changed (3) hide show
  1. package/library.js +27 -12
  2. package/package.json +4 -3
  3. package/plugin.json +3 -1
package/library.js CHANGED
@@ -9,8 +9,8 @@ const dns = require('dns');
9
9
 
10
10
  const { getLinkPreview } = require('link-preview-js');
11
11
  const { load } = require('cheerio');
12
+ const { isURL } = require('validator');
12
13
 
13
- const db = require.main.require('./src/database');
14
14
  const meta = require.main.require('./src/meta');
15
15
  const cache = require.main.require('./src/cache');
16
16
  const posts = require.main.require('./src/posts');
@@ -92,16 +92,11 @@ async function process(content, opts) {
92
92
 
93
93
  // Retrieve attachments
94
94
  if (opts.hasOwnProperty('pid') && await posts.exists(opts.pid)) {
95
- const hashes = await db.getSortedSetMembers(`post:${opts.pid}:attachments`);
96
- const keys = hashes.map(hash => `attachment:${hash}`);
97
- const attachments = (await db.getObjects(keys)).filter(Boolean);
98
- const urls = attachments
99
- // .filter(attachment => cache.has(`link-preview:${attachment.url}`))
100
- .map(attachment => attachment.url);
101
-
102
- urls.forEach(url => requests.set(url, {
103
- type: 'attachment',
104
- }));
95
+ const attachments = await posts.attachments.get(opts.pid);
96
+ attachments.forEach(({ url, _type }) => {
97
+ const type = _type || 'attachment';
98
+ requests.set(url, { type });
99
+ });
105
100
  }
106
101
 
107
102
  // Parse inline urls
@@ -127,6 +122,7 @@ async function process(content, opts) {
127
122
  continue;
128
123
  }
129
124
 
125
+ // Inline url takes precedence over attachment
130
126
  requests.set(url, {
131
127
  type: 'inline',
132
128
  target: $anchor,
@@ -150,7 +146,7 @@ async function process(content, opts) {
150
146
  }
151
147
 
152
148
  case 'attachment': {
153
- attachmentHtml += `<div class="col-6">${html}</div>`;
149
+ attachmentHtml += `<div class="col-lg-6">${html}</div>`;
154
150
  break;
155
151
  }
156
152
  }
@@ -302,6 +298,25 @@ plugin.onParse = async (payload) => {
302
298
  return payload;
303
299
  };
304
300
 
301
+ plugin.onPost = async ({ post }) => {
302
+ if (post._activitypub) {
303
+ return; // no attachment parsing for content from activitypub; attachments saved via notes.assert
304
+ }
305
+
306
+ // Only match standalone URLs on their own line
307
+ const lines = post.content.split('\n');
308
+ const urls = lines.filter(line => isURL(line));
309
+
310
+ let previews = await Promise.all(urls.map(async url => await preview(url)));
311
+ previews = previews.map(({ url, contentType: mediaType }) => ({
312
+ type: 'inline',
313
+ url,
314
+ mediaType,
315
+ })).filter(Boolean);
316
+
317
+ posts.attachments.update(post.pid, previews);
318
+ };
319
+
305
320
  plugin.addAdminNavigation = (header) => {
306
321
  header.plugins.push({
307
322
  route: '/plugins/link-preview',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-link-preview",
3
- "version": "1.3.3",
3
+ "version": "2.0.1",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "readmeFilename": "README.md",
33
33
  "nbbpm": {
34
- "compatibility": "^3.2.0"
34
+ "compatibility": "^4.0.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@commitlint/cli": "17.6.5",
@@ -44,6 +44,7 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "cheerio": "^1.0.0-rc.12",
47
- "link-preview-js": "^3.0.4"
47
+ "link-preview-js": "^3.0.4",
48
+ "validator": "^13.11.0"
48
49
  }
49
50
  }
package/plugin.json CHANGED
@@ -7,7 +7,9 @@
7
7
  { "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
8
8
  { "hook": "filter:settings.get", "method": "applyDefaults" },
9
9
  { "hook": "filter:parse.post", "method": "onParse" },
10
- { "hook": "filter:parse.raw", "method": "onParse" }
10
+ { "hook": "filter:parse.raw", "method": "onParse" },
11
+ { "hook": "action:post.save", "method": "onPost" },
12
+ { "hook": "action:post.edit", "method": "onPost" }
11
13
  ],
12
14
  "scss": [
13
15
  "static/scss/link-preview.scss"