@ecency/render-helper 2.5.38 → 2.5.39

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.
@@ -63,7 +63,7 @@ var INTERNAL_POST_TAG_REGEX = /^(.+?)\/(@[\w.\d-]+)\/(.*)$/i;
63
63
  var INTERNAL_POST_REGEX = /^\/(@[\w.\d-]+)\/(.*)$/i;
64
64
  var CUSTOM_COMMUNITY_REGEX = /^https?:\/\/(.*)\/c\/(hive-\d+)(.*)/i;
65
65
  var YOUTUBE_REGEX = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|shorts\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i;
66
- var YOUTUBE_EMBED_REGEX = /^(https?:)?\/\/www\.youtube\.com\/(embed|shorts)\/.*/i;
66
+ var YOUTUBE_EMBED_REGEX = /^(https?:)?\/\/(?:www\.|m\.)?youtube\.com\/(embed|shorts)\/.*/i;
67
67
  var VIMEO_REGEX = /(https?:\/\/)?(www\.)?(?:vimeo)\.com.*(?:videos|video|channels|)\/([\d]+)/i;
68
68
  var VIMEO_EMBED_REGEX = /https:\/\/player\.vimeo\.com\/video\/([0-9]+)(?:$|[?#])/;
69
69
  var BITCHUTE_REGEX = /^(?:https?:\/\/)?(?:www\.)?bitchute\.com\/(?:video|embed)\/([a-z0-9]+)/i;
@@ -417,8 +417,31 @@ function createDoc(html) {
417
417
  return null;
418
418
  }
419
419
  }
420
+ function hashBody(body) {
421
+ const s = typeof body === "string" ? body : "";
422
+ let h = 2166136261;
423
+ for (let i2 = 0; i2 < s.length; i2++) {
424
+ h ^= s.charCodeAt(i2);
425
+ h = Math.imul(h, 16777619);
426
+ }
427
+ return `${s.length}.${(h >>> 0).toString(36)}`;
428
+ }
429
+ var bodyHashes = /* @__PURE__ */ new WeakMap();
430
+ function entryBodyHash(entry) {
431
+ const body = entry.body;
432
+ if (typeof body !== "string" || typeof entry !== "object" || entry === null) {
433
+ return hashBody(body);
434
+ }
435
+ const stored = bodyHashes.get(entry);
436
+ if (stored && stored.body === body) {
437
+ return stored.hash;
438
+ }
439
+ const hash = hashBody(body);
440
+ bodyHashes.set(entry, { body, hash });
441
+ return hash;
442
+ }
420
443
  function makeEntryCacheKey(entry) {
421
- return `${entry.author}-${entry.permlink}-${entry.last_update}-${entry.updated}`;
444
+ return `${entry.author}-${entry.permlink}-${entry.last_update}-${entry.updated}-${entryBodyHash(entry)}`;
422
445
  }
423
446
  function stripHtmlTags(s) {
424
447
  const n = s.length;
@@ -510,6 +533,10 @@ function extractYtStartTime(url) {
510
533
  return "";
511
534
  }
512
535
  }
536
+ function youtubeVideoLinkClass(matchedUrl) {
537
+ const base = "markdown-video-link markdown-video-link-youtube";
538
+ return /youtube\.com\/shorts\//i.test(matchedUrl) ? `${base} markdown-video-link-youtube-portrait` : base;
539
+ }
513
540
  function sanitizePermlink(permlink) {
514
541
  if (!permlink || typeof permlink !== "string") {
515
542
  return "";
@@ -1311,7 +1338,8 @@ function a(el, forApp, parentDomain = "ecency.com", seoContext, renderOptions) {
1311
1338
  }
1312
1339
  let match = href.match(YOUTUBE_REGEX);
1313
1340
  if (match && match[1] && el.textContent.trim() === href) {
1314
- el.setAttribute("class", "markdown-video-link markdown-video-link-youtube");
1341
+ const videoClass = youtubeVideoLinkClass(match[0]);
1342
+ el.setAttribute("class", videoClass);
1315
1343
  el.removeAttribute("href");
1316
1344
  const vid = match[1];
1317
1345
  const thumbnail = proxifyImageSrc(`https://img.youtube.com/vi/${vid.split("?")[0]}/hqdefault.jpg`, 0, 0, "match");
@@ -1336,7 +1364,7 @@ function a(el, forApp, parentDomain = "ecency.com", seoContext, renderOptions) {
1336
1364
  iframe2.setAttribute("allowfullscreen", "");
1337
1365
  wrapper.appendChild(iframe2);
1338
1366
  el.appendChild(wrapper);
1339
- el.setAttribute("class", "markdown-video-link markdown-video-link-youtube er-youtube");
1367
+ el.setAttribute("class", `${videoClass} er-youtube`);
1340
1368
  } else {
1341
1369
  const thumbImg = el.ownerDocument.createElement("img");
1342
1370
  thumbImg.setAttribute("class", "no-replace video-thumbnail");
@@ -1608,8 +1636,20 @@ function iframe(el, parentDomain = "ecency.com", forApp = false, renderOptions)
1608
1636
  el.parentNode.removeChild(el);
1609
1637
  return;
1610
1638
  }
1611
- if (src.match(YOUTUBE_EMBED_REGEX)) {
1612
- el.setAttribute("src", stripQueryString(src));
1639
+ const ytMatch = src.match(YOUTUBE_EMBED_REGEX);
1640
+ if (ytMatch) {
1641
+ let stripped = stripQueryString(src);
1642
+ if (!/^(https?:)?\/\/www\./i.test(stripped)) {
1643
+ stripped = stripped.replace(/^(https?:)?\/\/(?:m\.)?youtube\.com/i, "https://www.youtube.com");
1644
+ }
1645
+ el.setAttribute("src", stripped);
1646
+ if (ytMatch[2].toLowerCase() === "shorts") {
1647
+ const shortId = stripped.match(/\/shorts\/([A-Za-z0-9_-]{11})\/?(?=[?#]|$)/i);
1648
+ if (shortId) {
1649
+ el.setAttribute("src", `https://www.youtube.com/embed/${shortId[1]}`);
1650
+ el.setAttribute("class", "portrait-embed");
1651
+ }
1652
+ }
1613
1653
  return;
1614
1654
  }
1615
1655
  if (src.match(BITCHUTE_REGEX)) {
@@ -1933,7 +1973,7 @@ function text(node, forApp, renderOptions) {
1933
1973
  const startTime = extractYtStartTime(nodeValue);
1934
1974
  const container = node.ownerDocument.createElement("p");
1935
1975
  const anchor = node.ownerDocument.createElement("a");
1936
- anchor.setAttribute("class", "markdown-video-link markdown-video-link-youtube");
1976
+ anchor.setAttribute("class", youtubeVideoLinkClass(e[0]));
1937
1977
  anchor.setAttribute("data-embed-src", embedSrc);
1938
1978
  anchor.setAttribute("data-youtube", vid);
1939
1979
  if (startTime) {
@@ -10216,11 +10256,16 @@ var cache = new LRUCache({ max: 500 });
10216
10256
  function setCacheSize(size) {
10217
10257
  cache = new LRUCache({ max: size });
10218
10258
  }
10219
- function cacheGet(key) {
10220
- return cache.get(key);
10259
+ var MEMO_MISS = /* @__PURE__ */ Symbol("memo-miss");
10260
+ function entryMemoGet(key, body, meta) {
10261
+ const slot = cache.get(key);
10262
+ if (slot === void 0 || slot.body !== body || slot.meta !== meta) {
10263
+ return MEMO_MISS;
10264
+ }
10265
+ return slot.value;
10221
10266
  }
10222
- function cacheSet(key, value) {
10223
- cache.set(key, value);
10267
+ function entryMemoSet(key, body, meta, value) {
10268
+ cache.set(key, { body, meta, value });
10224
10269
  }
10225
10270
 
10226
10271
  // src/markdown-2-html.ts
@@ -10245,8 +10290,8 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
10245
10290
  return res2;
10246
10291
  }
10247
10292
  const key = `${makeEntryCacheKey(obj)}-md-${forApp ? "app" : "site"}-${parentDomain}${seoContext ? `-seo${seoContext.authorReputation ?? ""}-${seoContext.postPayout ?? ""}` : ""}${renderOptions?.embedVideosDirectly ? "-embed" : ""}${renderOptions?.inertAuthorAndTagChips ? "-inert" : ""}${renderOptions?.externalProfileBase ? "-ext" + renderOptions.externalProfileBase : ""}`;
10248
- const item = cacheGet(key);
10249
- if (item) {
10293
+ const item = entryMemoGet(key, obj.body);
10294
+ if (item !== MEMO_MISS) {
10250
10295
  return item;
10251
10296
  }
10252
10297
  const cleanBody = cleanReply(obj.body);
@@ -10256,7 +10301,7 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
10256
10301
  performance.now() - t0,
10257
10302
  `author=@${obj.author} permlink=${obj.permlink} body_len=${obj.body?.length ?? 0}`
10258
10303
  );
10259
- cacheSet(key, res);
10304
+ entryMemoSet(key, obj.body, void 0, res);
10260
10305
  return res;
10261
10306
  }
10262
10307
 
@@ -10764,6 +10809,23 @@ function firstMetaUrl(value) {
10764
10809
  }
10765
10810
  return void 0;
10766
10811
  }
10812
+ function metaFingerprint(jsonMetadata) {
10813
+ let meta;
10814
+ if (typeof jsonMetadata === "object") {
10815
+ meta = jsonMetadata;
10816
+ } else {
10817
+ try {
10818
+ meta = JSON.parse(jsonMetadata);
10819
+ } catch {
10820
+ meta = null;
10821
+ }
10822
+ }
10823
+ try {
10824
+ return JSON.stringify([meta?.thumbnails, meta?.image]);
10825
+ } catch {
10826
+ return {};
10827
+ }
10828
+ }
10767
10829
  function proxifyFound(src, width, height, format) {
10768
10830
  return proxifyImageSrc(decodeEntities(src), width, height, format);
10769
10831
  }
@@ -10886,12 +10948,13 @@ function catchPostImage(obj, width = 0, height = 0, format = "match", options =
10886
10948
  return null;
10887
10949
  }
10888
10950
  const key = `${makeEntryCacheKey(obj)}-${width}x${height}-${format}${fastMode ? "-fast" : ""}`;
10889
- const item = cacheGet(key);
10890
- if (item !== void 0) {
10951
+ const meta = metaFingerprint(obj.json_metadata);
10952
+ const item = entryMemoGet(key, obj.body, meta);
10953
+ if (item !== MEMO_MISS) {
10891
10954
  return item;
10892
10955
  }
10893
10956
  const res = getImage(obj, width, height, format, fastMode);
10894
- cacheSet(key, res);
10957
+ entryMemoSet(key, obj.body, meta, res);
10895
10958
  return res;
10896
10959
  }
10897
10960
 
@@ -10978,12 +11041,12 @@ function getPostBodySummary(obj, length, platform) {
10978
11041
  return postBodySummary(obj, normalizedLength, normalizedPlatform);
10979
11042
  }
10980
11043
  const key = `${makeEntryCacheKey(obj)}-sum-${normalizedLength}-${normalizedPlatform}`;
10981
- const item = cacheGet(key);
10982
- if (item) {
11044
+ const item = entryMemoGet(key, obj.body);
11045
+ if (item !== MEMO_MISS) {
10983
11046
  return item;
10984
11047
  }
10985
11048
  const res = postBodySummary(obj.body, normalizedLength, normalizedPlatform);
10986
- cacheSet(key, res);
11049
+ entryMemoSet(key, obj.body, void 0, res);
10987
11050
  return res;
10988
11051
  }
10989
11052