@ecency/render-helper 2.4.34 → 2.5.0

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.
@@ -27,6 +27,7 @@ interface SeoContext {
27
27
  postPayout?: number;
28
28
  }
29
29
 
30
+ declare function setSlowRenderThresholdMs(ms: number): void;
30
31
  /**
31
32
  * @param obj - Entry object or raw markdown string
32
33
  * @param forApp - Whether rendering for app context
@@ -76,4 +77,4 @@ declare function isValidPermlink(permlink: string): boolean;
76
77
  */
77
78
  declare function simpleMarkdownToHTML(input: string): string;
78
79
 
79
- export { type Entry, type RenderOptions, SECTION_LIST, type SeoContext, buildSrcSet, catchPostImage, isValidPermlink, getPostBodySummary as postBodySummary, proxifyImageSrc, markdown2Html as renderPostBody, setCacheSize, setProxyBase, simpleMarkdownToHTML };
80
+ export { type Entry, type RenderOptions, SECTION_LIST, type SeoContext, buildSrcSet, catchPostImage, isValidPermlink, getPostBodySummary as postBodySummary, proxifyImageSrc, markdown2Html as renderPostBody, setCacheSize, setProxyBase, setSlowRenderThresholdMs, simpleMarkdownToHTML };
@@ -177,23 +177,112 @@ function createParser() {
177
177
  var DOMParser = createParser();
178
178
 
179
179
  // src/helper.ts
180
+ function isSpaceChar(c) {
181
+ return c === 32 || c === 9 || c === 10 || c === 13 || c === 12;
182
+ }
183
+ function isAsciiLetter(c) {
184
+ return c >= 65 && c <= 90 || c >= 97 && c <= 122;
185
+ }
186
+ function isTagNameChar(c) {
187
+ return isAsciiLetter(c) || c >= 48 && c <= 57;
188
+ }
189
+ function isAttrNameChar(c) {
190
+ return isAsciiLetter(c) || c >= 48 && c <= 57 || c === 45 || c === 95 || c === 58 || c === 46;
191
+ }
180
192
  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]);
193
+ const n = html.length;
194
+ let out = "";
195
+ let i = 0;
196
+ while (i < n) {
197
+ const lt = html.indexOf("<", i);
198
+ if (lt < 0) {
199
+ out += html.slice(i);
200
+ break;
201
+ }
202
+ out += html.slice(i, lt);
203
+ if (lt + 1 >= n || !isAsciiLetter(html.charCodeAt(lt + 1))) {
204
+ out += "<";
205
+ i = lt + 1;
206
+ continue;
207
+ }
208
+ let p2 = lt + 1;
209
+ while (p2 < n && isTagNameChar(html.charCodeAt(p2))) p2++;
210
+ const tagName = html.slice(lt + 1, p2);
211
+ if (p2 >= n || !isSpaceChar(html.charCodeAt(p2))) {
212
+ out += "<";
213
+ i = lt + 1;
214
+ continue;
215
+ }
216
+ const attrs = [];
217
+ const seen = /* @__PURE__ */ new Set();
218
+ let q = p2;
219
+ while (q < n) {
220
+ while (q < n && isSpaceChar(html.charCodeAt(q))) q++;
221
+ if (q >= n) break;
222
+ const ch = html.charCodeAt(q);
223
+ if (ch === 62) break;
224
+ if (ch === 47 && q + 1 < n && html.charCodeAt(q + 1) === 62) break;
225
+ const nameStart = q;
226
+ while (q < n && isAttrNameChar(html.charCodeAt(q))) q++;
227
+ if (q === nameStart) {
228
+ q++;
229
+ continue;
230
+ }
231
+ const attrName = html.slice(nameStart, q);
232
+ let r = q;
233
+ while (r < n && isSpaceChar(html.charCodeAt(r))) r++;
234
+ let valueEnd = q;
235
+ if (r < n && html.charCodeAt(r) === 61) {
236
+ r++;
237
+ while (r < n && isSpaceChar(html.charCodeAt(r))) r++;
238
+ if (r < n) {
239
+ const v = html.charCodeAt(r);
240
+ if (v === 34 || v === 39) {
241
+ const quote = html[r];
242
+ const end = html.indexOf(quote, r + 1);
243
+ if (end < 0) {
244
+ const gt = html.indexOf(">", r + 1);
245
+ valueEnd = gt < 0 ? n : gt;
246
+ } else {
247
+ valueEnd = end + 1;
248
+ }
249
+ } else {
250
+ let s = r;
251
+ while (s < n) {
252
+ const k = html.charCodeAt(s);
253
+ if (isSpaceChar(k) || k === 62) break;
254
+ s++;
255
+ }
256
+ valueEnd = s;
257
+ }
258
+ } else {
259
+ valueEnd = r;
260
+ }
261
+ }
262
+ const fullAttr = html.slice(nameStart, valueEnd);
263
+ q = valueEnd;
264
+ const key = attrName.toLowerCase();
265
+ if (!seen.has(key)) {
266
+ seen.add(key);
267
+ attrs.push(fullAttr);
192
268
  }
193
269
  }
194
- const attrsJoined = cleanedAttrs.length > 0 ? ` ${cleanedAttrs.join(" ")}` : "";
195
- return `<${tagName}${attrsJoined}${selfClose ? " /" : ""}>`;
196
- });
270
+ let selfClose = false;
271
+ if (q < n && html.charCodeAt(q) === 47) {
272
+ selfClose = true;
273
+ q++;
274
+ }
275
+ if (q >= n || html.charCodeAt(q) !== 62) {
276
+ out += "<";
277
+ i = lt + 1;
278
+ continue;
279
+ }
280
+ q++;
281
+ const attrsJoined = attrs.length > 0 ? " " + attrs.join(" ") : "";
282
+ out += "<" + tagName + attrsJoined + (selfClose ? " /" : "") + ">";
283
+ i = q;
284
+ }
285
+ return out;
197
286
  }
198
287
  function createDoc(html) {
199
288
  if (html.trim() === "") {
@@ -1586,7 +1675,6 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
1586
1675
  output = serializer.serializeToString(doc);
1587
1676
  } catch (error) {
1588
1677
  try {
1589
- output = md.render(input);
1590
1678
  const preSanitized = sanitizeHtml(output);
1591
1679
  const dom = htmlparser2.parseDocument(preSanitized, {
1592
1680
  // lenient options - don't throw on malformed HTML
@@ -1638,10 +1726,25 @@ function cacheSet(key, value) {
1638
1726
  }
1639
1727
 
1640
1728
  // src/markdown-2-html.ts
1729
+ var isNodeRuntime = typeof process !== "undefined" && typeof process?.versions?.node === "string";
1730
+ var slowRenderThresholdMs = isNodeRuntime ? 500 : 0;
1731
+ function setSlowRenderThresholdMs(ms) {
1732
+ slowRenderThresholdMs = Math.max(0, ms);
1733
+ }
1734
+ function logIfSlow(durationMs, context) {
1735
+ if (slowRenderThresholdMs > 0 && durationMs >= slowRenderThresholdMs) {
1736
+ console.warn(
1737
+ `[render-helper] slow markdown render: ${durationMs.toFixed(0)}ms ${context}`
1738
+ );
1739
+ }
1740
+ }
1641
1741
  function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency.com", seoContext, renderOptions) {
1642
1742
  if (typeof obj === "string") {
1643
1743
  const cleanedStr = cleanReply(obj);
1644
- return markdownToHTML(cleanedStr, forApp, parentDomain, seoContext, renderOptions);
1744
+ const t02 = performance.now();
1745
+ const res2 = markdownToHTML(cleanedStr, forApp, parentDomain, seoContext, renderOptions);
1746
+ logIfSlow(performance.now() - t02, `body_len=${obj.length}`);
1747
+ return res2;
1645
1748
  }
1646
1749
  const key = `${makeEntryCacheKey(obj)}-md-${forApp ? "app" : "site"}-${parentDomain}${seoContext ? `-seo${seoContext.authorReputation ?? ""}-${seoContext.postPayout ?? ""}` : ""}${renderOptions?.embedVideosDirectly ? "-embed" : ""}`;
1647
1750
  const item = cacheGet(key);
@@ -1649,7 +1752,12 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
1649
1752
  return item;
1650
1753
  }
1651
1754
  const cleanBody = cleanReply(obj.body);
1755
+ const t0 = performance.now();
1652
1756
  const res = markdownToHTML(cleanBody, forApp, parentDomain, seoContext, renderOptions);
1757
+ logIfSlow(
1758
+ performance.now() - t0,
1759
+ `author=@${obj.author} permlink=${obj.permlink} body_len=${obj.body?.length ?? 0}`
1760
+ );
1653
1761
  cacheSet(key, res);
1654
1762
  return res;
1655
1763
  }
@@ -1862,6 +1970,6 @@ function getPostBodySummary(obj, length, platform) {
1862
1970
  return res;
1863
1971
  }
1864
1972
 
1865
- export { SECTION_LIST, buildSrcSet, catchPostImage, isValidPermlink, getPostBodySummary as postBodySummary, proxifyImageSrc, markdown2Html as renderPostBody, setCacheSize, setProxyBase, simpleMarkdownToHTML };
1973
+ export { SECTION_LIST, buildSrcSet, catchPostImage, isValidPermlink, getPostBodySummary as postBodySummary, proxifyImageSrc, markdown2Html as renderPostBody, setCacheSize, setProxyBase, setSlowRenderThresholdMs, simpleMarkdownToHTML };
1866
1974
  //# sourceMappingURL=index.js.map
1867
1975
  //# sourceMappingURL=index.js.map