nodebb-plugin-mentions 4.6.1 → 4.6.2

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/library.js +29 -24
  2. package/package.json +4 -6
package/library.js CHANGED
@@ -5,7 +5,6 @@
5
5
  const _ = require('lodash');
6
6
  const validator = require('validator');
7
7
  const entitiesDecode = require('html-entities').decode;
8
- const cheerio = require('cheerio');
9
8
 
10
9
  const nconf = require.main.require('nconf');
11
10
  const winston = require.main.require('winston');
@@ -37,7 +36,7 @@ const parts = {
37
36
  };
38
37
  const regex = RegExp(`${parts.before}${parts.main}`, 'gu');
39
38
  const isLatinMention = /@[\w\d\-_.@]+$/;
40
- const hasAnchors = /<a.*>.*<\/a>/i;
39
+ const anchorRegex = /<a.*?href=['"](.+?)['"].*?>(.*?)<\/a>/ig;
41
40
 
42
41
  const Mentions = module.exports;
43
42
 
@@ -357,23 +356,24 @@ async function getMatches(content, isMarkdown = false) {
357
356
  }
358
357
  });
359
358
 
359
+ // Early return
360
+ if (isMarkdown) {
361
+ return { splitContent, matches, urlMap: new Map() };
362
+ }
363
+
364
+ // Find matches via backreferenced href
360
365
  const joined = splitContent.join('');
361
- const parseAnchors = joined.match(hasAnchors);
366
+ const anchors = new Set(joined.matchAll(anchorRegex));
362
367
  const urlMap = new Map();
363
- if (!isMarkdown && parseAnchors) {
364
- const $ = cheerio.load(splitContent.join(''));
365
- const anchors = $('a');
366
- const urls = new Set();
367
- Array.from(anchors).forEach((anchor) => {
368
- const text = $(anchor).prop('innerText');
369
- const match = text.match(regex);
370
- if (match) {
371
- urls.add($(anchor).attr('href'));
372
- }
368
+ const urls = new Set();
369
+ if (!isMarkdown && anchors.size) {
370
+ anchors.forEach((match) => {
371
+ const [, url] = match;
372
+ urls.add(url);
373
373
  });
374
374
 
375
375
  // Filter out urls that don't backreference to a remote id
376
- const backrefs = await db.getObjectFields('remoteUrl:uid', Array.from(urls));
376
+ const backrefs = urls.size ? await db.getObjectFields('remoteUrl:uid', Array.from(urls)) : {};
377
377
  const urlAsIdExists = await db.isSortedSetMembers('usersRemote:lastCrawled', Array.from(urls));
378
378
  Array.from(urls).map(async (url, index) => {
379
379
  if (backrefs[url] || urlAsIdExists[index]) {
@@ -502,20 +502,25 @@ Mentions.parseRaw = async (content, type = 'default') => {
502
502
  }
503
503
  }));
504
504
 
505
- const parsed = splitContent.join('');
505
+ let parsed = splitContent.join('');
506
+
507
+ // Early return if no urls to modify
508
+ if (!urlMap.size) {
509
+ return parsed;
510
+ }
506
511
 
507
512
  // Modify existing anchors to local profile
508
- const $ = cheerio.load(parsed);
509
- const anchors = $('a');
510
- Array.from(anchors).forEach((anchor) => {
511
- const $anchor = $(anchor);
512
- const url = $anchor.attr('href');
513
- if (urlMap.has(url)) {
514
- $anchor.attr('href', urlMap.get(url));
515
- }
513
+ const anchors = new Set(Array.from(parsed.matchAll(anchorRegex)).reverse());
514
+ if (!anchors.size) {
515
+ return parsed;
516
+ }
517
+
518
+ anchors.forEach(([match, href]) => {
519
+ const replacement = match.replace(href, urlMap.get(href));
520
+ parsed = parsed.split(match).join(replacement);
516
521
  });
517
522
 
518
- return $.html();
523
+ return parsed;
519
524
  };
520
525
 
521
526
  Mentions.clean = function (input, isMarkdown, stripBlockquote, stripCode) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-mentions",
3
- "version": "4.6.1",
3
+ "version": "4.6.2",
4
4
  "description": "NodeBB Plugin that allows users to mention other users by prepending an '@' sign to their username",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -22,16 +22,14 @@
22
22
  "compatibility": "^4.0.0"
23
23
  },
24
24
  "dependencies": {
25
- "cheerio": "^1.0.0-rc.12",
26
25
  "html-entities": "^2.3.2",
27
26
  "lodash": "4.17.21",
28
- "sanitize-html": "^2.13.0",
29
27
  "validator": "^13.0.0"
30
28
  },
31
29
  "devDependencies": {
32
- "mocha": "10.4.0",
33
- "eslint": "9.3.0",
30
+ "eslint": "^8.0.0",
34
31
  "eslint-config-nodebb": "0.2.1",
35
- "eslint-plugin-import": "2.29.1"
32
+ "eslint-plugin-import": "2.29.1",
33
+ "mocha": "10.4.0"
36
34
  }
37
35
  }