@ecency/render-helper 2.4.33 → 2.4.34

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.
@@ -2,11 +2,11 @@ import { DOMParser as DOMParser$1, XMLSerializer } from '@xmldom/xmldom';
2
2
  import xss from 'xss';
3
3
  import multihash from 'multihashes';
4
4
  import querystring from 'querystring';
5
+ import { LRUCache } from 'lru-cache';
5
6
  import { Remarkable } from 'remarkable';
6
7
  import { linkify as linkify$1 } from 'remarkable/linkify';
7
8
  import * as htmlparser2 from 'htmlparser2';
8
9
  import * as domSerializerModule from 'dom-serializer';
9
- import { LRUCache } from 'lru-cache';
10
10
  import he from 'he';
11
11
 
12
12
  // src/consts/white-list.const.ts
@@ -296,6 +296,14 @@ function sanitizeHtml(html) {
296
296
  });
297
297
  }
298
298
  var proxyBase = "https://images.ecency.com";
299
+ var urlHashCache = new LRUCache({ max: 500 });
300
+ function getUrlHash(url) {
301
+ const cached = urlHashCache.get(url);
302
+ if (cached) return cached;
303
+ const hash = multihash.toB58String(Buffer.from(url));
304
+ urlHashCache.set(url, hash);
305
+ return hash;
306
+ }
299
307
  function setProxyBase(p2) {
300
308
  proxyBase = p2;
301
309
  }
@@ -346,7 +354,7 @@ function proxifyImageSrc(url, width = 0, height = 0, _format = "match") {
346
354
  if (pHash) {
347
355
  return `${proxyBase}/p/${pHash}?${qs}`;
348
356
  }
349
- const b58url = multihash.toB58String(Buffer.from(realUrl.toString()));
357
+ const b58url = getUrlHash(realUrl.toString());
350
358
  return `${proxyBase}/p/${b58url}?${qs}`;
351
359
  }
352
360
  var SRCSET_WIDTHS = [320, 600, 800, 1024, 1280];
@@ -1618,7 +1626,7 @@ function simpleMarkdownToHTML(input) {
1618
1626
  const html = getMd().render(input);
1619
1627
  return sanitizeHtml(html);
1620
1628
  }
1621
- var cache = new LRUCache({ max: 60 });
1629
+ var cache = new LRUCache({ max: 500 });
1622
1630
  function setCacheSize(size) {
1623
1631
  cache = new LRUCache({ max: size });
1624
1632
  }
@@ -1649,6 +1657,40 @@ var gifLinkRegex = /\.(gif)$/i;
1649
1657
  function isGifLink(link) {
1650
1658
  return gifLinkRegex.test(link);
1651
1659
  }
1660
+ var BACKTICK_FENCE_RE = /```[\s\S]*?```/g;
1661
+ var TILDE_FENCE_RE = /~~~[\s\S]*?~~~/g;
1662
+ var INLINE_CODE_RE = /`[^`\n]*`/g;
1663
+ var INDENTED_CODE_RE = /^(?: {4}|\t).+$/gm;
1664
+ var MD_IMAGE_RE = /!\[[^\]]*\]\(\s*([^)\s]+)(?:\s+["'][^"']*["'])?\s*\)/;
1665
+ var HTML_IMAGE_RE = /<img\b[^>]*?\bsrc\s*=\s*["']([^"']+)["']/i;
1666
+ var SAFE_URL_RE = /^https?:\/\//i;
1667
+ function findFirstImageUrl(body) {
1668
+ if (!body) return null;
1669
+ const cleaned = body.replace(BACKTICK_FENCE_RE, "").replace(TILDE_FENCE_RE, "").replace(INLINE_CODE_RE, "").replace(INDENTED_CODE_RE, "");
1670
+ const mdMatch = cleaned.match(MD_IMAGE_RE);
1671
+ const htmlMatch = cleaned.match(HTML_IMAGE_RE);
1672
+ if (mdMatch) {
1673
+ const url = mdMatch[1];
1674
+ if (!url || !SAFE_URL_RE.test(url) || url.includes("(")) {
1675
+ return null;
1676
+ }
1677
+ }
1678
+ const mdValid = !!mdMatch;
1679
+ const htmlValid = !!(htmlMatch && htmlMatch[1] && SAFE_URL_RE.test(htmlMatch[1]));
1680
+ if (mdValid && htmlValid) {
1681
+ return (mdMatch.index ?? 0) < (htmlMatch.index ?? 0) ? mdMatch[1] : htmlMatch[1];
1682
+ }
1683
+ if (mdValid) return mdMatch[1];
1684
+ if (htmlValid) return htmlMatch[1];
1685
+ return null;
1686
+ }
1687
+ function proxifyFound(src, width, height, format) {
1688
+ const decoded = he.decode(src);
1689
+ if (isGifLink(decoded)) {
1690
+ return proxifyImageSrc(decoded, 0, 0, format);
1691
+ }
1692
+ return proxifyImageSrc(decoded, width, height, format);
1693
+ }
1652
1694
  function getImage(entry, width = 0, height = 0, format = "match") {
1653
1695
  let meta;
1654
1696
  if (typeof entry.json_metadata === "object") {
@@ -1680,6 +1722,10 @@ function getImage(entry, width = 0, height = 0, format = "match") {
1680
1722
  }
1681
1723
  return proxifyImageSrc(meta.image[0], width, height, format);
1682
1724
  }
1725
+ const fast = findFirstImageUrl(entry.body);
1726
+ if (fast) {
1727
+ return proxifyFound(fast, width, height, format);
1728
+ }
1683
1729
  const html = markdown2Html(entry);
1684
1730
  const doc = createDoc(html);
1685
1731
  if (!doc) {
@@ -1691,16 +1737,16 @@ function getImage(entry, width = 0, height = 0, format = "match") {
1691
1737
  if (!src) {
1692
1738
  return null;
1693
1739
  }
1694
- const decodedSrc = he.decode(src);
1695
- if (isGifLink(decodedSrc)) {
1696
- return proxifyImageSrc(decodedSrc, 0, 0, format);
1697
- }
1698
- return proxifyImageSrc(decodedSrc, width, height, format);
1740
+ return proxifyFound(src, width, height, format);
1699
1741
  }
1700
1742
  return null;
1701
1743
  }
1702
1744
  function catchPostImage(obj, width = 0, height = 0, format = "match") {
1703
1745
  if (typeof obj === "string") {
1746
+ const fast = findFirstImageUrl(obj);
1747
+ if (fast) {
1748
+ return proxifyFound(fast, width, height, format);
1749
+ }
1704
1750
  const html = markdown2Html(obj);
1705
1751
  const doc = createDoc(html);
1706
1752
  if (!doc) {
@@ -1712,11 +1758,7 @@ function catchPostImage(obj, width = 0, height = 0, format = "match") {
1712
1758
  if (!src) {
1713
1759
  return null;
1714
1760
  }
1715
- const decodedSrc = he.decode(src);
1716
- if (isGifLink(decodedSrc)) {
1717
- return proxifyImageSrc(decodedSrc, 0, 0, format);
1718
- }
1719
- return proxifyImageSrc(decodedSrc, width, height, format);
1761
+ return proxifyFound(src, width, height, format);
1720
1762
  }
1721
1763
  return null;
1722
1764
  }
@@ -1729,6 +1771,20 @@ function catchPostImage(obj, width = 0, height = 0, format = "match") {
1729
1771
  cacheSet(key, res);
1730
1772
  return res;
1731
1773
  }
1774
+ var summaryRenderer = new Remarkable({
1775
+ html: true,
1776
+ breaks: true,
1777
+ typographer: false
1778
+ });
1779
+ summaryRenderer.core.ruler.enable(["abbr"]);
1780
+ summaryRenderer.block.ruler.enable(["footnote", "deflist"]);
1781
+ summaryRenderer.inline.ruler.enable([
1782
+ "footnote_inline",
1783
+ "ins",
1784
+ "mark",
1785
+ "sub",
1786
+ "sup"
1787
+ ]);
1732
1788
  var joint = (arr, limit = 200) => {
1733
1789
  let result = "";
1734
1790
  if (arr) {
@@ -1754,25 +1810,6 @@ function postBodySummary(entryBody, length = 200, platform = "web") {
1754
1810
  return "";
1755
1811
  }
1756
1812
  entryBody = cleanReply(entryBody);
1757
- const mdd = new Remarkable({
1758
- html: true,
1759
- breaks: true,
1760
- typographer: false
1761
- }).use(linkify$1);
1762
- mdd.core.ruler.enable([
1763
- "abbr"
1764
- ]);
1765
- mdd.block.ruler.enable([
1766
- "footnote",
1767
- "deflist"
1768
- ]);
1769
- mdd.inline.ruler.enable([
1770
- "footnote_inline",
1771
- "ins",
1772
- "mark",
1773
- "sub",
1774
- "sup"
1775
- ]);
1776
1813
  const entities = entryBody.match(ENTITY_REGEX);
1777
1814
  const entityPlaceholders = [];
1778
1815
  if (entities && platform !== "web") {
@@ -1785,7 +1822,7 @@ function postBodySummary(entryBody, length = 200, platform = "web") {
1785
1822
  }
1786
1823
  let text2 = "";
1787
1824
  try {
1788
- text2 = mdd.render(entryBody);
1825
+ text2 = summaryRenderer.render(entryBody);
1789
1826
  } catch (err) {
1790
1827
  console.error("[postBodySummary] Failed to render markdown:", {
1791
1828
  error: err instanceof Error ? err.message : String(err),