@ecency/render-helper 2.4.30 → 2.4.31

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.
@@ -123,6 +123,8 @@ var ALLOWED_ATTRIBUTES = {
123
123
  ],
124
124
  "img": [
125
125
  "src",
126
+ "srcset",
127
+ "sizes",
126
128
  "alt",
127
129
  "class",
128
130
  "loading",
@@ -281,6 +283,10 @@ function sanitizeHtml(html) {
281
283
  const decodedLower = decoded.toLowerCase();
282
284
  if (name.startsWith("on")) return "";
283
285
  if (tag === "img" && name === "src" && (!/^https?:\/\//.test(decodedLower) || decodedLower.startsWith("javascript:"))) return "";
286
+ if (tag === "img" && name === "srcset") {
287
+ const candidates = decoded.split(",").map((c) => c.trim().split(/\s+/)[0]);
288
+ if (candidates.some((url) => !/^https?:\/\//.test(url))) return "";
289
+ }
284
290
  if (tag === "video" && ["src", "poster"].includes(name) && (!/^https?:\/\//.test(decodedLower) || decodedLower.startsWith("javascript:"))) return "";
285
291
  if (tag === "img" && ["dynsrc", "lowsrc"].includes(name)) return "";
286
292
  if (tag === "span" && name === "class" && decoded.toLowerCase().trim() === "wr") return "";
@@ -295,6 +301,9 @@ var proxyBase = "https://images.ecency.com";
295
301
  function setProxyBase(p2) {
296
302
  proxyBase = p2;
297
303
  }
304
+ function getProxyBase() {
305
+ return proxyBase;
306
+ }
298
307
  function extractPHash(url) {
299
308
  if (url.startsWith(`${proxyBase}/p/`)) {
300
309
  const [hash] = url.split("/p/")[1].split("?");
@@ -342,8 +351,24 @@ function proxifyImageSrc(url, width = 0, height = 0, _format = "match") {
342
351
  const b58url = multihash.toB58String(Buffer.from(realUrl.toString()));
343
352
  return `${proxyBase}/p/${b58url}?${qs}`;
344
353
  }
354
+ var SRCSET_WIDTHS = [320, 600, 800, 1024, 1280];
355
+ function buildSrcSet(url) {
356
+ if (!url || typeof url !== "string") return "";
357
+ const escapedBase = proxyBase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
358
+ const proxyPattern = new RegExp(`^${escapedBase}/p/([^?]+)`);
359
+ const match = url.match(proxyPattern);
360
+ if (match) {
361
+ const phash = extractPHash(url) || match[1];
362
+ return SRCSET_WIDTHS.map((w) => `${proxyBase}/p/${phash}?format=match&mode=fit&width=${w} ${w}w`).join(", ");
363
+ }
364
+ return SRCSET_WIDTHS.map((w) => {
365
+ const proxied = proxifyImageSrc(url, w);
366
+ return proxied ? `${proxied} ${w}w` : "";
367
+ }).filter(Boolean).join(", ");
368
+ }
345
369
 
346
370
  // src/methods/img.method.ts
371
+ var IMAGE_SIZES = "(max-width: 768px) 100vw, 700px";
347
372
  function img(el, state) {
348
373
  const src = el.getAttribute("src") || "";
349
374
  const decodedSrc = decodeURIComponent(
@@ -353,11 +378,15 @@ function img(el, state) {
353
378
  const isInvalid = !src || decodedSrc.startsWith("javascript") || decodedSrc.startsWith("vbscript") || decodedSrc === "x";
354
379
  if (isInvalid) {
355
380
  el.removeAttribute("src");
381
+ el.removeAttribute("srcset");
382
+ el.removeAttribute("sizes");
356
383
  return;
357
384
  }
358
385
  const isRelative = !/^https?:\/\//i.test(decodedSrc) && !decodedSrc.startsWith("/");
359
386
  if (isRelative) {
360
387
  el.removeAttribute("src");
388
+ el.removeAttribute("srcset");
389
+ el.removeAttribute("sizes");
361
390
  return;
362
391
  }
363
392
  el.setAttribute("itemprop", "image");
@@ -372,22 +401,41 @@ function img(el, state) {
372
401
  }
373
402
  const cls = el.getAttribute("class") || "";
374
403
  const shouldReplace = !cls.includes("no-replace");
375
- const hasAlreadyProxied = src.startsWith("https://images.ecency.com/p/") || src.startsWith("https://images.ecency.com/u/") || /^https:\/\/images\.ecency\.com\/\d+x\d+\//.test(src);
404
+ const base = getProxyBase().replace(/\/+$/, "");
405
+ const hasAlreadyProxied = src.startsWith(`${base}/p/`) || src.startsWith(`${base}/u/`) || new RegExp(`^${base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/\\d+x\\d+/`).test(src);
376
406
  if (shouldReplace && !hasAlreadyProxied) {
377
407
  const proxified = proxifyImageSrc(decodedSrc);
378
408
  if (proxified) {
379
409
  el.setAttribute("src", proxified);
410
+ const srcset = buildSrcSet(decodedSrc);
411
+ if (srcset) {
412
+ el.setAttribute("srcset", srcset);
413
+ el.setAttribute("sizes", IMAGE_SIZES);
414
+ }
415
+ }
416
+ } else if (shouldReplace && hasAlreadyProxied) {
417
+ if (src.startsWith(`${base}/p/`)) {
418
+ const srcset = buildSrcSet(src);
419
+ if (srcset) {
420
+ el.setAttribute("srcset", srcset);
421
+ el.setAttribute("sizes", IMAGE_SIZES);
422
+ }
380
423
  }
381
424
  }
382
425
  }
383
426
  function createImageHTML(src, isLCP) {
384
427
  const proxified = proxifyImageSrc(src);
385
428
  if (!proxified) return "";
429
+ const base = getProxyBase().replace(/\/+$/, "");
430
+ const isAlreadyProxied = src.startsWith(`${base}/u/`) || new RegExp(`^${base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/\\d+x\\d+/`).test(src);
431
+ const srcset = isAlreadyProxied ? "" : buildSrcSet(src);
386
432
  const loading = isLCP ? "eager" : "lazy";
387
433
  const fetch = isLCP ? 'fetchpriority="high"' : 'decoding="async"';
434
+ const srcsetAttr = srcset ? `srcset="${srcset}" sizes="${IMAGE_SIZES}"` : "";
388
435
  return `<img
389
436
  class="markdown-img-link"
390
437
  src="${proxified}"
438
+ ${srcsetAttr}
391
439
  loading="${loading}"
392
440
  ${fetch}
393
441
  itemprop="image"
@@ -1780,6 +1828,6 @@ function getPostBodySummary(obj, length, platform) {
1780
1828
  return res;
1781
1829
  }
1782
1830
 
1783
- export { SECTION_LIST, catchPostImage, isValidPermlink, getPostBodySummary as postBodySummary, proxifyImageSrc, markdown2Html as renderPostBody, setCacheSize, setProxyBase, simpleMarkdownToHTML };
1831
+ export { SECTION_LIST, buildSrcSet, catchPostImage, isValidPermlink, getPostBodySummary as postBodySummary, proxifyImageSrc, markdown2Html as renderPostBody, setCacheSize, setProxyBase, simpleMarkdownToHTML };
1784
1832
  //# sourceMappingURL=index.mjs.map
1785
1833
  //# sourceMappingURL=index.mjs.map