@ilamanownuk/blog-kit 0.0.0-stage → 0.2.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.
- package/AI_INSTALL_PROMPT.md +141 -0
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +503 -2
- package/dist/index.cjs +963 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +171 -0
- package/dist/index.d.ts +171 -0
- package/dist/index.js +948 -0
- package/dist/index.js.map +1 -0
- package/dist/renderer.cjs +180 -0
- package/dist/renderer.cjs.map +1 -0
- package/dist/renderer.d.cts +23 -0
- package/dist/renderer.d.ts +23 -0
- package/dist/renderer.js +142 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.cjs +214 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +31 -0
- package/dist/server.d.ts +31 -0
- package/dist/server.js +172 -0
- package/dist/server.js.map +1 -0
- package/dist/styles.css +298 -0
- package/dist/styles.layered.css +301 -0
- package/package.json +110 -4
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/server/index.ts
|
|
31
|
+
var server_exports = {};
|
|
32
|
+
__export(server_exports, {
|
|
33
|
+
ALL_EMBED_PROVIDERS: () => ALL_EMBED_PROVIDERS,
|
|
34
|
+
EMBED_PROVIDERS: () => EMBED_PROVIDERS,
|
|
35
|
+
hasRichContent: () => hasRichContent,
|
|
36
|
+
isAllowedEmbedSrc: () => isAllowedEmbedSrc,
|
|
37
|
+
normalizeLinkUrl: () => normalizeLinkUrl,
|
|
38
|
+
richTextToPlain: () => richTextToPlain,
|
|
39
|
+
sanitizeRichText: () => sanitizeRichText,
|
|
40
|
+
toVideoEmbedUrl: () => toVideoEmbedUrl
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(server_exports);
|
|
43
|
+
var import_sanitize_html = __toESM(require("sanitize-html"), 1);
|
|
44
|
+
|
|
45
|
+
// src/shared/embed.ts
|
|
46
|
+
var ALL_EMBED_PROVIDERS = ["aparat", "youtube"];
|
|
47
|
+
var EMBED_PROVIDERS = {
|
|
48
|
+
aparat: {
|
|
49
|
+
hostname: "www.aparat.com",
|
|
50
|
+
prefix: "https://www.aparat.com/video/video/embed/",
|
|
51
|
+
embedBase: "https://www.aparat.com/video/video/embed/videohash/"
|
|
52
|
+
},
|
|
53
|
+
youtube: {
|
|
54
|
+
hostname: "www.youtube-nocookie.com",
|
|
55
|
+
prefix: "https://www.youtube-nocookie.com/embed/",
|
|
56
|
+
embedBase: "https://www.youtube-nocookie.com/embed/"
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var YOUTUBE_ID = /^[A-Za-z0-9_-]{6,20}$/;
|
|
60
|
+
var APARAT_ID = /^[A-Za-z0-9]{3,20}$/;
|
|
61
|
+
function toVideoEmbedUrl(input, allowed = ALL_EMBED_PROVIDERS) {
|
|
62
|
+
let url;
|
|
63
|
+
try {
|
|
64
|
+
url = new URL(input.trim());
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (url.protocol !== "https:") return null;
|
|
69
|
+
const host = url.hostname.replace(/^(www\.|m\.)/, "");
|
|
70
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
71
|
+
if (host === "aparat.com") {
|
|
72
|
+
if (!allowed.includes("aparat")) return null;
|
|
73
|
+
const id2 = segments[0] === "v" ? segments[1] : segments.at(-1) === "frame" ? segments.at(-3) : void 0;
|
|
74
|
+
return id2 && APARAT_ID.test(id2) ? `${EMBED_PROVIDERS.aparat.embedBase}${id2}/vt/frame` : null;
|
|
75
|
+
}
|
|
76
|
+
if (!allowed.includes("youtube")) return null;
|
|
77
|
+
let id;
|
|
78
|
+
if (host === "youtu.be") id = segments[0];
|
|
79
|
+
else if (host === "youtube.com" || host === "youtube-nocookie.com") {
|
|
80
|
+
id = ["embed", "shorts", "live"].includes(segments[0] ?? "") ? segments[1] : url.searchParams.get("v");
|
|
81
|
+
}
|
|
82
|
+
return id && YOUTUBE_ID.test(id) ? `${EMBED_PROVIDERS.youtube.embedBase}${id}` : null;
|
|
83
|
+
}
|
|
84
|
+
function isAllowedEmbedSrc(src, allowed = ALL_EMBED_PROVIDERS) {
|
|
85
|
+
if (/[\s\\\u0000-\u001f]/.test(src)) return false;
|
|
86
|
+
let url;
|
|
87
|
+
try {
|
|
88
|
+
url = new URL(src);
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (url.protocol !== "https:" || url.username || url.password || url.port) return false;
|
|
93
|
+
return allowed.some((name) => {
|
|
94
|
+
const provider = EMBED_PROVIDERS[name];
|
|
95
|
+
return src.startsWith(provider.prefix) && url.hostname === provider.hostname && url.pathname.startsWith(new URL(provider.prefix).pathname);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function normalizeLinkUrl(input) {
|
|
99
|
+
const value = input.trim();
|
|
100
|
+
if (!value) return null;
|
|
101
|
+
if (value.startsWith("//")) return null;
|
|
102
|
+
if (value.startsWith("/")) return value;
|
|
103
|
+
if (/^(mailto|tel):[^\s]+$/i.test(value)) return value;
|
|
104
|
+
try {
|
|
105
|
+
const url = new URL(/^[a-z][a-z0-9+.-]*:/i.test(value) ? value : `https://${value}`);
|
|
106
|
+
return url.protocol === "http:" || url.protocol === "https:" ? url.toString() : null;
|
|
107
|
+
} catch {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/server/index.ts
|
|
113
|
+
var COLOR = [
|
|
114
|
+
/^#[0-9a-f]{3,8}$/i,
|
|
115
|
+
/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*(0|1|0?\.\d+)\s*)?\)$/i
|
|
116
|
+
];
|
|
117
|
+
var IFRAME_SANDBOX = "allow-scripts allow-same-origin allow-presentation";
|
|
118
|
+
function sanitizeRichText(html, options = {}) {
|
|
119
|
+
const embeds = options.allowedEmbeds ?? ALL_EMBED_PROVIDERS;
|
|
120
|
+
const extraImageFilter = options.isAllowedImageSrc;
|
|
121
|
+
return (0, import_sanitize_html.default)(html, {
|
|
122
|
+
allowedTags: [
|
|
123
|
+
"p",
|
|
124
|
+
"br",
|
|
125
|
+
"strong",
|
|
126
|
+
"em",
|
|
127
|
+
"u",
|
|
128
|
+
"s",
|
|
129
|
+
"h2",
|
|
130
|
+
"h3",
|
|
131
|
+
"h4",
|
|
132
|
+
"ul",
|
|
133
|
+
"ol",
|
|
134
|
+
"li",
|
|
135
|
+
"blockquote",
|
|
136
|
+
"pre",
|
|
137
|
+
"code",
|
|
138
|
+
"a",
|
|
139
|
+
"hr",
|
|
140
|
+
"img",
|
|
141
|
+
"table",
|
|
142
|
+
"thead",
|
|
143
|
+
"tbody",
|
|
144
|
+
"tr",
|
|
145
|
+
"th",
|
|
146
|
+
"td",
|
|
147
|
+
"mark",
|
|
148
|
+
"span",
|
|
149
|
+
"iframe"
|
|
150
|
+
],
|
|
151
|
+
allowedAttributes: {
|
|
152
|
+
a: ["href", "target", "rel"],
|
|
153
|
+
img: ["src", "alt", "width", "height", "loading"],
|
|
154
|
+
td: ["colspan", "rowspan"],
|
|
155
|
+
th: ["colspan", "rowspan"],
|
|
156
|
+
mark: ["data-color"],
|
|
157
|
+
iframe: ["src", "sandbox", "loading", "allowfullscreen"],
|
|
158
|
+
"*": ["style"]
|
|
159
|
+
},
|
|
160
|
+
allowedStyles: {
|
|
161
|
+
"*": {
|
|
162
|
+
"text-align": [/^(right|left|center|justify)$/],
|
|
163
|
+
color: COLOR,
|
|
164
|
+
"background-color": COLOR
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
allowedSchemes: ["http", "https", "mailto", "tel"],
|
|
168
|
+
allowedSchemesByTag: { img: ["https"], iframe: ["https"] },
|
|
169
|
+
allowProtocolRelative: false,
|
|
170
|
+
allowedIframeHostnames: embeds.map((name) => EMBED_PROVIDERS[name].hostname),
|
|
171
|
+
transformTags: {
|
|
172
|
+
a: (tagName, attribs) => {
|
|
173
|
+
const rest = { ...attribs };
|
|
174
|
+
delete rest.target;
|
|
175
|
+
const external = /^https?:\/\//i.test(attribs.href ?? "");
|
|
176
|
+
return {
|
|
177
|
+
tagName,
|
|
178
|
+
attribs: { ...rest, rel: "noopener noreferrer nofollow", ...external ? { target: "_blank" } : {} }
|
|
179
|
+
};
|
|
180
|
+
},
|
|
181
|
+
img: (tagName, attribs) => ({ tagName, attribs: { ...attribs, loading: "lazy" } }),
|
|
182
|
+
// Rebuild iframe attributes from scratch: nothing from the input except src survives.
|
|
183
|
+
iframe: (tagName, attribs) => ({
|
|
184
|
+
tagName,
|
|
185
|
+
attribs: { src: attribs.src ?? "", sandbox: IFRAME_SANDBOX, loading: "lazy", allowfullscreen: "true" }
|
|
186
|
+
})
|
|
187
|
+
},
|
|
188
|
+
exclusiveFilter: (frame) => {
|
|
189
|
+
const src = frame.attribs.src ?? "";
|
|
190
|
+
if (frame.tag === "img") return !(src.startsWith("https://") && (extraImageFilter?.(src) ?? true));
|
|
191
|
+
if (frame.tag === "iframe") return !isAllowedEmbedSrc(src, embeds);
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function richTextToPlain(html) {
|
|
197
|
+
return (0, import_sanitize_html.default)(html, { allowedTags: [], allowedAttributes: {} }).replace(/ | /g, " ").replace(/&/g, "&").replace(/\s+/g, " ").trim();
|
|
198
|
+
}
|
|
199
|
+
function hasRichContent(html, options = {}) {
|
|
200
|
+
const clean = sanitizeRichText(html, options);
|
|
201
|
+
return Boolean(richTextToPlain(clean)) || /<(img|iframe|table)\b/.test(clean);
|
|
202
|
+
}
|
|
203
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
204
|
+
0 && (module.exports = {
|
|
205
|
+
ALL_EMBED_PROVIDERS,
|
|
206
|
+
EMBED_PROVIDERS,
|
|
207
|
+
hasRichContent,
|
|
208
|
+
isAllowedEmbedSrc,
|
|
209
|
+
normalizeLinkUrl,
|
|
210
|
+
richTextToPlain,
|
|
211
|
+
sanitizeRichText,
|
|
212
|
+
toVideoEmbedUrl
|
|
213
|
+
});
|
|
214
|
+
//# sourceMappingURL=server.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server/index.ts","../src/shared/embed.ts"],"sourcesContent":["import sanitizeHtml from \"sanitize-html\";\nimport {\n ALL_EMBED_PROVIDERS,\n EMBED_PROVIDERS,\n type EmbedProvider,\n isAllowedEmbedSrc,\n normalizeLinkUrl,\n toVideoEmbedUrl,\n} from \"../shared/embed\";\n\nexport { ALL_EMBED_PROVIDERS, EMBED_PROVIDERS, isAllowedEmbedSrc, normalizeLinkUrl, toVideoEmbedUrl };\nexport type { EmbedProvider };\n\nexport type SanitizeRichTextOptions = {\n /** Iframe providers kept in the output. Default: aparat + youtube. Must match the editor. */\n allowedEmbeds?: readonly EmbedProvider[];\n /** Extra image filter (e.g. only your bucket). Images must always be https; rejected img tags are removed. */\n isAllowedImageSrc?: (src: string) => boolean;\n};\n\nconst COLOR = [\n /^#[0-9a-f]{3,8}$/i,\n /^rgba?\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*(,\\s*(0|1|0?\\.\\d+)\\s*)?\\)$/i,\n];\n\nconst IFRAME_SANDBOX = \"allow-scripts allow-same-origin allow-presentation\";\n\n/** Allowlist sanitizer for editor output. Run it when saving AND again before rendering. */\nexport function sanitizeRichText(html: string, options: SanitizeRichTextOptions = {}): string {\n const embeds = options.allowedEmbeds ?? ALL_EMBED_PROVIDERS;\n const extraImageFilter = options.isAllowedImageSrc;\n return sanitizeHtml(html, {\n allowedTags: [\n \"p\", \"br\", \"strong\", \"em\", \"u\", \"s\", \"h2\", \"h3\", \"h4\", \"ul\", \"ol\", \"li\", \"blockquote\",\n \"pre\", \"code\", \"a\", \"hr\", \"img\", \"table\", \"thead\", \"tbody\", \"tr\", \"th\", \"td\", \"mark\",\n \"span\", \"iframe\",\n ],\n allowedAttributes: {\n a: [\"href\", \"target\", \"rel\"],\n img: [\"src\", \"alt\", \"width\", \"height\", \"loading\"],\n td: [\"colspan\", \"rowspan\"],\n th: [\"colspan\", \"rowspan\"],\n mark: [\"data-color\"],\n iframe: [\"src\", \"sandbox\", \"loading\", \"allowfullscreen\"],\n \"*\": [\"style\"],\n },\n allowedStyles: {\n \"*\": {\n \"text-align\": [/^(right|left|center|justify)$/],\n color: COLOR,\n \"background-color\": COLOR,\n },\n },\n allowedSchemes: [\"http\", \"https\", \"mailto\", \"tel\"],\n allowedSchemesByTag: { img: [\"https\"], iframe: [\"https\"] },\n allowProtocolRelative: false,\n allowedIframeHostnames: embeds.map((name) => EMBED_PROVIDERS[name].hostname),\n transformTags: {\n a: (tagName, attribs) => {\n const rest = { ...attribs };\n delete rest.target;\n const external = /^https?:\\/\\//i.test(attribs.href ?? \"\");\n return {\n tagName,\n attribs: { ...rest, rel: \"noopener noreferrer nofollow\", ...(external ? { target: \"_blank\" } : {}) },\n };\n },\n img: (tagName, attribs) => ({ tagName, attribs: { ...attribs, loading: \"lazy\" } }),\n // Rebuild iframe attributes from scratch: nothing from the input except src survives.\n iframe: (tagName, attribs) => ({\n tagName,\n attribs: { src: attribs.src ?? \"\", sandbox: IFRAME_SANDBOX, loading: \"lazy\", allowfullscreen: \"true\" },\n }),\n },\n exclusiveFilter: (frame) => {\n const src = frame.attribs.src ?? \"\";\n if (frame.tag === \"img\") return !(src.startsWith(\"https://\") && (extraImageFilter?.(src) ?? true));\n if (frame.tag === \"iframe\") return !isAllowedEmbedSrc(src, embeds);\n return false;\n },\n });\n}\n\n/** Plain text (no tags): empty checks and SEO descriptions. */\nexport function richTextToPlain(html: string): string {\n return sanitizeHtml(html, { allowedTags: [], allowedAttributes: {} })\n .replace(/ | /g, \" \")\n .replace(/&/g, \"&\")\n .replace(/\\s+/g, \" \")\n .trim();\n}\n\n/** True when the HTML has real content (text, image, allowed video or table) after sanitizing. */\nexport function hasRichContent(html: string, options: SanitizeRichTextOptions = {}): boolean {\n const clean = sanitizeRichText(html, options);\n return Boolean(richTextToPlain(clean)) || /<(img|iframe|table)\\b/.test(clean);\n}\n","export type EmbedProvider = \"aparat\" | \"youtube\";\n\nexport const ALL_EMBED_PROVIDERS: readonly EmbedProvider[] = [\"aparat\", \"youtube\"];\n\ntype ProviderConfig = {\n hostname: string;\n /** Every accepted iframe src must start with this exact string. */\n prefix: string;\n /** Base used when building an embed URL from a watch link. */\n embedBase: string;\n};\n\nexport const EMBED_PROVIDERS: Record<EmbedProvider, ProviderConfig> = {\n aparat: {\n hostname: \"www.aparat.com\",\n prefix: \"https://www.aparat.com/video/video/embed/\",\n embedBase: \"https://www.aparat.com/video/video/embed/videohash/\",\n },\n youtube: {\n hostname: \"www.youtube-nocookie.com\",\n prefix: \"https://www.youtube-nocookie.com/embed/\",\n embedBase: \"https://www.youtube-nocookie.com/embed/\",\n },\n};\n\nconst YOUTUBE_ID = /^[A-Za-z0-9_-]{6,20}$/;\nconst APARAT_ID = /^[A-Za-z0-9]{3,20}$/;\n\n/** Watch link (Aparat / YouTube) to a safe embed URL; anything else returns null. */\nexport function toVideoEmbedUrl(\n input: string,\n allowed: readonly EmbedProvider[] = ALL_EMBED_PROVIDERS,\n): string | null {\n let url: URL;\n try {\n url = new URL(input.trim());\n } catch {\n return null;\n }\n if (url.protocol !== \"https:\") return null;\n const host = url.hostname.replace(/^(www\\.|m\\.)/, \"\");\n const segments = url.pathname.split(\"/\").filter(Boolean);\n\n if (host === \"aparat.com\") {\n if (!allowed.includes(\"aparat\")) return null;\n const id =\n segments[0] === \"v\" ? segments[1] : segments.at(-1) === \"frame\" ? segments.at(-3) : undefined;\n return id && APARAT_ID.test(id) ? `${EMBED_PROVIDERS.aparat.embedBase}${id}/vt/frame` : null;\n }\n\n if (!allowed.includes(\"youtube\")) return null;\n let id: string | null | undefined;\n if (host === \"youtu.be\") id = segments[0];\n else if (host === \"youtube.com\" || host === \"youtube-nocookie.com\") {\n id = [\"embed\", \"shorts\", \"live\"].includes(segments[0] ?? \"\") ? segments[1] : url.searchParams.get(\"v\");\n }\n return id && YOUTUBE_ID.test(id) ? `${EMBED_PROVIDERS.youtube.embedBase}${id}` : null;\n}\n\n/** Strict allowlist check for an iframe src (exact host + path prefix, https, no credentials). */\nexport function isAllowedEmbedSrc(\n src: string,\n allowed: readonly EmbedProvider[] = ALL_EMBED_PROVIDERS,\n): boolean {\n if (/[\\s\\\\\\u0000-\\u001f]/.test(src)) return false;\n let url: URL;\n try {\n url = new URL(src);\n } catch {\n return false;\n }\n if (url.protocol !== \"https:\" || url.username || url.password || url.port) return false;\n return allowed.some((name) => {\n const provider = EMBED_PROVIDERS[name];\n return (\n src.startsWith(provider.prefix) &&\n url.hostname === provider.hostname &&\n url.pathname.startsWith(new URL(provider.prefix).pathname)\n );\n });\n}\n\n/** Link href: internal path, http(s), mailto/tel; no scheme means https. javascript:/data: are rejected. */\nexport function normalizeLinkUrl(input: string): string | null {\n const value = input.trim();\n if (!value) return null;\n if (value.startsWith(\"//\")) return null;\n if (value.startsWith(\"/\")) return value;\n if (/^(mailto|tel):[^\\s]+$/i.test(value)) return value;\n try {\n const url = new URL(/^[a-z][a-z0-9+.-]*:/i.test(value) ? value : `https://${value}`);\n return url.protocol === \"http:\" || url.protocol === \"https:\" ? url.toString() : null;\n } catch {\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAAyB;;;ACElB,IAAM,sBAAgD,CAAC,UAAU,SAAS;AAU1E,IAAM,kBAAyD;AAAA,EACpE,QAAQ;AAAA,IACN,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,WAAW;AAAA,EACb;AAAA,EACA,SAAS;AAAA,IACP,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,WAAW;AAAA,EACb;AACF;AAEA,IAAM,aAAa;AACnB,IAAM,YAAY;AAGX,SAAS,gBACd,OACA,UAAoC,qBACrB;AACf,MAAI;AACJ,MAAI;AACF,UAAM,IAAI,IAAI,MAAM,KAAK,CAAC;AAAA,EAC5B,QAAQ;AACN,W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SAAU,QAAO,CAAC,kBAAkB,KAAK,MAAM;AACjE,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;AAGO,SAAS,gBAAgB,MAAsB;AACpD,aAAO,qBAAAA,SAAa,MAAM,EAAE,aAAa,CAAC,GAAG,mBAAmB,CAAC,EAAE,CAAC,EACjE,QAAQ,aAAa,GAAG,EACxB,QAAQ,UAAU,GAAG,EACrB,QAAQ,QAAQ,GAAG,EACnB,KAAK;AACV;AAGO,SAAS,eAAe,MAAc,UAAmC,CAAC,GAAY;AAC3F,QAAM,QAAQ,iBAAiB,MAAM,OAAO;AAC5C,SAAO,QAAQ,gBAAgB,KAAK,CAAC,KAAK,wBAAwB,KAAK,KAAK;AAC9E;","names":["id","sanitizeHtml"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type EmbedProvider = "aparat" | "youtube";
|
|
2
|
+
declare const ALL_EMBED_PROVIDERS: readonly EmbedProvider[];
|
|
3
|
+
type ProviderConfig = {
|
|
4
|
+
hostname: string;
|
|
5
|
+
/** Every accepted iframe src must start with this exact string. */
|
|
6
|
+
prefix: string;
|
|
7
|
+
/** Base used when building an embed URL from a watch link. */
|
|
8
|
+
embedBase: string;
|
|
9
|
+
};
|
|
10
|
+
declare const EMBED_PROVIDERS: Record<EmbedProvider, ProviderConfig>;
|
|
11
|
+
/** Watch link (Aparat / YouTube) to a safe embed URL; anything else returns null. */
|
|
12
|
+
declare function toVideoEmbedUrl(input: string, allowed?: readonly EmbedProvider[]): string | null;
|
|
13
|
+
/** Strict allowlist check for an iframe src (exact host + path prefix, https, no credentials). */
|
|
14
|
+
declare function isAllowedEmbedSrc(src: string, allowed?: readonly EmbedProvider[]): boolean;
|
|
15
|
+
/** Link href: internal path, http(s), mailto/tel; no scheme means https. javascript:/data: are rejected. */
|
|
16
|
+
declare function normalizeLinkUrl(input: string): string | null;
|
|
17
|
+
|
|
18
|
+
type SanitizeRichTextOptions = {
|
|
19
|
+
/** Iframe providers kept in the output. Default: aparat + youtube. Must match the editor. */
|
|
20
|
+
allowedEmbeds?: readonly EmbedProvider[];
|
|
21
|
+
/** Extra image filter (e.g. only your bucket). Images must always be https; rejected img tags are removed. */
|
|
22
|
+
isAllowedImageSrc?: (src: string) => boolean;
|
|
23
|
+
};
|
|
24
|
+
/** Allowlist sanitizer for editor output. Run it when saving AND again before rendering. */
|
|
25
|
+
declare function sanitizeRichText(html: string, options?: SanitizeRichTextOptions): string;
|
|
26
|
+
/** Plain text (no tags): empty checks and SEO descriptions. */
|
|
27
|
+
declare function richTextToPlain(html: string): string;
|
|
28
|
+
/** True when the HTML has real content (text, image, allowed video or table) after sanitizing. */
|
|
29
|
+
declare function hasRichContent(html: string, options?: SanitizeRichTextOptions): boolean;
|
|
30
|
+
|
|
31
|
+
export { ALL_EMBED_PROVIDERS, EMBED_PROVIDERS, type EmbedProvider, type SanitizeRichTextOptions, hasRichContent, isAllowedEmbedSrc, normalizeLinkUrl, richTextToPlain, sanitizeRichText, toVideoEmbedUrl };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type EmbedProvider = "aparat" | "youtube";
|
|
2
|
+
declare const ALL_EMBED_PROVIDERS: readonly EmbedProvider[];
|
|
3
|
+
type ProviderConfig = {
|
|
4
|
+
hostname: string;
|
|
5
|
+
/** Every accepted iframe src must start with this exact string. */
|
|
6
|
+
prefix: string;
|
|
7
|
+
/** Base used when building an embed URL from a watch link. */
|
|
8
|
+
embedBase: string;
|
|
9
|
+
};
|
|
10
|
+
declare const EMBED_PROVIDERS: Record<EmbedProvider, ProviderConfig>;
|
|
11
|
+
/** Watch link (Aparat / YouTube) to a safe embed URL; anything else returns null. */
|
|
12
|
+
declare function toVideoEmbedUrl(input: string, allowed?: readonly EmbedProvider[]): string | null;
|
|
13
|
+
/** Strict allowlist check for an iframe src (exact host + path prefix, https, no credentials). */
|
|
14
|
+
declare function isAllowedEmbedSrc(src: string, allowed?: readonly EmbedProvider[]): boolean;
|
|
15
|
+
/** Link href: internal path, http(s), mailto/tel; no scheme means https. javascript:/data: are rejected. */
|
|
16
|
+
declare function normalizeLinkUrl(input: string): string | null;
|
|
17
|
+
|
|
18
|
+
type SanitizeRichTextOptions = {
|
|
19
|
+
/** Iframe providers kept in the output. Default: aparat + youtube. Must match the editor. */
|
|
20
|
+
allowedEmbeds?: readonly EmbedProvider[];
|
|
21
|
+
/** Extra image filter (e.g. only your bucket). Images must always be https; rejected img tags are removed. */
|
|
22
|
+
isAllowedImageSrc?: (src: string) => boolean;
|
|
23
|
+
};
|
|
24
|
+
/** Allowlist sanitizer for editor output. Run it when saving AND again before rendering. */
|
|
25
|
+
declare function sanitizeRichText(html: string, options?: SanitizeRichTextOptions): string;
|
|
26
|
+
/** Plain text (no tags): empty checks and SEO descriptions. */
|
|
27
|
+
declare function richTextToPlain(html: string): string;
|
|
28
|
+
/** True when the HTML has real content (text, image, allowed video or table) after sanitizing. */
|
|
29
|
+
declare function hasRichContent(html: string, options?: SanitizeRichTextOptions): boolean;
|
|
30
|
+
|
|
31
|
+
export { ALL_EMBED_PROVIDERS, EMBED_PROVIDERS, type EmbedProvider, type SanitizeRichTextOptions, hasRichContent, isAllowedEmbedSrc, normalizeLinkUrl, richTextToPlain, sanitizeRichText, toVideoEmbedUrl };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// src/server/index.ts
|
|
2
|
+
import sanitizeHtml from "sanitize-html";
|
|
3
|
+
|
|
4
|
+
// src/shared/embed.ts
|
|
5
|
+
var ALL_EMBED_PROVIDERS = ["aparat", "youtube"];
|
|
6
|
+
var EMBED_PROVIDERS = {
|
|
7
|
+
aparat: {
|
|
8
|
+
hostname: "www.aparat.com",
|
|
9
|
+
prefix: "https://www.aparat.com/video/video/embed/",
|
|
10
|
+
embedBase: "https://www.aparat.com/video/video/embed/videohash/"
|
|
11
|
+
},
|
|
12
|
+
youtube: {
|
|
13
|
+
hostname: "www.youtube-nocookie.com",
|
|
14
|
+
prefix: "https://www.youtube-nocookie.com/embed/",
|
|
15
|
+
embedBase: "https://www.youtube-nocookie.com/embed/"
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var YOUTUBE_ID = /^[A-Za-z0-9_-]{6,20}$/;
|
|
19
|
+
var APARAT_ID = /^[A-Za-z0-9]{3,20}$/;
|
|
20
|
+
function toVideoEmbedUrl(input, allowed = ALL_EMBED_PROVIDERS) {
|
|
21
|
+
let url;
|
|
22
|
+
try {
|
|
23
|
+
url = new URL(input.trim());
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
if (url.protocol !== "https:") return null;
|
|
28
|
+
const host = url.hostname.replace(/^(www\.|m\.)/, "");
|
|
29
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
30
|
+
if (host === "aparat.com") {
|
|
31
|
+
if (!allowed.includes("aparat")) return null;
|
|
32
|
+
const id2 = segments[0] === "v" ? segments[1] : segments.at(-1) === "frame" ? segments.at(-3) : void 0;
|
|
33
|
+
return id2 && APARAT_ID.test(id2) ? `${EMBED_PROVIDERS.aparat.embedBase}${id2}/vt/frame` : null;
|
|
34
|
+
}
|
|
35
|
+
if (!allowed.includes("youtube")) return null;
|
|
36
|
+
let id;
|
|
37
|
+
if (host === "youtu.be") id = segments[0];
|
|
38
|
+
else if (host === "youtube.com" || host === "youtube-nocookie.com") {
|
|
39
|
+
id = ["embed", "shorts", "live"].includes(segments[0] ?? "") ? segments[1] : url.searchParams.get("v");
|
|
40
|
+
}
|
|
41
|
+
return id && YOUTUBE_ID.test(id) ? `${EMBED_PROVIDERS.youtube.embedBase}${id}` : null;
|
|
42
|
+
}
|
|
43
|
+
function isAllowedEmbedSrc(src, allowed = ALL_EMBED_PROVIDERS) {
|
|
44
|
+
if (/[\s\\\u0000-\u001f]/.test(src)) return false;
|
|
45
|
+
let url;
|
|
46
|
+
try {
|
|
47
|
+
url = new URL(src);
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (url.protocol !== "https:" || url.username || url.password || url.port) return false;
|
|
52
|
+
return allowed.some((name) => {
|
|
53
|
+
const provider = EMBED_PROVIDERS[name];
|
|
54
|
+
return src.startsWith(provider.prefix) && url.hostname === provider.hostname && url.pathname.startsWith(new URL(provider.prefix).pathname);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function normalizeLinkUrl(input) {
|
|
58
|
+
const value = input.trim();
|
|
59
|
+
if (!value) return null;
|
|
60
|
+
if (value.startsWith("//")) return null;
|
|
61
|
+
if (value.startsWith("/")) return value;
|
|
62
|
+
if (/^(mailto|tel):[^\s]+$/i.test(value)) return value;
|
|
63
|
+
try {
|
|
64
|
+
const url = new URL(/^[a-z][a-z0-9+.-]*:/i.test(value) ? value : `https://${value}`);
|
|
65
|
+
return url.protocol === "http:" || url.protocol === "https:" ? url.toString() : null;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/server/index.ts
|
|
72
|
+
var COLOR = [
|
|
73
|
+
/^#[0-9a-f]{3,8}$/i,
|
|
74
|
+
/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*(0|1|0?\.\d+)\s*)?\)$/i
|
|
75
|
+
];
|
|
76
|
+
var IFRAME_SANDBOX = "allow-scripts allow-same-origin allow-presentation";
|
|
77
|
+
function sanitizeRichText(html, options = {}) {
|
|
78
|
+
const embeds = options.allowedEmbeds ?? ALL_EMBED_PROVIDERS;
|
|
79
|
+
const extraImageFilter = options.isAllowedImageSrc;
|
|
80
|
+
return sanitizeHtml(html, {
|
|
81
|
+
allowedTags: [
|
|
82
|
+
"p",
|
|
83
|
+
"br",
|
|
84
|
+
"strong",
|
|
85
|
+
"em",
|
|
86
|
+
"u",
|
|
87
|
+
"s",
|
|
88
|
+
"h2",
|
|
89
|
+
"h3",
|
|
90
|
+
"h4",
|
|
91
|
+
"ul",
|
|
92
|
+
"ol",
|
|
93
|
+
"li",
|
|
94
|
+
"blockquote",
|
|
95
|
+
"pre",
|
|
96
|
+
"code",
|
|
97
|
+
"a",
|
|
98
|
+
"hr",
|
|
99
|
+
"img",
|
|
100
|
+
"table",
|
|
101
|
+
"thead",
|
|
102
|
+
"tbody",
|
|
103
|
+
"tr",
|
|
104
|
+
"th",
|
|
105
|
+
"td",
|
|
106
|
+
"mark",
|
|
107
|
+
"span",
|
|
108
|
+
"iframe"
|
|
109
|
+
],
|
|
110
|
+
allowedAttributes: {
|
|
111
|
+
a: ["href", "target", "rel"],
|
|
112
|
+
img: ["src", "alt", "width", "height", "loading"],
|
|
113
|
+
td: ["colspan", "rowspan"],
|
|
114
|
+
th: ["colspan", "rowspan"],
|
|
115
|
+
mark: ["data-color"],
|
|
116
|
+
iframe: ["src", "sandbox", "loading", "allowfullscreen"],
|
|
117
|
+
"*": ["style"]
|
|
118
|
+
},
|
|
119
|
+
allowedStyles: {
|
|
120
|
+
"*": {
|
|
121
|
+
"text-align": [/^(right|left|center|justify)$/],
|
|
122
|
+
color: COLOR,
|
|
123
|
+
"background-color": COLOR
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
allowedSchemes: ["http", "https", "mailto", "tel"],
|
|
127
|
+
allowedSchemesByTag: { img: ["https"], iframe: ["https"] },
|
|
128
|
+
allowProtocolRelative: false,
|
|
129
|
+
allowedIframeHostnames: embeds.map((name) => EMBED_PROVIDERS[name].hostname),
|
|
130
|
+
transformTags: {
|
|
131
|
+
a: (tagName, attribs) => {
|
|
132
|
+
const rest = { ...attribs };
|
|
133
|
+
delete rest.target;
|
|
134
|
+
const external = /^https?:\/\//i.test(attribs.href ?? "");
|
|
135
|
+
return {
|
|
136
|
+
tagName,
|
|
137
|
+
attribs: { ...rest, rel: "noopener noreferrer nofollow", ...external ? { target: "_blank" } : {} }
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
img: (tagName, attribs) => ({ tagName, attribs: { ...attribs, loading: "lazy" } }),
|
|
141
|
+
// Rebuild iframe attributes from scratch: nothing from the input except src survives.
|
|
142
|
+
iframe: (tagName, attribs) => ({
|
|
143
|
+
tagName,
|
|
144
|
+
attribs: { src: attribs.src ?? "", sandbox: IFRAME_SANDBOX, loading: "lazy", allowfullscreen: "true" }
|
|
145
|
+
})
|
|
146
|
+
},
|
|
147
|
+
exclusiveFilter: (frame) => {
|
|
148
|
+
const src = frame.attribs.src ?? "";
|
|
149
|
+
if (frame.tag === "img") return !(src.startsWith("https://") && (extraImageFilter?.(src) ?? true));
|
|
150
|
+
if (frame.tag === "iframe") return !isAllowedEmbedSrc(src, embeds);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function richTextToPlain(html) {
|
|
156
|
+
return sanitizeHtml(html, { allowedTags: [], allowedAttributes: {} }).replace(/ | /g, " ").replace(/&/g, "&").replace(/\s+/g, " ").trim();
|
|
157
|
+
}
|
|
158
|
+
function hasRichContent(html, options = {}) {
|
|
159
|
+
const clean = sanitizeRichText(html, options);
|
|
160
|
+
return Boolean(richTextToPlain(clean)) || /<(img|iframe|table)\b/.test(clean);
|
|
161
|
+
}
|
|
162
|
+
export {
|
|
163
|
+
ALL_EMBED_PROVIDERS,
|
|
164
|
+
EMBED_PROVIDERS,
|
|
165
|
+
hasRichContent,
|
|
166
|
+
isAllowedEmbedSrc,
|
|
167
|
+
normalizeLinkUrl,
|
|
168
|
+
richTextToPlain,
|
|
169
|
+
sanitizeRichText,
|
|
170
|
+
toVideoEmbedUrl
|
|
171
|
+
};
|
|
172
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server/index.ts","../src/shared/embed.ts"],"sourcesContent":["import sanitizeHtml from \"sanitize-html\";\nimport {\n ALL_EMBED_PROVIDERS,\n EMBED_PROVIDERS,\n type EmbedProvider,\n isAllowedEmbedSrc,\n normalizeLinkUrl,\n toVideoEmbedUrl,\n} from \"../shared/embed\";\n\nexport { ALL_EMBED_PROVIDERS, EMBED_PROVIDERS, isAllowedEmbedSrc, normalizeLinkUrl, toVideoEmbedUrl };\nexport type { EmbedProvider };\n\nexport type SanitizeRichTextOptions = {\n /** Iframe providers kept in the output. Default: aparat + youtube. Must match the editor. */\n allowedEmbeds?: readonly EmbedProvider[];\n /** Extra image filter (e.g. only your bucket). Images must always be https; rejected img tags are removed. */\n isAllowedImageSrc?: (src: string) => boolean;\n};\n\nconst COLOR = [\n /^#[0-9a-f]{3,8}$/i,\n /^rgba?\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*(,\\s*(0|1|0?\\.\\d+)\\s*)?\\)$/i,\n];\n\nconst IFRAME_SANDBOX = \"allow-scripts allow-same-origin allow-presentation\";\n\n/** Allowlist sanitizer for editor output. Run it when saving AND again before rendering. */\nexport function sanitizeRichText(html: string, options: SanitizeRichTextOptions = {}): string {\n const embeds = options.allowedEmbeds ?? ALL_EMBED_PROVIDERS;\n const extraImageFilter = options.isAllowedImageSrc;\n return sanitizeHtml(html, {\n allowedTags: [\n \"p\", \"br\", \"strong\", \"em\", \"u\", \"s\", \"h2\", \"h3\", \"h4\", \"ul\", \"ol\", \"li\", \"blockquote\",\n \"pre\", \"code\", \"a\", \"hr\", \"img\", \"table\", \"thead\", \"tbody\", \"tr\", \"th\", \"td\", \"mark\",\n \"span\", \"iframe\",\n ],\n allowedAttributes: {\n a: [\"href\", \"target\", \"rel\"],\n img: [\"src\", \"alt\", \"width\", \"height\", \"loading\"],\n td: [\"colspan\", \"rowspan\"],\n th: [\"colspan\", \"rowspan\"],\n mark: [\"data-color\"],\n iframe: [\"src\", \"sandbox\", \"loading\", \"allowfullscreen\"],\n \"*\": [\"style\"],\n },\n allowedStyles: {\n \"*\": {\n \"text-align\": [/^(right|left|center|justify)$/],\n color: COLOR,\n \"background-color\": COLOR,\n },\n },\n allowedSchemes: [\"http\", \"https\", \"mailto\", \"tel\"],\n allowedSchemesByTag: { img: [\"https\"], iframe: [\"https\"] },\n allowProtocolRelative: false,\n allowedIframeHostnames: embeds.map((name) => EMBED_PROVIDERS[name].hostname),\n transformTags: {\n a: (tagName, attribs) => {\n const rest = { ...attribs };\n delete rest.target;\n const external = /^https?:\\/\\//i.test(attribs.href ?? \"\");\n return {\n tagName,\n attribs: { ...rest, rel: \"noopener noreferrer nofollow\", ...(external ? { target: \"_blank\" } : {}) },\n };\n },\n img: (tagName, attribs) => ({ tagName, attribs: { ...attribs, loading: \"lazy\" } }),\n // Rebuild iframe attributes from scratch: nothing from the input except src survives.\n iframe: (tagName, attribs) => ({\n tagName,\n attribs: { src: attribs.src ?? \"\", sandbox: IFRAME_SANDBOX, loading: \"lazy\", allowfullscreen: \"true\" },\n }),\n },\n exclusiveFilter: (frame) => {\n const src = frame.attribs.src ?? \"\";\n if (frame.tag === \"img\") return !(src.startsWith(\"https://\") && (extraImageFilter?.(src) ?? true));\n if (frame.tag === \"iframe\") return !isAllowedEmbedSrc(src, embeds);\n return false;\n },\n });\n}\n\n/** Plain text (no tags): empty checks and SEO descriptions. */\nexport function richTextToPlain(html: string): string {\n return sanitizeHtml(html, { allowedTags: [], allowedAttributes: {} })\n .replace(/ | /g, \" \")\n .replace(/&/g, \"&\")\n .replace(/\\s+/g, \" \")\n .trim();\n}\n\n/** True when the HTML has real content (text, image, allowed video or table) after sanitizing. */\nexport function hasRichContent(html: string, options: SanitizeRichTextOptions = {}): boolean {\n const clean = sanitizeRichText(html, options);\n return Boolean(richTextToPlain(clean)) || /<(img|iframe|table)\\b/.test(clean);\n}\n","export type EmbedProvider = \"aparat\" | \"youtube\";\n\nexport const ALL_EMBED_PROVIDERS: readonly EmbedProvider[] = [\"aparat\", \"youtube\"];\n\ntype ProviderConfig = {\n hostname: string;\n /** Every accepted iframe src must start with this exact string. */\n prefix: string;\n /** Base used when building an embed URL from a watch link. */\n embedBase: string;\n};\n\nexport const EMBED_PROVIDERS: Record<EmbedProvider, ProviderConfig> = {\n aparat: {\n hostname: \"www.aparat.com\",\n prefix: \"https://www.aparat.com/video/video/embed/\",\n embedBase: \"https://www.aparat.com/video/video/embed/videohash/\",\n },\n youtube: {\n hostname: \"www.youtube-nocookie.com\",\n prefix: \"https://www.youtube-nocookie.com/embed/\",\n embedBase: \"https://www.youtube-nocookie.com/embed/\",\n },\n};\n\nconst YOUTUBE_ID = /^[A-Za-z0-9_-]{6,20}$/;\nconst APARAT_ID = /^[A-Za-z0-9]{3,20}$/;\n\n/** Watch link (Aparat / YouTube) to a safe embed URL; anything else returns null. */\nexport function toVideoEmbedUrl(\n input: string,\n allowed: readonly EmbedProvider[] = ALL_EMBED_PROVIDERS,\n): string | null {\n let url: URL;\n try {\n url = new URL(input.trim());\n } catch {\n return null;\n }\n if (url.protocol !== \"https:\") return null;\n const host = url.hostname.replace(/^(www\\.|m\\.)/, \"\");\n const segments = url.pathname.split(\"/\").filter(Boolean);\n\n if (host === \"aparat.com\") {\n if (!allowed.includes(\"aparat\")) return null;\n const id =\n segments[0] === \"v\" ? segments[1] : segments.at(-1) === \"frame\" ? segments.at(-3) : undefined;\n return id && APARAT_ID.test(id) ? `${EMBED_PROVIDERS.aparat.embedBase}${id}/vt/frame` : null;\n }\n\n if (!allowed.includes(\"youtube\")) return null;\n let id: string | null | undefined;\n if (host === \"youtu.be\") id = segments[0];\n else if (host === \"youtube.com\" || host === \"youtube-nocookie.com\") {\n id = [\"embed\", \"shorts\", \"live\"].includes(segments[0] ?? \"\") ? segments[1] : url.searchParams.get(\"v\");\n }\n return id && YOUTUBE_ID.test(id) ? `${EMBED_PROVIDERS.youtube.embedBase}${id}` : null;\n}\n\n/** Strict allowlist check for an iframe src (exact host + path prefix, https, no credentials). */\nexport function isAllowedEmbedSrc(\n src: string,\n allowed: readonly EmbedProvider[] = ALL_EMBED_PROVIDERS,\n): boolean {\n if (/[\\s\\\\\\u0000-\\u001f]/.test(src)) return false;\n let url: URL;\n try {\n url = new URL(src);\n } catch {\n return false;\n }\n if (url.protocol !== \"https:\" || url.username || url.password || url.port) return false;\n return allowed.some((name) => {\n const provider = EMBED_PROVIDERS[name];\n return (\n src.startsWith(provider.prefix) &&\n url.hostname === provider.hostname &&\n url.pathname.startsWith(new URL(provider.prefix).pathname)\n );\n });\n}\n\n/** Link href: internal path, http(s), mailto/tel; no scheme means https. javascript:/data: are rejected. */\nexport function normalizeLinkUrl(input: string): string | null {\n const value = input.trim();\n if (!value) return null;\n if (value.startsWith(\"//\")) return null;\n if (value.startsWith(\"/\")) return value;\n if (/^(mailto|tel):[^\\s]+$/i.test(value)) return value;\n try {\n const url = new URL(/^[a-z][a-z0-9+.-]*:/i.test(value) ? value : `https://${value}`);\n return url.protocol === \"http:\" || url.protocol === \"https:\" ? url.toString() : null;\n } catch {\n return null;\n }\n}\n"],"mappings":";AAAA,OAAO,kBAAkB;;;ACElB,IAAM,sBAAgD,CAAC,UAAU,SAAS;AAU1E,IAAM,kBAAyD;AAAA,EACpE,QAAQ;AAAA,IACN,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,WAAW;AAAA,EACb;AAAA,EACA,SAAS;AAAA,IACP,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,WAAW;AAAA,EACb;AACF;AAEA,IAAM,aAAa;AACnB,IAAM,YAAY;AAGX,SAAS,gBACd,OACA,UAAoC,qBACrB;AACf,MAAI;AACJ,MAAI;AACF,UAAM,IAAI,IAAI,MAAM,KAAK,CAAC;AAAA,EAC5B,QAAQ;AACN,WAAO;AAAA,EACT;AACA,MAAI,IAAI,aAAa,SAAU,QAAO;AACtC,QAAM,OAAO,IAAI,SAAS,QAAQ,gBAAgB,EAAE;AACpD,QAAM,WAAW,IAAI,SAAS,MAAM,GAAG,EAAE,OAAO,OAAO;AAEvD,MAAI,SAAS,cAAc;AACzB,QAAI,CAAC,QAAQ,SAAS,QAAQ,EAAG,QAAO;AACxC,UAAMA,MACJ,SAAS,CAAC,MAAM,MAAM,SAA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