@ecency/render-helper 2.5.39 → 2.5.40
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.
- package/dist/browser/index.d.ts +13 -0
- package/dist/browser/index.js +64 -9
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +64 -9
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.mjs +64 -9
- package/dist/node/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/node/index.cjs
CHANGED
|
@@ -798,6 +798,27 @@ function buildPictureSources(rawUrl) {
|
|
|
798
798
|
return { avif, webp };
|
|
799
799
|
}
|
|
800
800
|
|
|
801
|
+
// src/methods/image-dimensions.ts
|
|
802
|
+
var MAX_IMAGE_DIMENSION_PX = 8192;
|
|
803
|
+
var MAX_IMAGE_DIMENSION_RATIO = 40;
|
|
804
|
+
function parsePixelDimension(value) {
|
|
805
|
+
if (value == null) return null;
|
|
806
|
+
const trimmed = value.trim();
|
|
807
|
+
if (!/^\d{1,4}$/.test(trimmed)) return null;
|
|
808
|
+
const n = Number(trimmed);
|
|
809
|
+
if (n < 1 || n > MAX_IMAGE_DIMENSION_PX) return null;
|
|
810
|
+
return n;
|
|
811
|
+
}
|
|
812
|
+
function authorPixelSize(width, height) {
|
|
813
|
+
const w = parsePixelDimension(width);
|
|
814
|
+
const h = parsePixelDimension(height);
|
|
815
|
+
if (w == null || h == null) return null;
|
|
816
|
+
const larger = Math.max(w, h);
|
|
817
|
+
const smaller = Math.min(w, h);
|
|
818
|
+
if (larger / smaller > MAX_IMAGE_DIMENSION_RATIO) return null;
|
|
819
|
+
return { width: String(w), height: String(h) };
|
|
820
|
+
}
|
|
821
|
+
|
|
801
822
|
// src/methods/sanitize-html.method.ts
|
|
802
823
|
var EMBED_SRC_DATA_ATTRS = /* @__PURE__ */ new Set(["data-embed-src", "data-video-href"]);
|
|
803
824
|
var isSafeNavValue = (value) => {
|
|
@@ -812,9 +833,34 @@ var isProxyPSrcset = (srcset) => {
|
|
|
812
833
|
const candidates = srcset.split(",").map((c) => c.trim().split(/\s+/)[0]).filter(Boolean);
|
|
813
834
|
return candidates.length > 0 && candidates.every((url) => url.startsWith(`${base}/p/`));
|
|
814
835
|
};
|
|
815
|
-
|
|
836
|
+
var QUOTED_ATTR = /\s([^\s"'>\/=]+)="([^"]*)"/g;
|
|
837
|
+
function enforceImageDimensionPair(html) {
|
|
838
|
+
return html.replace(/<img\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi, (tag) => {
|
|
839
|
+
QUOTED_ATTR.lastIndex = 0;
|
|
840
|
+
const attrs = [];
|
|
841
|
+
let match;
|
|
842
|
+
while (match = QUOTED_ATTR.exec(tag)) {
|
|
843
|
+
attrs.push({ name: match[1].toLowerCase(), value: decodeEntities(match[2]) });
|
|
844
|
+
}
|
|
845
|
+
const width = attrs.find((attr) => attr.name === "width")?.value;
|
|
846
|
+
const height = attrs.find((attr) => attr.name === "height")?.value;
|
|
847
|
+
const src = (attrs.find((attr) => attr.name === "src")?.value ?? "").trim();
|
|
848
|
+
if (src && authorPixelSize(width, height)) return tag;
|
|
849
|
+
QUOTED_ATTR.lastIndex = 0;
|
|
850
|
+
return tag.replace(
|
|
851
|
+
QUOTED_ATTR,
|
|
852
|
+
(full, name) => name.toLowerCase() === "width" || name.toLowerCase() === "height" ? "" : full
|
|
853
|
+
);
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
function sanitizeHtml(html, options) {
|
|
857
|
+
const preserveImageDimensions = !!options?.preserveImageDimensions;
|
|
858
|
+
const whiteList = preserveImageDimensions ? {
|
|
859
|
+
...ALLOWED_ATTRIBUTES,
|
|
860
|
+
img: [...ALLOWED_ATTRIBUTES.img ?? [], "width", "height"]
|
|
861
|
+
} : ALLOWED_ATTRIBUTES;
|
|
816
862
|
const cleaned = xss__default.default(html, {
|
|
817
|
-
whiteList
|
|
863
|
+
whiteList,
|
|
818
864
|
stripIgnoreTag: true,
|
|
819
865
|
stripIgnoreTagBody: ["style"],
|
|
820
866
|
css: false,
|
|
@@ -833,6 +879,9 @@ function sanitizeHtml(html) {
|
|
|
833
879
|
if ((tag === "video" || tag === "audio") && ["src", "poster"].includes(name) && !/^https?:\/\//.test(decodedLower)) return "";
|
|
834
880
|
if (tag === "audio" && name === "preload" && decodedLower !== "metadata" && decodedLower !== "none") return "";
|
|
835
881
|
if (tag === "img" && ["dynsrc", "lowsrc"].includes(name)) return "";
|
|
882
|
+
if (tag === "img" && (name === "width" || name === "height")) {
|
|
883
|
+
if (!preserveImageDimensions || parsePixelDimension(decoded) == null) return "";
|
|
884
|
+
}
|
|
836
885
|
if (tag === "span" && name === "class" && decoded.toLowerCase().trim() === "wr") return "";
|
|
837
886
|
if (EMBED_SRC_DATA_ATTRS.has(name) && !isAllowedEmbedSrc(decoded)) return "";
|
|
838
887
|
if (name === "data-href" && !isSafeNavValue(decoded)) return "";
|
|
@@ -842,10 +891,11 @@ function sanitizeHtml(html) {
|
|
|
842
891
|
return void 0;
|
|
843
892
|
}
|
|
844
893
|
});
|
|
845
|
-
|
|
894
|
+
const withoutBareSource = cleaned.replace(
|
|
846
895
|
/<source\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi,
|
|
847
896
|
(t) => /\btype\s*=\s*["'](?:image\/avif|image\/webp)["']/i.test(t) ? t : ""
|
|
848
897
|
);
|
|
898
|
+
return preserveImageDimensions ? enforceImageDimensionPair(withoutBareSource) : withoutBareSource;
|
|
849
899
|
}
|
|
850
900
|
|
|
851
901
|
// src/methods/img.method.ts
|
|
@@ -873,9 +923,10 @@ function wrapInPicture(el, rawUrl) {
|
|
|
873
923
|
picture.appendChild(webp);
|
|
874
924
|
picture.appendChild(el);
|
|
875
925
|
}
|
|
876
|
-
function img(el, state, forApp = true) {
|
|
926
|
+
function img(el, state, forApp = true, renderOptions) {
|
|
877
927
|
const src = el.getAttribute("src") || "";
|
|
878
928
|
const decodedSrc = decodeImageSrc(src);
|
|
929
|
+
const reserved = renderOptions?.preserveImageDimensions ? authorPixelSize(el.getAttribute("width"), el.getAttribute("height")) : null;
|
|
879
930
|
["onerror", "dynsrc", "lowsrc", "width", "height"].forEach((attr) => el.removeAttribute(attr));
|
|
880
931
|
const isInvalid = !src || decodedSrc.startsWith("javascript") || decodedSrc.startsWith("vbscript") || decodedSrc === "x";
|
|
881
932
|
if (isInvalid) {
|
|
@@ -891,6 +942,10 @@ function img(el, state, forApp = true) {
|
|
|
891
942
|
el.removeAttribute("sizes");
|
|
892
943
|
return;
|
|
893
944
|
}
|
|
945
|
+
if (reserved) {
|
|
946
|
+
el.setAttribute("width", reserved.width);
|
|
947
|
+
el.setAttribute("height", reserved.height);
|
|
948
|
+
}
|
|
894
949
|
el.setAttribute("itemprop", "image");
|
|
895
950
|
const avatarRoute = new RegExp(`^${trimTrailingSlash(getProxyBase()).replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/u/[^/]+/avatar(?:/[a-z]+)?$`);
|
|
896
951
|
const classTokens = (el.getAttribute("class") || "").split(/\s+/);
|
|
@@ -2059,7 +2114,7 @@ function traverse(node, forApp, depth = 0, state = { firstImageFound: false }, p
|
|
|
2059
2114
|
text(child, forApp, renderOptions);
|
|
2060
2115
|
}
|
|
2061
2116
|
if (child.nodeName.toLowerCase() === "img") {
|
|
2062
|
-
img(child, state, forApp);
|
|
2117
|
+
img(child, state, forApp, renderOptions);
|
|
2063
2118
|
}
|
|
2064
2119
|
if (child.nodeName.toLowerCase() === "p") {
|
|
2065
2120
|
p(child);
|
|
@@ -10238,7 +10293,7 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
|
|
|
10238
10293
|
output = serializer.serializeToString(doc);
|
|
10239
10294
|
} catch (error) {
|
|
10240
10295
|
try {
|
|
10241
|
-
const preSanitized = sanitizeHtml(output);
|
|
10296
|
+
const preSanitized = sanitizeHtml(output, renderOptions);
|
|
10242
10297
|
const dom = htmlparser2__namespace.parseDocument(preSanitized, {
|
|
10243
10298
|
// lenient options - don't throw on malformed HTML
|
|
10244
10299
|
lowerCaseTags: false,
|
|
@@ -10249,7 +10304,7 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
|
|
|
10249
10304
|
traverse(doc, forApp, 0, { firstImageFound: false }, parentDomain, seoContext, renderOptions);
|
|
10250
10305
|
output = serializer.serializeToString(doc);
|
|
10251
10306
|
} catch (fallbackError) {
|
|
10252
|
-
output = sanitizeHtml(output || md.render(input));
|
|
10307
|
+
output = sanitizeHtml(output || md.render(input), renderOptions);
|
|
10253
10308
|
}
|
|
10254
10309
|
}
|
|
10255
10310
|
if (forApp && output && entityPlaceholders.length > 0) {
|
|
@@ -10259,7 +10314,7 @@ function markdownToHTML(input, forApp, parentDomain = "ecency.com", seoContext,
|
|
|
10259
10314
|
});
|
|
10260
10315
|
}
|
|
10261
10316
|
output = output.replace(/ xmlns="http:\/\/www.w3.org\/1999\/xhtml"/g, "").replace(/^<\?xml[^?]*\?>/, "").replace(/^<!DOCTYPE[^>]*>/i, "").replace(/<\/?html[^>]*>/g, "").replace(/<head[^>]*>[\s\S]*?<\/head>/g, "").replace('<body id="root">', "").replace("</body>", "").trim();
|
|
10262
|
-
return sanitizeHtml(output);
|
|
10317
|
+
return sanitizeHtml(output, renderOptions);
|
|
10263
10318
|
}
|
|
10264
10319
|
|
|
10265
10320
|
// src/methods/simple-markdown-to-html.method.ts
|
|
@@ -10316,7 +10371,7 @@ function markdown2Html(obj, forApp = true, _webp = false, parentDomain = "ecency
|
|
|
10316
10371
|
logIfSlow(performance.now() - t02, `body_len=${obj.length}`);
|
|
10317
10372
|
return res2;
|
|
10318
10373
|
}
|
|
10319
|
-
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 : ""}`;
|
|
10374
|
+
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 : ""}${renderOptions?.preserveImageDimensions ? "-imgdim" : ""}`;
|
|
10320
10375
|
const item = entryMemoGet(key, obj.body);
|
|
10321
10376
|
if (item !== MEMO_MISS) {
|
|
10322
10377
|
return item;
|