@ecency/render-helper 2.5.37 → 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.
@@ -65,7 +65,7 @@ var INTERNAL_POST_TAG_REGEX = /^(.+?)\/(@[\w.\d-]+)\/(.*)$/i;
65
65
  var INTERNAL_POST_REGEX = /^\/(@[\w.\d-]+)\/(.*)$/i;
66
66
  var CUSTOM_COMMUNITY_REGEX = /^https?:\/\/(.*)\/c\/(hive-\d+)(.*)/i;
67
67
  var YOUTUBE_REGEX = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|shorts\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i;
68
- var YOUTUBE_EMBED_REGEX = /^(https?:)?\/\/www\.youtube\.com\/(embed|shorts)\/.*/i;
68
+ var YOUTUBE_EMBED_REGEX = /^(https?:)?\/\/(?:www\.|m\.)?youtube\.com\/(embed|shorts)\/.*/i;
69
69
  var VIMEO_REGEX = /(https?:\/\/)?(www\.)?(?:vimeo)\.com.*(?:videos|video|channels|)\/([\d]+)/i;
70
70
  var VIMEO_EMBED_REGEX = /https:\/\/player\.vimeo\.com\/video\/([0-9]+)(?:$|[?#])/;
71
71
  var BITCHUTE_REGEX = /^(?:https?:\/\/)?(?:www\.)?bitchute\.com\/(?:video|embed)\/([a-z0-9]+)/i;
@@ -419,8 +419,31 @@ function createDoc(html) {
419
419
  return null;
420
420
  }
421
421
  }
422
+ function hashBody(body) {
423
+ const s = typeof body === "string" ? body : "";
424
+ let h = 2166136261;
425
+ for (let i = 0; i < s.length; i++) {
426
+ h ^= s.charCodeAt(i);
427
+ h = Math.imul(h, 16777619);
428
+ }
429
+ return `${s.length}.${(h >>> 0).toString(36)}`;
430
+ }
431
+ var bodyHashes = /* @__PURE__ */ new WeakMap();
432
+ function entryBodyHash(entry) {
433
+ const body = entry.body;
434
+ if (typeof body !== "string" || typeof entry !== "object" || entry === null) {
435
+ return hashBody(body);
436
+ }
437
+ const stored = bodyHashes.get(entry);
438
+ if (stored && stored.body === body) {
439
+ return stored.hash;
440
+ }
441
+ const hash = hashBody(body);
442
+ bodyHashes.set(entry, { body, hash });
443
+ return hash;
444
+ }
422
445
  function makeEntryCacheKey(entry) {
423
- return `${entry.author}-${entry.permlink}-${entry.last_update}-${entry.updated}`;
446
+ return `${entry.author}-${entry.permlink}-${entry.last_update}-${entry.updated}-${entryBodyHash(entry)}`;
424
447
  }
425
448
  function stripHtmlTags(s) {
426
449
  const n = s.length;
@@ -512,6 +535,10 @@ function extractYtStartTime(url) {
512
535
  return "";
513
536
  }
514
537
  }
538
+ function youtubeVideoLinkClass(matchedUrl) {
539
+ const base = "markdown-video-link markdown-video-link-youtube";
540
+ return /youtube\.com\/shorts\//i.test(matchedUrl) ? `${base} markdown-video-link-youtube-portrait` : base;
541
+ }
515
542
  function sanitizePermlink(permlink) {
516
543
  if (!permlink || typeof permlink !== "string") {
517
544
  return "";
@@ -1313,7 +1340,8 @@ function a(el, forApp, parentDomain = "ecency.com", seoContext, renderOptions) {
1313
1340
  }
1314
1341
  let match = href.match(YOUTUBE_REGEX);
1315
1342
  if (match && match[1] && el.textContent.trim() === href) {
1316
- el.setAttribute("class", "markdown-video-link markdown-video-link-youtube");
1343
+ const videoClass = youtubeVideoLinkClass(match[0]);
1344
+ el.setAttribute("class", videoClass);
1317
1345
  el.removeAttribute("href");
1318
1346
  const vid = match[1];
1319
1347
  const thumbnail = proxifyImageSrc(`https://img.youtube.com/vi/${vid.split("?")[0]}/hqdefault.jpg`, 0, 0, "match");
@@ -1338,7 +1366,7 @@ function a(el, forApp, parentDomain = "ecency.com", seoContext, renderOptions) {
1338
1366
  iframe2.setAttribute("allowfullscreen", "");
1339
1367
  wrapper.appendChild(iframe2);
1340
1368
  el.appendChild(wrapper);
1341
- el.setAttribute("class", "markdown-video-link markdown-video-link-youtube er-youtube");
1369
+ el.setAttribute("class", `${videoClass} er-youtube`);
1342
1370
  } else {
1343
1371
  const thumbImg = el.ownerDocument.createElement("img");
1344
1372
  thumbImg.setAttribute("class", "no-replace video-thumbnail");
@@ -1610,8 +1638,20 @@ function iframe(el, parentDomain = "ecency.com", forApp = false, renderOptions)
1610
1638
  el.parentNode.removeChild(el);
1611
1639
  return;
1612
1640
  }
1613
- if (src.match(YOUTUBE_EMBED_REGEX)) {
1614
- el.setAttribute("src", stripQueryString(src));
1641
+ const ytMatch = src.match(YOUTUBE_EMBED_REGEX);
1642
+ if (ytMatch) {
1643
+ let stripped = stripQueryString(src);
1644
+ if (!/^(https?:)?\/\/www\./i.test(stripped)) {
1645
+ stripped = stripped.replace(/^(https?:)?\/\/(?:m\.)?youtube\.com/i, "https://www.youtube.com");
1646
+ }
1647
+ el.setAttribute("src", stripped);
1648
+ if (ytMatch[2].toLowerCase() === "shorts") {
1649
+ const shortId = stripped.match(/\/shorts\/([A-Za-z0-9_-]{11})\/?(?=[?#]|$)/i);
1650
+ if (shortId) {
1651
+ el.setAttribute("src", `https://www.youtube.com/embed/${shortId[1]}`);
1652
+ el.setAttribute("class", "portrait-embed");
1653
+ }
1654
+ }
1615
1655
  return;
1616
1656
  }
1617
1657
  if (src.match(BITCHUTE_REGEX)) {
@@ -1935,7 +1975,7 @@ function text(node, forApp, renderOptions) {
1935
1975
  const startTime = extractYtStartTime(nodeValue);
1936
1976
  const container = node.ownerDocument.createElement("p");
1937
1977
  const anchor = node.ownerDocument.createElement("a");
1938
- anchor.setAttribute("class", "markdown-video-link markdown-video-link-youtube");
1978
+ anchor.setAttribute("class", youtubeVideoLinkClass(e[0]));
1939
1979
  anchor.setAttribute("data-embed-src", embedSrc);
1940
1980
  anchor.setAttribute("data-youtube", vid);
1941
1981
  if (startTime) {
@@ -2171,11 +2211,16 @@ var cache = new LRUCache({ max: 500 });
2171
2211
  function setCacheSize(size) {
2172
2212
  cache = new LRUCache({ max: size });
2173
2213
  }
2174
- function cacheGet(key) {
2175
- return cache.get(key);
2214
+ var MEMO_MISS = /* @__PURE__ */ Symbol("memo-miss");
2215
+ function entryMemoGet(key, body, meta) {
2216
+ const slot = cache.get(key);
2217
+ if (slot === void 0 || slot.body !== body || slot.meta !== meta) {
2218
+ return MEMO_MISS;
2219
+ }
2220
+ return slot.value;
2176
2221
  }
2177
- function cacheSet(key, value) {
2178
- cache.set(key, value);
2222
+ function entryMemoSet(key, body, meta, value) {
2223
+ cache.set(key, { body, meta, value });
2179
2224
  }
2180
2225
 
2181
2226
  // src/markdown-2-html.ts
@@ -2200,8 +2245,8 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
2200
2245
  return res2;
2201
2246
  }
2202
2247
  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 : ""}`;
2203
- const item = cacheGet(key);
2204
- if (item) {
2248
+ const item = entryMemoGet(key, obj.body);
2249
+ if (item !== MEMO_MISS) {
2205
2250
  return item;
2206
2251
  }
2207
2252
  const cleanBody = cleanReply(obj.body);
@@ -2211,7 +2256,7 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
2211
2256
  performance.now() - t0,
2212
2257
  `author=@${obj.author} permlink=${obj.permlink} body_len=${obj.body?.length ?? 0}`
2213
2258
  );
2214
- cacheSet(key, res);
2259
+ entryMemoSet(key, obj.body, void 0, res);
2215
2260
  return res;
2216
2261
  }
2217
2262
 
@@ -2719,6 +2764,23 @@ function firstMetaUrl(value) {
2719
2764
  }
2720
2765
  return void 0;
2721
2766
  }
2767
+ function metaFingerprint(jsonMetadata) {
2768
+ let meta;
2769
+ if (typeof jsonMetadata === "object") {
2770
+ meta = jsonMetadata;
2771
+ } else {
2772
+ try {
2773
+ meta = JSON.parse(jsonMetadata);
2774
+ } catch {
2775
+ meta = null;
2776
+ }
2777
+ }
2778
+ try {
2779
+ return JSON.stringify([meta?.thumbnails, meta?.image]);
2780
+ } catch {
2781
+ return {};
2782
+ }
2783
+ }
2722
2784
  function proxifyFound(src, width, height, format) {
2723
2785
  return proxifyImageSrc(decodeEntities(src), width, height, format);
2724
2786
  }
@@ -2841,12 +2903,13 @@ function catchPostImage(obj, width = 0, height = 0, format = "match", options =
2841
2903
  return null;
2842
2904
  }
2843
2905
  const key = `${makeEntryCacheKey(obj)}-${width}x${height}-${format}${fastMode ? "-fast" : ""}`;
2844
- const item = cacheGet(key);
2845
- if (item !== void 0) {
2906
+ const meta = metaFingerprint(obj.json_metadata);
2907
+ const item = entryMemoGet(key, obj.body, meta);
2908
+ if (item !== MEMO_MISS) {
2846
2909
  return item;
2847
2910
  }
2848
2911
  const res = getImage(obj, width, height, format, fastMode);
2849
- cacheSet(key, res);
2912
+ entryMemoSet(key, obj.body, meta, res);
2850
2913
  return res;
2851
2914
  }
2852
2915
  var summaryRenderer = new Remarkable({
@@ -2931,12 +2994,12 @@ function getPostBodySummary(obj, length, platform) {
2931
2994
  return postBodySummary(obj, normalizedLength, normalizedPlatform);
2932
2995
  }
2933
2996
  const key = `${makeEntryCacheKey(obj)}-sum-${normalizedLength}-${normalizedPlatform}`;
2934
- const item = cacheGet(key);
2935
- if (item) {
2997
+ const item = entryMemoGet(key, obj.body);
2998
+ if (item !== MEMO_MISS) {
2936
2999
  return item;
2937
3000
  }
2938
3001
  const res = postBodySummary(obj.body, normalizedLength, normalizedPlatform);
2939
- cacheSet(key, res);
3002
+ entryMemoSet(key, obj.body, void 0, res);
2940
3003
  return res;
2941
3004
  }
2942
3005