@ecency/render-helper 2.4.12 → 2.4.14

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.
@@ -4,7 +4,7 @@ import multihash from 'multihashes';
4
4
  import querystring from 'querystring';
5
5
  import { Remarkable } from 'remarkable';
6
6
  import { linkify as linkify$1 } from 'remarkable/linkify';
7
- import he from 'he';
7
+ import he2 from 'he';
8
8
  import * as htmlparser2 from 'htmlparser2';
9
9
  import * as domSerializerModule from 'dom-serializer';
10
10
  import { LRUCache } from 'lru-cache';
@@ -164,10 +164,11 @@ var ALLOWED_ATTRIBUTES = {
164
164
  "del": [],
165
165
  "ins": []
166
166
  };
167
- var lenientErrorHandler = (level, msg) => {
168
- if (process.env.NODE_ENV === "development" && level === "fatalError") {
167
+ var lenientErrorHandler = (level, msg, context) => {
168
+ if (process.env.NODE_ENV === "development") {
169
169
  console.warn("[DOMParser]", level, msg);
170
170
  }
171
+ return void 0;
171
172
  };
172
173
  var DOMParser = new DOMParser$1({
173
174
  // Use onError instead of deprecated errorHandler
@@ -176,11 +177,30 @@ var DOMParser = new DOMParser$1({
176
177
  });
177
178
 
178
179
  // src/helper.ts
180
+ function removeDuplicateAttributes(html) {
181
+ const tagRegex = /<([a-zA-Z][a-zA-Z0-9]*)\s+((?:[^>"']+|"[^"]*"|'[^']*')*?)\s*(\/?)>/g;
182
+ return html.replace(tagRegex, (match, tagName, attrsString, selfClose) => {
183
+ const seenAttrs = /* @__PURE__ */ new Set();
184
+ const cleanedAttrs = [];
185
+ const attrRegex = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*(?:=\s*(?:"[^"]*"|'[^']*'|[^\s/>]+))?/g;
186
+ let attrMatch;
187
+ while ((attrMatch = attrRegex.exec(attrsString)) !== null) {
188
+ const attrName = attrMatch[1].toLowerCase();
189
+ if (!seenAttrs.has(attrName)) {
190
+ seenAttrs.add(attrName);
191
+ cleanedAttrs.push(attrMatch[0]);
192
+ }
193
+ }
194
+ const attrsJoined = cleanedAttrs.length > 0 ? ` ${cleanedAttrs.join(" ")}` : "";
195
+ return `<${tagName}${attrsJoined}${selfClose ? " /" : ""}>`;
196
+ });
197
+ }
179
198
  function createDoc(html) {
180
199
  if (html.trim() === "") {
181
200
  return null;
182
201
  }
183
- const doc = DOMParser.parseFromString(`<body>${html}</body>`, "text/html");
202
+ const cleanedHtml = removeDuplicateAttributes(html);
203
+ const doc = DOMParser.parseFromString(`<body>${cleanedHtml}</body>`, "text/html");
184
204
  return doc;
185
205
  }
186
206
  function makeEntryCacheKey(entry) {
@@ -1387,7 +1407,7 @@ function markdownToHTML(input, forApp, webp, parentDomain = "ecency.com") {
1387
1407
  try {
1388
1408
  output = md.render(input);
1389
1409
  output = fixBlockLevelTagsInParagraphs(output);
1390
- const doc = DOMParser.parseFromString(`<body id="root">${output}</body>`, "text/html");
1410
+ const doc = DOMParser.parseFromString(`<body id="root">${removeDuplicateAttributes(output)}</body>`, "text/html");
1391
1411
  traverse(doc, forApp, 0, webp, { firstImageFound: false }, parentDomain);
1392
1412
  output = serializer.serializeToString(doc);
1393
1413
  } catch (error) {
@@ -1400,11 +1420,11 @@ function markdownToHTML(input, forApp, webp, parentDomain = "ecency.com") {
1400
1420
  lowerCaseAttributeNames: false
1401
1421
  });
1402
1422
  const repairedHtml = domSerializer(dom.children);
1403
- const doc = DOMParser.parseFromString(`<body id="root">${repairedHtml}</body>`, "text/html");
1423
+ const doc = DOMParser.parseFromString(`<body id="root">${removeDuplicateAttributes(repairedHtml)}</body>`, "text/html");
1404
1424
  traverse(doc, forApp, 0, webp, { firstImageFound: false }, parentDomain);
1405
1425
  output = serializer.serializeToString(doc);
1406
1426
  } catch (fallbackError) {
1407
- const escapedContent = he.encode(output || md.render(input));
1427
+ const escapedContent = he2.encode(output || md.render(input));
1408
1428
  output = `<p dir="auto">${escapedContent}</p>`;
1409
1429
  }
1410
1430
  }
@@ -1444,8 +1464,6 @@ function markdown2Html(obj, forApp = true, webp = false, parentDomain = "ecency.
1444
1464
  cacheSet(key, res);
1445
1465
  return res;
1446
1466
  }
1447
-
1448
- // src/catch-post-image.ts
1449
1467
  var gifLinkRegex = /\.(gif)$/i;
1450
1468
  function isGifLink(link) {
1451
1469
  return gifLinkRegex.test(link);
@@ -1462,12 +1480,20 @@ function getImage(entry, width = 0, height = 0, format = "match") {
1462
1480
  }
1463
1481
  }
1464
1482
  if (meta && typeof meta.image === "string" && meta.image.length > 0) {
1465
- if (isGifLink(meta.image)) {
1466
- return proxifyImageSrc(meta.image, 0, 0, format);
1483
+ const decodedImage = he2.decode(meta.image);
1484
+ if (isGifLink(decodedImage)) {
1485
+ return proxifyImageSrc(decodedImage, 0, 0, format);
1467
1486
  }
1468
- return proxifyImageSrc(meta.image, width, height, format);
1487
+ return proxifyImageSrc(decodedImage, width, height, format);
1469
1488
  }
1470
1489
  if (meta && meta.image && !!meta.image.length && meta.image[0]) {
1490
+ if (typeof meta.image[0] === "string") {
1491
+ const decodedImage = he2.decode(meta.image[0]);
1492
+ if (isGifLink(decodedImage)) {
1493
+ return proxifyImageSrc(decodedImage, 0, 0, format);
1494
+ }
1495
+ return proxifyImageSrc(decodedImage, width, height, format);
1496
+ }
1471
1497
  if (isGifLink(meta.image[0])) {
1472
1498
  return proxifyImageSrc(meta.image[0], 0, 0, format);
1473
1499
  }
@@ -1484,10 +1510,11 @@ function getImage(entry, width = 0, height = 0, format = "match") {
1484
1510
  if (!src) {
1485
1511
  return null;
1486
1512
  }
1487
- if (isGifLink(src)) {
1488
- return proxifyImageSrc(src, 0, 0, format);
1513
+ const decodedSrc = he2.decode(src);
1514
+ if (isGifLink(decodedSrc)) {
1515
+ return proxifyImageSrc(decodedSrc, 0, 0, format);
1489
1516
  }
1490
- return proxifyImageSrc(src, width, height, format);
1517
+ return proxifyImageSrc(decodedSrc, width, height, format);
1491
1518
  }
1492
1519
  return null;
1493
1520
  }
@@ -1504,10 +1531,11 @@ function catchPostImage(obj, width = 0, height = 0, format = "match") {
1504
1531
  if (!src) {
1505
1532
  return null;
1506
1533
  }
1507
- if (isGifLink(src)) {
1508
- return proxifyImageSrc(src, 0, 0, format);
1534
+ const decodedSrc = he2.decode(src);
1535
+ if (isGifLink(decodedSrc)) {
1536
+ return proxifyImageSrc(decodedSrc, 0, 0, format);
1509
1537
  }
1510
- return proxifyImageSrc(src, width, height, format);
1538
+ return proxifyImageSrc(decodedSrc, width, height, format);
1511
1539
  }
1512
1540
  return null;
1513
1541
  }
@@ -1596,7 +1624,7 @@ function postBodySummary(entryBody, length = 200, platform = "web") {
1596
1624
  text2 = joint(text2.split(" "), length);
1597
1625
  }
1598
1626
  if (text2) {
1599
- text2 = he.decode(text2);
1627
+ text2 = he2.decode(text2);
1600
1628
  }
1601
1629
  return text2;
1602
1630
  }