@quickbuilder.io/rich-text 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +122 -2
- package/adapters.d.ts +1 -0
- package/adapters.js +1 -0
- package/collaboration.d.ts +1 -0
- package/collaboration.js +1 -0
- package/commands.d.ts +1 -0
- package/commands.js +1 -0
- package/content.d.ts +1 -0
- package/content.js +1 -0
- package/contracts.d.ts +1 -0
- package/contracts.js +1 -0
- package/dist/adapters.d.mts +11 -0
- package/dist/adapters.d.ts +11 -0
- package/dist/adapters.js +564 -0
- package/dist/adapters.js.map +1 -0
- package/dist/adapters.mjs +557 -0
- package/dist/adapters.mjs.map +1 -0
- package/dist/collaboration.d.mts +29 -0
- package/dist/collaboration.d.ts +29 -0
- package/dist/collaboration.js +758 -0
- package/dist/collaboration.js.map +1 -0
- package/dist/collaboration.mjs +733 -0
- package/dist/collaboration.mjs.map +1 -0
- package/dist/commands.d.mts +8 -0
- package/dist/commands.d.ts +8 -0
- package/dist/commands.js +42 -0
- package/dist/commands.js.map +1 -0
- package/dist/commands.mjs +38 -0
- package/dist/commands.mjs.map +1 -0
- package/dist/content-types-CYHsrMrs.d.mts +27 -0
- package/dist/content-types-CYHsrMrs.d.ts +27 -0
- package/dist/content.d.mts +19 -0
- package/dist/content.d.ts +19 -0
- package/dist/content.js +785 -0
- package/dist/content.js.map +1 -0
- package/dist/content.mjs +775 -0
- package/dist/content.mjs.map +1 -0
- package/dist/contracts.d.mts +199 -0
- package/dist/contracts.d.ts +199 -0
- package/dist/contracts.js +4 -0
- package/dist/contracts.js.map +1 -0
- package/dist/contracts.mjs +3 -0
- package/dist/contracts.mjs.map +1 -0
- package/dist/extensions.d.mts +363 -0
- package/dist/extensions.d.ts +363 -0
- package/dist/extensions.js +153 -0
- package/dist/extensions.js.map +1 -0
- package/dist/extensions.mjs +142 -0
- package/dist/extensions.mjs.map +1 -0
- package/dist/history.d.mts +12 -0
- package/dist/history.d.ts +12 -0
- package/dist/history.js +464 -0
- package/dist/history.js.map +1 -0
- package/dist/history.mjs +459 -0
- package/dist/history.mjs.map +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1438 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1372 -0
- package/dist/index.mjs.map +1 -0
- package/dist/markdown.d.mts +32 -0
- package/dist/markdown.d.ts +32 -0
- package/dist/markdown.js +739 -0
- package/dist/markdown.js.map +1 -0
- package/dist/markdown.mjs +735 -0
- package/dist/markdown.mjs.map +1 -0
- package/dist/runtime/dom.d.mts +148 -0
- package/dist/runtime/dom.d.ts +148 -0
- package/dist/runtime/dom.js +2145 -0
- package/dist/runtime/dom.js.map +1 -0
- package/dist/runtime/dom.mjs +2084 -0
- package/dist/runtime/dom.mjs.map +1 -0
- package/dist/schema.d.mts +23 -0
- package/dist/schema.d.ts +23 -0
- package/dist/schema.js +157 -0
- package/dist/schema.js.map +1 -0
- package/dist/schema.mjs +137 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/serializer.d.mts +38 -0
- package/dist/serializer.d.ts +38 -0
- package/dist/serializer.js +504 -0
- package/dist/serializer.js.map +1 -0
- package/dist/serializer.mjs +491 -0
- package/dist/serializer.mjs.map +1 -0
- package/dist/svg-export-BtI8x_mG.d.mts +15 -0
- package/dist/svg-export-BtI8x_mG.d.ts +15 -0
- package/dist/transactions.d.mts +16 -0
- package/dist/transactions.d.ts +16 -0
- package/dist/transactions.js +637 -0
- package/dist/transactions.js.map +1 -0
- package/dist/transactions.mjs +631 -0
- package/dist/transactions.mjs.map +1 -0
- package/extensions.d.ts +1 -0
- package/extensions.js +1 -0
- package/history.d.ts +1 -0
- package/history.js +1 -0
- package/package.json +352 -4
- package/schema.d.ts +1 -0
- package/schema.js +1 -0
- package/serializer.d.ts +1 -0
- package/serializer.js +1 -0
- package/transactions.d.ts +1 -0
- package/transactions.js +1 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1372 @@
|
|
|
1
|
+
// src/schema.ts
|
|
2
|
+
var QUICK_BUILDER_RICH_TEXT_SCHEMA_VERSION = 2;
|
|
3
|
+
var richTextAllowedTags = [
|
|
4
|
+
"a",
|
|
5
|
+
"b",
|
|
6
|
+
"blockquote",
|
|
7
|
+
"br",
|
|
8
|
+
"code",
|
|
9
|
+
"em",
|
|
10
|
+
"h1",
|
|
11
|
+
"h2",
|
|
12
|
+
"h3",
|
|
13
|
+
"h4",
|
|
14
|
+
"h5",
|
|
15
|
+
"h6",
|
|
16
|
+
"hr",
|
|
17
|
+
"i",
|
|
18
|
+
"img",
|
|
19
|
+
"input",
|
|
20
|
+
"label",
|
|
21
|
+
"li",
|
|
22
|
+
"mark",
|
|
23
|
+
"ol",
|
|
24
|
+
"p",
|
|
25
|
+
"pre",
|
|
26
|
+
"qb-video",
|
|
27
|
+
"s",
|
|
28
|
+
"span",
|
|
29
|
+
"sub",
|
|
30
|
+
"sup",
|
|
31
|
+
"strong",
|
|
32
|
+
"table",
|
|
33
|
+
"tbody",
|
|
34
|
+
"td",
|
|
35
|
+
"tfoot",
|
|
36
|
+
"th",
|
|
37
|
+
"thead",
|
|
38
|
+
"tr",
|
|
39
|
+
"u",
|
|
40
|
+
"ul"
|
|
41
|
+
];
|
|
42
|
+
var richTextVoidTags = ["br", "hr", "img", "input"];
|
|
43
|
+
var richTextAllowedTargets = ["_blank", "_self", "_parent", "_top"];
|
|
44
|
+
var richTextAlignmentTags = [
|
|
45
|
+
"blockquote",
|
|
46
|
+
"h1",
|
|
47
|
+
"h2",
|
|
48
|
+
"h3",
|
|
49
|
+
"h4",
|
|
50
|
+
"h5",
|
|
51
|
+
"h6",
|
|
52
|
+
"li",
|
|
53
|
+
"ol",
|
|
54
|
+
"p",
|
|
55
|
+
"pre",
|
|
56
|
+
"td",
|
|
57
|
+
"th",
|
|
58
|
+
"ul"
|
|
59
|
+
];
|
|
60
|
+
var richTextAllowedAlignments = ["left", "center", "right", "justify"];
|
|
61
|
+
var richTextAllowedFontFamilies = [
|
|
62
|
+
"default",
|
|
63
|
+
"carlito",
|
|
64
|
+
"source-sans-3",
|
|
65
|
+
"noto-sans",
|
|
66
|
+
"arimo",
|
|
67
|
+
"tinos",
|
|
68
|
+
"cousine"
|
|
69
|
+
];
|
|
70
|
+
var richTextAllowedFontSizes = [
|
|
71
|
+
"default",
|
|
72
|
+
"8",
|
|
73
|
+
"9",
|
|
74
|
+
"10",
|
|
75
|
+
"11",
|
|
76
|
+
"12",
|
|
77
|
+
"14",
|
|
78
|
+
"16",
|
|
79
|
+
"18",
|
|
80
|
+
"24",
|
|
81
|
+
"30",
|
|
82
|
+
"36",
|
|
83
|
+
"48",
|
|
84
|
+
"60",
|
|
85
|
+
"72",
|
|
86
|
+
"96"
|
|
87
|
+
];
|
|
88
|
+
var richTextAllowedLineHeights = ["1", "1.15", "1.5", "2"];
|
|
89
|
+
var richTextAllowedColors = [
|
|
90
|
+
"default",
|
|
91
|
+
"gray",
|
|
92
|
+
"brown",
|
|
93
|
+
"orange",
|
|
94
|
+
"yellow",
|
|
95
|
+
"green",
|
|
96
|
+
"blue",
|
|
97
|
+
"purple",
|
|
98
|
+
"pink",
|
|
99
|
+
"red"
|
|
100
|
+
];
|
|
101
|
+
var richTextBlockSelector = "p,h1,h2,h3,pre,blockquote,li,ul,ol,td,th";
|
|
102
|
+
var richTextBlockFormatByTagName = {
|
|
103
|
+
p: "paragraph",
|
|
104
|
+
h1: "heading1",
|
|
105
|
+
h2: "heading2",
|
|
106
|
+
h3: "heading3",
|
|
107
|
+
pre: "codeBlock"
|
|
108
|
+
};
|
|
109
|
+
var richTextBlockFormats = [
|
|
110
|
+
{ value: "paragraph", tagName: "p" },
|
|
111
|
+
{ value: "heading1", tagName: "h1" },
|
|
112
|
+
{ value: "heading2", tagName: "h2" },
|
|
113
|
+
{ value: "heading3", tagName: "h3" },
|
|
114
|
+
{ value: "codeBlock", tagName: "pre" }
|
|
115
|
+
];
|
|
116
|
+
function isRichTextAlignment(value) {
|
|
117
|
+
return richTextAllowedAlignments.includes(value);
|
|
118
|
+
}
|
|
119
|
+
function isRichTextFontFamily(value) {
|
|
120
|
+
return richTextAllowedFontFamilies.includes(value);
|
|
121
|
+
}
|
|
122
|
+
function isRichTextFontSize(value) {
|
|
123
|
+
return richTextAllowedFontSizes.includes(value);
|
|
124
|
+
}
|
|
125
|
+
function isRichTextLineHeight(value) {
|
|
126
|
+
return richTextAllowedLineHeights.includes(value);
|
|
127
|
+
}
|
|
128
|
+
function isRichTextColor(value) {
|
|
129
|
+
return richTextAllowedColors.includes(value);
|
|
130
|
+
}
|
|
131
|
+
function isRichTextBlockTag(value) {
|
|
132
|
+
return value === "p" || value === "h1" || value === "h2" || value === "h3" || value === "pre" || value === "blockquote";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/commands.ts
|
|
136
|
+
var richTextCommandRegistry = [
|
|
137
|
+
{ command: "undo", group: "history" },
|
|
138
|
+
{ command: "redo", group: "history" },
|
|
139
|
+
{ command: "bold", group: "inline", inlineTagName: "strong" },
|
|
140
|
+
{ command: "italic", group: "inline", inlineTagName: "em" },
|
|
141
|
+
{ command: "underline", group: "inline", inlineTagName: "u" },
|
|
142
|
+
{ command: "strike", group: "inline", inlineTagName: "s" },
|
|
143
|
+
{ command: "inlineCode", group: "inline", inlineTagName: "code" },
|
|
144
|
+
{ command: "highlight", group: "inline", inlineTagName: "mark" },
|
|
145
|
+
{ command: "subscript", group: "inline", inlineTagName: "sub" },
|
|
146
|
+
{ command: "superscript", group: "inline", inlineTagName: "sup" },
|
|
147
|
+
{ command: "link", group: "inline" },
|
|
148
|
+
{ command: "bulletedList", group: "structure", listTagName: "ul" },
|
|
149
|
+
{ command: "numberedList", group: "structure", listTagName: "ol" },
|
|
150
|
+
{ command: "taskList", group: "structure", taskList: true },
|
|
151
|
+
{ command: "blockquote", group: "structure", blockTagName: "blockquote" },
|
|
152
|
+
{ command: "horizontalRule", group: "structure" },
|
|
153
|
+
{ command: "clearFormatting", group: "structure" },
|
|
154
|
+
{ command: "alignLeft", group: "alignment", alignment: "left" },
|
|
155
|
+
{ command: "alignCenter", group: "alignment", alignment: "center" },
|
|
156
|
+
{ command: "alignRight", group: "alignment", alignment: "right" },
|
|
157
|
+
{ command: "alignJustify", group: "alignment", alignment: "justify" },
|
|
158
|
+
{ command: "findReplace", group: "tools" }
|
|
159
|
+
];
|
|
160
|
+
var richTextAlignmentCommandByValue = {
|
|
161
|
+
left: "alignLeft",
|
|
162
|
+
center: "alignCenter",
|
|
163
|
+
right: "alignRight",
|
|
164
|
+
justify: "alignJustify"
|
|
165
|
+
};
|
|
166
|
+
function getRichTextCommandDefinition(command) {
|
|
167
|
+
return richTextCommandRegistry.find((definition) => definition.command === command);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/entities.ts
|
|
171
|
+
var entities = {
|
|
172
|
+
amp: "&",
|
|
173
|
+
lt: "<",
|
|
174
|
+
gt: ">",
|
|
175
|
+
quot: '"',
|
|
176
|
+
apos: "'",
|
|
177
|
+
nbsp: "\xA0",
|
|
178
|
+
copy: "\xA9",
|
|
179
|
+
reg: "\xAE",
|
|
180
|
+
trade: "\u2122",
|
|
181
|
+
mdash: "\u2014",
|
|
182
|
+
ndash: "\u2013",
|
|
183
|
+
hellip: "\u2026",
|
|
184
|
+
lsquo: "\u2018",
|
|
185
|
+
rsquo: "\u2019",
|
|
186
|
+
ldquo: "\u201C",
|
|
187
|
+
rdquo: "\u201D",
|
|
188
|
+
bull: "\u2022",
|
|
189
|
+
middot: "\xB7"
|
|
190
|
+
};
|
|
191
|
+
function decodeRichTextEntities(value) {
|
|
192
|
+
return value.replace(
|
|
193
|
+
/&(#x[\da-f]+|#\d+|[a-z][a-z\d]+);/gi,
|
|
194
|
+
(original, entity) => {
|
|
195
|
+
if (!entity.startsWith("#")) {
|
|
196
|
+
const decoded = entities[entity];
|
|
197
|
+
if (decoded === void 0)
|
|
198
|
+
throw new Error(
|
|
199
|
+
`Unsupported named rich-text entity ${original}; use Unicode or a numeric entity`
|
|
200
|
+
);
|
|
201
|
+
return decoded;
|
|
202
|
+
}
|
|
203
|
+
const point = entity[1]?.toLowerCase() === "x" ? Number.parseInt(entity.slice(2), 16) : Number(entity.slice(1));
|
|
204
|
+
if (point < 1 || point > 1114111 || point >= 55296 && point <= 57343)
|
|
205
|
+
throw new Error("Invalid rich-text character entity");
|
|
206
|
+
return String.fromCodePoint(point);
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/serializer.ts
|
|
212
|
+
var ACTIVE_CONTENT_TAG_PATTERN = /<\s*(script|style|iframe|object|embed|form)\b[^>]*>[\s\S]*?<\s*\/\s*\1\s*>/gi;
|
|
213
|
+
var ACTIVE_VOID_TAG_PATTERN = /<\s*(?:script|style|iframe|object|embed|link|meta|base)\b[^>]*\/?\s*>/gi;
|
|
214
|
+
var EVENT_HANDLER_ATTRIBUTE_PATTERN = /\son[a-z]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]*)/gi;
|
|
215
|
+
var UNSAFE_URL_ATTRIBUTE_PATTERN = /\s(?:href|src|xlink:href)\s*=\s*(["']?)\s*(?:javascript|vbscript|data\s*:\s*text\/html)\s*:[\s\S]*?\1/gi;
|
|
216
|
+
var UNSAFE_STYLE_URL_PATTERN = /url\s*\(\s*(["']?)\s*(?:javascript|vbscript|data\s*:\s*text\/html)\s*:/gi;
|
|
217
|
+
var RICH_TEXT_VIDEO_PATTERN = /<qb-video\b((?:"[^"]*"|'[^']*'|[^'">])*)>[\s\S]*?<\/qb-video>/gi;
|
|
218
|
+
var RICH_TEXT_TAG_PATTERN = /<qb-video\b((?:"[^"]*"|'[^']*'|[^'">])*)>[\s\S]*?<\/qb-video>|<\/?([a-z][a-z0-9-]*)\b((?:"[^"]*"|'[^']*'|[^'">])*)>/gi;
|
|
219
|
+
var ATTRIBUTE_VALUE_PATTERN = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/g;
|
|
220
|
+
var allowedTagSet = new Set(richTextAllowedTags);
|
|
221
|
+
var voidTagSet = new Set(richTextVoidTags);
|
|
222
|
+
var allowedTargetSet = new Set(richTextAllowedTargets);
|
|
223
|
+
var alignmentTagSet = new Set(richTextAlignmentTags);
|
|
224
|
+
var richTextMaximumUrlLength = 4096;
|
|
225
|
+
var richTextMaximumImageTextLength = 512;
|
|
226
|
+
function validateRichTextVideo(src, title) {
|
|
227
|
+
src = src.trim();
|
|
228
|
+
title = title.trim();
|
|
229
|
+
if (!src || src.length > 2048 || !title || title.length > 200 || /[\u0000-\u0020\u007f\\]/u.test(src)) return;
|
|
230
|
+
const relative = /^\/(?!\/)/u.test(src);
|
|
231
|
+
if (!relative && !src.startsWith("https://")) return;
|
|
232
|
+
try {
|
|
233
|
+
const url = new URL(src, "https://rich-text.invalid");
|
|
234
|
+
if (url.protocol !== "https:" || url.username || url.password) return;
|
|
235
|
+
return { src, title };
|
|
236
|
+
} catch {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function readVideoAttributes(raw) {
|
|
241
|
+
const attributes = readHtmlAttributes(raw);
|
|
242
|
+
return validateRichTextVideo(attributes.get("data-src") ?? "", attributes.get("data-title") ?? "");
|
|
243
|
+
}
|
|
244
|
+
function splitRichTextVideoBlocks(html) {
|
|
245
|
+
const content = normalizeRichTextHtml(html);
|
|
246
|
+
const parts = [];
|
|
247
|
+
let offset = 0;
|
|
248
|
+
for (const match of content.matchAll(RICH_TEXT_VIDEO_PATTERN)) {
|
|
249
|
+
const video = readVideoAttributes(match[1] ?? "");
|
|
250
|
+
if (!video) continue;
|
|
251
|
+
if (match.index > offset) parts.push(content.slice(offset, match.index));
|
|
252
|
+
parts.push(video);
|
|
253
|
+
offset = match.index + match[0].length;
|
|
254
|
+
}
|
|
255
|
+
if (offset < content.length) parts.push(content.slice(offset));
|
|
256
|
+
return parts;
|
|
257
|
+
}
|
|
258
|
+
function stripActiveMarkup(value) {
|
|
259
|
+
return value.replace(ACTIVE_CONTENT_TAG_PATTERN, "").replace(ACTIVE_VOID_TAG_PATTERN, "").replace(EVENT_HANDLER_ATTRIBUTE_PATTERN, "").replace(UNSAFE_URL_ATTRIBUTE_PATTERN, "").replace(UNSAFE_STYLE_URL_PATTERN, "url(");
|
|
260
|
+
}
|
|
261
|
+
function sanitizeRichTextMarkup(value, options = {}) {
|
|
262
|
+
if (options.resolveImageSource !== void 0 && typeof options.resolveImageSource !== "function")
|
|
263
|
+
throw new TypeError("Invalid rich text image resolver.");
|
|
264
|
+
if (options.allowVideos !== void 0 && typeof options.allowVideos !== "boolean")
|
|
265
|
+
throw new TypeError("Invalid rich text video policy.");
|
|
266
|
+
if (options.videoPresentation !== void 0 && !["embed", "link"].includes(options.videoPresentation))
|
|
267
|
+
throw new TypeError("Invalid rich text video presentation.");
|
|
268
|
+
if (options.allowContentIdImages !== void 0 && typeof options.allowContentIdImages !== "boolean")
|
|
269
|
+
throw new TypeError("Invalid rich text Content-ID image policy.");
|
|
270
|
+
if (options.imageCrossOrigin !== void 0 && options.imageCrossOrigin !== "anonymous" && typeof options.imageCrossOrigin !== "function")
|
|
271
|
+
throw new TypeError("Invalid rich text image credentials policy.");
|
|
272
|
+
return sanitizeMarkup(value, options);
|
|
273
|
+
}
|
|
274
|
+
function rewriteRichTextImageSources(value, rewrite) {
|
|
275
|
+
if (value.length > 1e6) throw new RangeError("Rich text exceeds the serialization limit");
|
|
276
|
+
return sanitizeMarkup(value, { resolveImageSource: (source) => isSafeRichTextImageSrc(source) ? rewrite(source) : null });
|
|
277
|
+
}
|
|
278
|
+
function sanitizeMarkup(value, options = {}) {
|
|
279
|
+
return value.replace(ACTIVE_CONTENT_TAG_PATTERN, "").replace(ACTIVE_VOID_TAG_PATTERN, "").replace(RICH_TEXT_TAG_PATTERN, (match, rawVideo, rawTagName, rawAttributes) => {
|
|
280
|
+
if (rawVideo !== void 0) {
|
|
281
|
+
if (options.allowVideos === false) return "";
|
|
282
|
+
const video = readVideoAttributes(rawVideo);
|
|
283
|
+
if (!video) return "";
|
|
284
|
+
if (options.videoPresentation === "link") {
|
|
285
|
+
return withBlockIdentity("<p>", rawVideo) + `<br><a href="${escapeHtmlAttribute(video.src)}">${escapeHtmlAttribute(video.title)}</a></p>`;
|
|
286
|
+
}
|
|
287
|
+
return withBlockIdentity(`<qb-video data-src="${escapeHtmlAttribute(video.src)}" data-title="${escapeHtmlAttribute(video.title)}" contenteditable="false">`, rawVideo) + "</qb-video>";
|
|
288
|
+
}
|
|
289
|
+
const tagName = rawTagName.toLowerCase();
|
|
290
|
+
if (tagName === "qb-video") return "";
|
|
291
|
+
if (!allowedTagSet.has(tagName)) {
|
|
292
|
+
return "";
|
|
293
|
+
}
|
|
294
|
+
if (match.startsWith("</")) {
|
|
295
|
+
return voidTagSet.has(tagName) ? "" : `</${tagName}>`;
|
|
296
|
+
}
|
|
297
|
+
if (voidTagSet.has(tagName)) {
|
|
298
|
+
if (tagName === "img") {
|
|
299
|
+
return withBlockIdentity(renderSafeImageTag(rawAttributes, options), rawAttributes);
|
|
300
|
+
}
|
|
301
|
+
if (tagName === "input") {
|
|
302
|
+
return withBlockIdentity(renderSafeTaskCheckbox(rawAttributes), rawAttributes);
|
|
303
|
+
}
|
|
304
|
+
return withBlockIdentity(`<${tagName}>`, rawAttributes);
|
|
305
|
+
}
|
|
306
|
+
if (tagName !== "a") {
|
|
307
|
+
return withBlockIdentity(renderSafeRichTextTag(tagName, rawAttributes), rawAttributes);
|
|
308
|
+
}
|
|
309
|
+
return renderSafeAnchorTag(rawAttributes);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
function plainTextToRichTextHtml(value) {
|
|
313
|
+
return `<p>${value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/\r\n?/g, "\n").replace(/\n/g, "<br>")}</p>`;
|
|
314
|
+
}
|
|
315
|
+
function normalizeRichTextHtml(value) {
|
|
316
|
+
const sanitized = sanitizeRichTextMarkup(value);
|
|
317
|
+
return sanitized.trim() ? sanitized : "<p></p>";
|
|
318
|
+
}
|
|
319
|
+
function richTextHtmlToPlainText(html) {
|
|
320
|
+
if (html.length > 1e6) throw new RangeError("Rich text exceeds the serialization limit");
|
|
321
|
+
const text = sanitizeRichTextMarkup(html).replace(RICH_TEXT_VIDEO_PATTERN, (_element, raw) => `
|
|
322
|
+
${escapeHtmlAttribute(readVideoAttributes(raw)?.title ?? "")}
|
|
323
|
+
`).replace(/<!--[\s\S]*?-->/g, "").replace(/<(?:br|hr)\b[^>]*>/gi, "\n").replace(/<\/(?:p|h[1-6]|li|blockquote|pre|tr|table)>/gi, "\n").replace(/<\/(?:td|th)>/gi, " ").replace(/<[^>]*>/g, "");
|
|
324
|
+
return decodeRichTextEntities(text).replace(/\t+\n/g, "\n").trim();
|
|
325
|
+
}
|
|
326
|
+
function withBlockIdentity(tag, rawAttributes) {
|
|
327
|
+
const id = readHtmlAttributes(rawAttributes).get("data-qb-block-id");
|
|
328
|
+
if (!tag || tag.startsWith("<span") || !id || !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(id)) return tag;
|
|
329
|
+
return tag.replace(/>$/, ` data-qb-block-id="${id}">`);
|
|
330
|
+
}
|
|
331
|
+
function renderSafeRichTextTag(tagName, rawAttributes) {
|
|
332
|
+
if (tagName === "span") {
|
|
333
|
+
return renderSafeSpanTag(rawAttributes);
|
|
334
|
+
}
|
|
335
|
+
if (tagName === "td" || tagName === "th") {
|
|
336
|
+
return renderSafeTableCellTag(tagName, rawAttributes);
|
|
337
|
+
}
|
|
338
|
+
if (tagName === "ul") {
|
|
339
|
+
const attributes2 = readHtmlAttributes(rawAttributes);
|
|
340
|
+
return attributes2.get("data-task-list") === "true" ? '<ul data-task-list="true">' : "<ul>";
|
|
341
|
+
}
|
|
342
|
+
if (tagName === "li") {
|
|
343
|
+
const attributes2 = readHtmlAttributes(rawAttributes);
|
|
344
|
+
const checked = attributes2.get("data-checked");
|
|
345
|
+
return checked === "true" || checked === "false" ? `<li data-checked="${checked}">` : "<li>";
|
|
346
|
+
}
|
|
347
|
+
if (!alignmentTagSet.has(tagName)) {
|
|
348
|
+
return `<${tagName}>`;
|
|
349
|
+
}
|
|
350
|
+
const attributes = readHtmlAttributes(rawAttributes);
|
|
351
|
+
const renderedAttributes = [];
|
|
352
|
+
const alignment = attributes.get("data-align");
|
|
353
|
+
const lineHeight = attributes.get("data-line-height");
|
|
354
|
+
if (isRichTextAlignment(alignment)) renderedAttributes.push(`data-align="${alignment}"`);
|
|
355
|
+
if (isRichTextLineHeight(lineHeight)) renderedAttributes.push(`data-line-height="${lineHeight}"`);
|
|
356
|
+
return renderedAttributes.length > 0 ? `<${tagName} ${renderedAttributes.join(" ")}>` : `<${tagName}>`;
|
|
357
|
+
}
|
|
358
|
+
function renderSafeSpanTag(rawAttributes) {
|
|
359
|
+
const attributes = readHtmlAttributes(rawAttributes);
|
|
360
|
+
const renderedAttributes = [];
|
|
361
|
+
const id = normalizeMentionAttribute(attributes.get("data-mention-id"), 128);
|
|
362
|
+
const label = normalizeMentionAttribute(attributes.get("data-mention-label"), 160);
|
|
363
|
+
const fontFamily = attributes.get("data-font-family");
|
|
364
|
+
const fontSize = attributes.get("data-font-size");
|
|
365
|
+
const textColor = attributes.get("data-text-color");
|
|
366
|
+
const highlightColor = attributes.get("data-highlight-color");
|
|
367
|
+
if (id && label) {
|
|
368
|
+
renderedAttributes.push(
|
|
369
|
+
`data-mention-id="${escapeHtmlAttribute(id)}"`,
|
|
370
|
+
`data-mention-label="${escapeHtmlAttribute(label)}"`
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
if (isRichTextFontFamily(fontFamily) && fontFamily !== "default") renderedAttributes.push(`data-font-family="${fontFamily}"`);
|
|
374
|
+
if (isRichTextFontSize(fontSize) && fontSize !== "default") renderedAttributes.push(`data-font-size="${fontSize}"`);
|
|
375
|
+
if (isRichTextColor(textColor) && textColor !== "default") renderedAttributes.push(`data-text-color="${textColor}"`);
|
|
376
|
+
if (isRichTextColor(highlightColor) && highlightColor !== "default") renderedAttributes.push(`data-highlight-color="${highlightColor}"`);
|
|
377
|
+
return renderedAttributes.length > 0 ? `<span ${renderedAttributes.join(" ")}>` : "<span>";
|
|
378
|
+
}
|
|
379
|
+
function renderSafeTableCellTag(tagName, rawAttributes) {
|
|
380
|
+
const attributes = readHtmlAttributes(rawAttributes);
|
|
381
|
+
const renderedAttributes = [];
|
|
382
|
+
const alignment = attributes.get("data-align");
|
|
383
|
+
const colSpan = normalizeTableSpan(attributes.get("colspan"));
|
|
384
|
+
const rowSpan = normalizeTableSpan(attributes.get("rowspan"));
|
|
385
|
+
const scope = attributes.get("scope");
|
|
386
|
+
if (isRichTextAlignment(alignment)) {
|
|
387
|
+
renderedAttributes.push(`data-align="${alignment}"`);
|
|
388
|
+
}
|
|
389
|
+
if (colSpan) {
|
|
390
|
+
renderedAttributes.push(`colspan="${colSpan}"`);
|
|
391
|
+
}
|
|
392
|
+
if (rowSpan) {
|
|
393
|
+
renderedAttributes.push(`rowspan="${rowSpan}"`);
|
|
394
|
+
}
|
|
395
|
+
if (tagName === "th" && scope && ["col", "row", "colgroup", "rowgroup"].includes(scope)) {
|
|
396
|
+
renderedAttributes.push(`scope="${scope}"`);
|
|
397
|
+
}
|
|
398
|
+
return renderedAttributes.length > 0 ? `<${tagName} ${renderedAttributes.join(" ")}>` : `<${tagName}>`;
|
|
399
|
+
}
|
|
400
|
+
function renderSafeAnchorTag(rawAttributes) {
|
|
401
|
+
const attributes = readHtmlAttributes(rawAttributes);
|
|
402
|
+
const renderedAttributes = [];
|
|
403
|
+
const href = attributes.get("href");
|
|
404
|
+
const target = attributes.get("target");
|
|
405
|
+
if (href && isSafeRichTextHref(href)) {
|
|
406
|
+
renderedAttributes.push(`href="${escapeHtmlAttribute(href)}"`);
|
|
407
|
+
}
|
|
408
|
+
if (target && allowedTargetSet.has(target)) {
|
|
409
|
+
renderedAttributes.push(`target="${target}"`);
|
|
410
|
+
if (target === "_blank") {
|
|
411
|
+
renderedAttributes.push('rel="noopener noreferrer"');
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return renderedAttributes.length > 0 ? `<a ${renderedAttributes.join(" ")}>` : "<a>";
|
|
415
|
+
}
|
|
416
|
+
function renderSafeImageTag(rawAttributes, options) {
|
|
417
|
+
const attributes = readHtmlAttributes(rawAttributes);
|
|
418
|
+
const source = attributes.get("src");
|
|
419
|
+
const resolveSource = options.resolveImageSource;
|
|
420
|
+
if (!source || !resolveSource && !isAllowedImageSource(source, options)) return "";
|
|
421
|
+
const src = resolveSource ? resolveSource(source) : source;
|
|
422
|
+
if (src === null) return "";
|
|
423
|
+
if (!isAllowedImageSource(src, options)) throw new TypeError("Invalid replacement image URL.");
|
|
424
|
+
const renderedAttributes = [
|
|
425
|
+
`src="${escapeHtmlAttribute(src)}"`,
|
|
426
|
+
`alt="${escapeHtmlAttribute(normalizeTextAttribute(attributes.get("alt"), 512) ?? "")}"`
|
|
427
|
+
];
|
|
428
|
+
const title = normalizeTextAttribute(attributes.get("title"), 512);
|
|
429
|
+
const width = normalizeImageDimension(attributes.get("width"));
|
|
430
|
+
const height = normalizeImageDimension(attributes.get("height"));
|
|
431
|
+
if (title) renderedAttributes.push(`title="${escapeHtmlAttribute(title)}"`);
|
|
432
|
+
if (width) renderedAttributes.push(`width="${width}"`);
|
|
433
|
+
if (height) renderedAttributes.push(`height="${height}"`);
|
|
434
|
+
const credentials = typeof options.imageCrossOrigin === "function" ? options.imageCrossOrigin(src) : options.imageCrossOrigin;
|
|
435
|
+
if (credentials !== void 0 && credentials !== "anonymous") throw new TypeError("Invalid resolved image credentials policy.");
|
|
436
|
+
if ((credentials ?? attributes.get("crossorigin")) === "anonymous") renderedAttributes.push('crossorigin="anonymous"');
|
|
437
|
+
renderedAttributes.push('loading="lazy"', 'decoding="async"');
|
|
438
|
+
return `<img ${renderedAttributes.join(" ")}>`;
|
|
439
|
+
}
|
|
440
|
+
function isAllowedImageSource(value, options) {
|
|
441
|
+
if (isSafeRichTextImageSrc(value)) return true;
|
|
442
|
+
if (!options.allowContentIdImages || value.length > richTextMaximumUrlLength || !/^cid:/iu.test(value)) return false;
|
|
443
|
+
try {
|
|
444
|
+
const identity = decodeURIComponent(value.slice(4));
|
|
445
|
+
return identity.length > 0 && identity.length <= 200 && !/[^\u0021-\u007e]|[<>"]/u.test(identity);
|
|
446
|
+
} catch {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
function renderSafeTaskCheckbox(rawAttributes) {
|
|
451
|
+
const attributes = readHtmlAttributes(rawAttributes);
|
|
452
|
+
if (attributes.get("type")?.toLowerCase() !== "checkbox") return "";
|
|
453
|
+
const checked = attributes.get("checked") !== void 0 || /(?:^|\s)checked(?:\s|=|$)/i.test(rawAttributes);
|
|
454
|
+
return `<input type="checkbox"${checked ? ' checked=""' : ""} contenteditable="false">`;
|
|
455
|
+
}
|
|
456
|
+
function readHtmlAttributes(rawAttributes) {
|
|
457
|
+
const attributes = /* @__PURE__ */ new Map();
|
|
458
|
+
for (const match of rawAttributes.matchAll(ATTRIBUTE_VALUE_PATTERN)) {
|
|
459
|
+
const name = match[1]?.toLowerCase();
|
|
460
|
+
const value = match[2] ?? match[3] ?? match[4] ?? "";
|
|
461
|
+
if (name) {
|
|
462
|
+
attributes.set(name, decodeHtmlAttribute(value.trim()));
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return attributes;
|
|
466
|
+
}
|
|
467
|
+
function isSafeRichTextHref(value) {
|
|
468
|
+
const trimmed = value.trim();
|
|
469
|
+
if (!trimmed || trimmed.length > richTextMaximumUrlLength || /[\u0000-\u001f\u007f]/.test(trimmed)) {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
if (trimmed.startsWith("//")) {
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
if (/^(?:#|\/|\.\/|\.\.\/|\?)/.test(trimmed)) {
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
try {
|
|
479
|
+
const url = new URL(trimmed);
|
|
480
|
+
return ["http:", "https:", "mailto:", "tel:"].includes(url.protocol.toLowerCase());
|
|
481
|
+
} catch {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
function isSafeRichTextImageSrc(value) {
|
|
486
|
+
const trimmed = value.trim();
|
|
487
|
+
if (!trimmed || trimmed.length > richTextMaximumUrlLength || /[\u0000-\u001f\u007f]/.test(trimmed) || trimmed.startsWith("//")) return false;
|
|
488
|
+
if (/^(?:\/|\.\/|\.\.\/)/.test(trimmed)) return true;
|
|
489
|
+
try {
|
|
490
|
+
const url = new URL(trimmed);
|
|
491
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
492
|
+
} catch {
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
function normalizeTableSpan(value) {
|
|
497
|
+
if (!value || !/^\d{1,3}$/.test(value)) {
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
const numericValue = Number(value);
|
|
501
|
+
return numericValue >= 1 && numericValue <= 100 ? String(numericValue) : null;
|
|
502
|
+
}
|
|
503
|
+
function normalizeMentionAttribute(value, maxLength) {
|
|
504
|
+
const normalized = value?.trim();
|
|
505
|
+
if (!normalized || normalized.length > maxLength || /[<>"'`]/.test(normalized)) {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
return normalized;
|
|
509
|
+
}
|
|
510
|
+
function normalizeTextAttribute(value, maxLength) {
|
|
511
|
+
const normalized = value?.trim();
|
|
512
|
+
if (!normalized || normalized.length > maxLength) return null;
|
|
513
|
+
return normalized;
|
|
514
|
+
}
|
|
515
|
+
function normalizeImageDimension(value) {
|
|
516
|
+
if (!value || !/^\d{1,5}$/.test(value)) return null;
|
|
517
|
+
const numericValue = Number(value);
|
|
518
|
+
return numericValue >= 1 && numericValue <= 1e4 ? String(numericValue) : null;
|
|
519
|
+
}
|
|
520
|
+
function escapeHtmlAttribute(value) {
|
|
521
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
522
|
+
}
|
|
523
|
+
function decodeHtmlAttribute(value) {
|
|
524
|
+
return value.replace(/&(?:#(\d+)|#x([0-9a-f]+)|(amp|quot|apos|lt|gt));/gi, (entity, decimal, hexadecimal, named) => {
|
|
525
|
+
if (decimal || hexadecimal) {
|
|
526
|
+
const codePoint = decimal ? Number(decimal) : Number.parseInt(hexadecimal, 16);
|
|
527
|
+
if (!Number.isSafeInteger(codePoint) || codePoint <= 0 || codePoint > 1114111 || codePoint >= 55296 && codePoint <= 57343) {
|
|
528
|
+
return "\uFFFD";
|
|
529
|
+
}
|
|
530
|
+
return String.fromCodePoint(codePoint);
|
|
531
|
+
}
|
|
532
|
+
const namedEntity = {
|
|
533
|
+
amp: "&",
|
|
534
|
+
apos: "'",
|
|
535
|
+
gt: ">",
|
|
536
|
+
lt: "<",
|
|
537
|
+
quot: '"'
|
|
538
|
+
};
|
|
539
|
+
return namedEntity[String(named).toLowerCase()] ?? entity;
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// src/history.ts
|
|
544
|
+
var defaultRichTextHistoryLimit = 80;
|
|
545
|
+
function assertHistoryLimit(maxEntries) {
|
|
546
|
+
if (!Number.isSafeInteger(maxEntries) || maxEntries < 1) {
|
|
547
|
+
throw new RangeError("Rich text history limit must be a positive integer.");
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function createRichTextHistory(initialHtml, maxEntries = defaultRichTextHistoryLimit) {
|
|
551
|
+
assertHistoryLimit(maxEntries);
|
|
552
|
+
return {
|
|
553
|
+
entries: [normalizeRichTextHtml(initialHtml)],
|
|
554
|
+
index: 0,
|
|
555
|
+
maxEntries
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
function pushRichTextHistory(history, nextHtml, maxEntries = history.maxEntries ?? defaultRichTextHistoryLimit) {
|
|
559
|
+
assertHistoryLimit(maxEntries);
|
|
560
|
+
const safeHtml = normalizeRichTextHtml(nextHtml);
|
|
561
|
+
if (history.entries[history.index] === safeHtml) {
|
|
562
|
+
return history;
|
|
563
|
+
}
|
|
564
|
+
const startIndex = Math.max(0, history.index + 2 - maxEntries);
|
|
565
|
+
const entries = history.entries.slice(startIndex, history.index + 1);
|
|
566
|
+
entries.push(safeHtml);
|
|
567
|
+
return {
|
|
568
|
+
entries,
|
|
569
|
+
index: entries.length - 1,
|
|
570
|
+
maxEntries
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
function moveRichTextHistory(history, direction) {
|
|
574
|
+
const nextIndex = history.index + direction;
|
|
575
|
+
if (nextIndex < 0 || nextIndex >= history.entries.length) {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
const nextHistory = {
|
|
579
|
+
...history,
|
|
580
|
+
index: nextIndex
|
|
581
|
+
};
|
|
582
|
+
return {
|
|
583
|
+
history: nextHistory,
|
|
584
|
+
html: history.entries[nextIndex] ?? "<p></p>"
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// src/adapters.ts
|
|
589
|
+
var QUICK_BUILDER_RICH_TEXT_FRAMEWORKS = [
|
|
590
|
+
"react",
|
|
591
|
+
"next",
|
|
592
|
+
"vue",
|
|
593
|
+
"nuxt",
|
|
594
|
+
"angular",
|
|
595
|
+
"astro",
|
|
596
|
+
"svelte",
|
|
597
|
+
"solid",
|
|
598
|
+
"qwik",
|
|
599
|
+
"preact",
|
|
600
|
+
"react-native",
|
|
601
|
+
"webcomponents"
|
|
602
|
+
];
|
|
603
|
+
function createRichTextBinding(target, options = {}) {
|
|
604
|
+
const attributes = createRichTextAttributes(options);
|
|
605
|
+
const binding = {
|
|
606
|
+
target,
|
|
607
|
+
attributes
|
|
608
|
+
};
|
|
609
|
+
if (target === "react" || target === "next" || target === "preact") {
|
|
610
|
+
return {
|
|
611
|
+
...binding,
|
|
612
|
+
props: {
|
|
613
|
+
...attributes,
|
|
614
|
+
className: attributes.class,
|
|
615
|
+
contentEditable: attributes["aria-disabled"] !== "true",
|
|
616
|
+
suppressContentEditableWarning: true,
|
|
617
|
+
role: "textbox"
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
if (target === "vue" || target === "nuxt" || target === "svelte" || target === "solid" || target === "qwik" || target === "astro") {
|
|
622
|
+
return {
|
|
623
|
+
...binding,
|
|
624
|
+
binding: {
|
|
625
|
+
attrs: attributes,
|
|
626
|
+
class: attributes.class
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
if (target === "angular") {
|
|
631
|
+
return {
|
|
632
|
+
...binding,
|
|
633
|
+
hostBinding: {
|
|
634
|
+
attributes,
|
|
635
|
+
className: attributes.class
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
return binding;
|
|
640
|
+
}
|
|
641
|
+
function createReactRichTextBinding(options = {}) {
|
|
642
|
+
return createRichTextBinding("react", options);
|
|
643
|
+
}
|
|
644
|
+
function createVueRichTextBinding(options = {}) {
|
|
645
|
+
return createRichTextBinding("vue", options);
|
|
646
|
+
}
|
|
647
|
+
function createSvelteRichTextBinding(options = {}) {
|
|
648
|
+
return createRichTextBinding("svelte", options);
|
|
649
|
+
}
|
|
650
|
+
function createWebcomponentsRichTextBinding(options = {}) {
|
|
651
|
+
return createRichTextBinding("webcomponents", options);
|
|
652
|
+
}
|
|
653
|
+
function createRichTextAttributes(options) {
|
|
654
|
+
const safeHtml = normalizeRichTextHtml(options.value ?? "<p></p>");
|
|
655
|
+
const attributes = {
|
|
656
|
+
"data-qb-rich-text": "",
|
|
657
|
+
"data-qb-rich-text-schema-version": String(options.schemaVersion ?? QUICK_BUILDER_RICH_TEXT_SCHEMA_VERSION),
|
|
658
|
+
"data-qb-rich-text-html": safeHtml,
|
|
659
|
+
"data-qb-rich-text-plain-text": options.plainText ?? richTextHtmlToPlainText(safeHtml),
|
|
660
|
+
role: "textbox",
|
|
661
|
+
"aria-multiline": "true"
|
|
662
|
+
};
|
|
663
|
+
if (options.id) {
|
|
664
|
+
attributes.id = options.id;
|
|
665
|
+
}
|
|
666
|
+
if (options.className) {
|
|
667
|
+
attributes.class = options.className;
|
|
668
|
+
}
|
|
669
|
+
if (options.placeholder) {
|
|
670
|
+
attributes["aria-placeholder"] = options.placeholder;
|
|
671
|
+
}
|
|
672
|
+
if (options.disabled) {
|
|
673
|
+
attributes["aria-disabled"] = "true";
|
|
674
|
+
}
|
|
675
|
+
if (options.editable === false) {
|
|
676
|
+
attributes.contenteditable = "false";
|
|
677
|
+
}
|
|
678
|
+
return attributes;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// src/transactions.ts
|
|
682
|
+
var defaultEditorState = {
|
|
683
|
+
activeCommands: /* @__PURE__ */ new Set(),
|
|
684
|
+
alignment: "left",
|
|
685
|
+
blockFormat: "paragraph"
|
|
686
|
+
};
|
|
687
|
+
function createRichTextDocumentSnapshot(html, schemaVersion = QUICK_BUILDER_RICH_TEXT_SCHEMA_VERSION) {
|
|
688
|
+
const safeHtml = normalizeRichTextHtml(html);
|
|
689
|
+
return {
|
|
690
|
+
html: safeHtml,
|
|
691
|
+
plainText: richTextHtmlToPlainText(safeHtml),
|
|
692
|
+
schemaVersion
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
function createRichTextRuntimeState(initialHtml = "<p></p>", options = {}) {
|
|
696
|
+
const document = createRichTextDocumentSnapshot(initialHtml, options.schemaVersion);
|
|
697
|
+
const editorState = options.editorState ?? defaultEditorState;
|
|
698
|
+
return {
|
|
699
|
+
activeCommands: sortRichTextCommands(editorState.activeCommands),
|
|
700
|
+
alignment: editorState.alignment,
|
|
701
|
+
blockFormat: editorState.blockFormat,
|
|
702
|
+
document,
|
|
703
|
+
history: createRichTextHistory(document.html, options.maxHistoryEntries),
|
|
704
|
+
lastTransactionId: "tx-0",
|
|
705
|
+
selection: options.selection ?? null,
|
|
706
|
+
version: 0
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
function applyRichTextTransaction(state, transaction) {
|
|
710
|
+
const source = transaction.source ?? defaultTransactionSource(transaction.kind);
|
|
711
|
+
const transactionId = transaction.transactionId ?? createRichTextTransactionId(state.version + 1, transaction.kind);
|
|
712
|
+
if (transaction.kind === "undo" || transaction.kind === "redo") {
|
|
713
|
+
const moved = moveRichTextHistory(state.history, transaction.kind === "undo" ? -1 : 1);
|
|
714
|
+
if (!moved) {
|
|
715
|
+
return createTransactionResult(state, transaction, source, transactionId, false, false);
|
|
716
|
+
}
|
|
717
|
+
const nextState2 = createNextRuntimeState(state, {
|
|
718
|
+
editorState: transaction.editorState,
|
|
719
|
+
history: moved.history,
|
|
720
|
+
html: moved.html,
|
|
721
|
+
selection: transaction.selection,
|
|
722
|
+
transactionId
|
|
723
|
+
});
|
|
724
|
+
return createTransactionResult(nextState2, transaction, source, transactionId, true, true);
|
|
725
|
+
}
|
|
726
|
+
if (transaction.kind === "sync-selection") {
|
|
727
|
+
const nextState2 = createNextRuntimeState(state, {
|
|
728
|
+
editorState: transaction.editorState,
|
|
729
|
+
history: state.history,
|
|
730
|
+
html: state.document.html,
|
|
731
|
+
selection: transaction.selection,
|
|
732
|
+
transactionId,
|
|
733
|
+
versionDelta: 0
|
|
734
|
+
});
|
|
735
|
+
return createTransactionResult(nextState2, transaction, source, transactionId, false, false);
|
|
736
|
+
}
|
|
737
|
+
const document = createRichTextDocumentSnapshot(transaction.html ?? state.document.html, state.document.schemaVersion);
|
|
738
|
+
const changed = document.html !== state.document.html || document.plainText !== state.document.plainText;
|
|
739
|
+
const shouldAddToHistory = transaction.addToHistory ?? changed;
|
|
740
|
+
const history = shouldAddToHistory ? pushRichTextHistory(state.history, document.html, transaction.maxHistoryEntries) : state.history;
|
|
741
|
+
const nextState = createNextRuntimeState(state, {
|
|
742
|
+
document,
|
|
743
|
+
editorState: transaction.editorState,
|
|
744
|
+
history,
|
|
745
|
+
selection: transaction.selection,
|
|
746
|
+
transactionId,
|
|
747
|
+
versionDelta: changed ? 1 : 0
|
|
748
|
+
});
|
|
749
|
+
return createTransactionResult(nextState, transaction, source, transactionId, changed, history !== state.history);
|
|
750
|
+
}
|
|
751
|
+
function canUndoRichTextTransaction(state) {
|
|
752
|
+
return state.history.index > 0;
|
|
753
|
+
}
|
|
754
|
+
function canRedoRichTextTransaction(state) {
|
|
755
|
+
return state.history.index < state.history.entries.length - 1;
|
|
756
|
+
}
|
|
757
|
+
function createNextRuntimeState(state, options) {
|
|
758
|
+
const document = options.document ?? createRichTextDocumentSnapshot(options.html ?? state.document.html, state.document.schemaVersion);
|
|
759
|
+
const editorState = options.editorState;
|
|
760
|
+
return {
|
|
761
|
+
activeCommands: editorState ? sortRichTextCommands(editorState.activeCommands) : state.activeCommands,
|
|
762
|
+
alignment: editorState?.alignment ?? state.alignment,
|
|
763
|
+
blockFormat: editorState?.blockFormat ?? state.blockFormat,
|
|
764
|
+
document,
|
|
765
|
+
history: options.history,
|
|
766
|
+
lastTransactionId: options.transactionId,
|
|
767
|
+
selection: options.selection === void 0 ? state.selection : options.selection,
|
|
768
|
+
version: state.version + (options.versionDelta ?? 1)
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
function createTransactionResult(state, transaction, source, transactionId, changed, historyChanged) {
|
|
772
|
+
return {
|
|
773
|
+
changed,
|
|
774
|
+
historyChanged,
|
|
775
|
+
state,
|
|
776
|
+
transaction: {
|
|
777
|
+
command: transaction.command,
|
|
778
|
+
kind: transaction.kind,
|
|
779
|
+
source,
|
|
780
|
+
transactionId
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
function createRichTextTransactionId(version, kind) {
|
|
785
|
+
return `tx-${version}-${kind}`;
|
|
786
|
+
}
|
|
787
|
+
function defaultTransactionSource(kind) {
|
|
788
|
+
if (kind === "undo" || kind === "redo") {
|
|
789
|
+
return "history";
|
|
790
|
+
}
|
|
791
|
+
if (kind === "paste-html") {
|
|
792
|
+
return "paste";
|
|
793
|
+
}
|
|
794
|
+
if (kind === "apply-command") {
|
|
795
|
+
return "command";
|
|
796
|
+
}
|
|
797
|
+
return "api";
|
|
798
|
+
}
|
|
799
|
+
function sortRichTextCommands(commands) {
|
|
800
|
+
return Array.from(commands).sort();
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// src/extensions.ts
|
|
804
|
+
var richTextTableExtension = {
|
|
805
|
+
id: "tables",
|
|
806
|
+
label: "Tables",
|
|
807
|
+
tier: "pro",
|
|
808
|
+
availability: "registered",
|
|
809
|
+
schema: {
|
|
810
|
+
nodes: ["table", "tableRow", "tableHeaderCell", "tableCell"]
|
|
811
|
+
},
|
|
812
|
+
commands: [
|
|
813
|
+
{ command: "insertTable", label: "Insert table", group: "insert" },
|
|
814
|
+
{ command: "deleteTable", label: "Delete table", group: "structure" },
|
|
815
|
+
{ command: "addTableRow", label: "Add table row", group: "structure" },
|
|
816
|
+
{ command: "deleteTableRow", label: "Delete table row", group: "structure" },
|
|
817
|
+
{ command: "addTableColumn", label: "Add table column", group: "structure" },
|
|
818
|
+
{ command: "deleteTableColumn", label: "Delete table column", group: "structure" }
|
|
819
|
+
]
|
|
820
|
+
};
|
|
821
|
+
var richTextMentionExtension = {
|
|
822
|
+
id: "mentions",
|
|
823
|
+
label: "Mentions",
|
|
824
|
+
tier: "pro",
|
|
825
|
+
availability: "registered",
|
|
826
|
+
schema: {
|
|
827
|
+
nodes: ["mention", "mentionQuery", "mentionMenu"]
|
|
828
|
+
},
|
|
829
|
+
commands: [
|
|
830
|
+
{ command: "insertMention", label: "Insert mention", group: "insert" }
|
|
831
|
+
]
|
|
832
|
+
};
|
|
833
|
+
var richTextSlashCommandExtension = {
|
|
834
|
+
id: "slash-commands",
|
|
835
|
+
label: "Slash commands",
|
|
836
|
+
tier: "pro",
|
|
837
|
+
availability: "registered",
|
|
838
|
+
schema: {
|
|
839
|
+
nodes: ["slashCommandQuery", "slashCommandMenu"]
|
|
840
|
+
},
|
|
841
|
+
commands: [
|
|
842
|
+
{ command: "heading1", label: "Heading 1", group: "structure" },
|
|
843
|
+
{ command: "heading2", label: "Heading 2", group: "structure" },
|
|
844
|
+
{ command: "heading3", label: "Heading 3", group: "structure" },
|
|
845
|
+
{ command: "paragraph", label: "Paragraph", group: "structure" },
|
|
846
|
+
{ command: "blockquote", label: "Quote", group: "structure" },
|
|
847
|
+
{ command: "bulletedList", label: "Bulleted list", group: "structure" },
|
|
848
|
+
{ command: "numberedList", label: "Numbered list", group: "structure" },
|
|
849
|
+
{ command: "taskList", label: "Task list", group: "structure" },
|
|
850
|
+
{ command: "codeBlock", label: "Code block", group: "structure" },
|
|
851
|
+
{ command: "horizontalRule", label: "Separator", group: "insert" },
|
|
852
|
+
{ command: "table", label: "Table", group: "insert" }
|
|
853
|
+
]
|
|
854
|
+
};
|
|
855
|
+
var richTextCollaborationExtension = {
|
|
856
|
+
id: "collaboration",
|
|
857
|
+
label: "Collaboration",
|
|
858
|
+
tier: "pro",
|
|
859
|
+
availability: "registered",
|
|
860
|
+
schema: {
|
|
861
|
+
nodes: ["collaborationPresence", "collaborationCursor"]
|
|
862
|
+
},
|
|
863
|
+
commands: [
|
|
864
|
+
{ command: "syncPresence", label: "Sync presence", group: "collaboration" }
|
|
865
|
+
]
|
|
866
|
+
};
|
|
867
|
+
var richTextPagesExtension = {
|
|
868
|
+
id: "pages",
|
|
869
|
+
label: "Paginated documents",
|
|
870
|
+
tier: "pro",
|
|
871
|
+
availability: "registered",
|
|
872
|
+
schema: {
|
|
873
|
+
nodes: ["documentPage", "pageHeader", "pageFooter", "pageNumberField", "manualPageBreak"]
|
|
874
|
+
},
|
|
875
|
+
commands: [
|
|
876
|
+
{ command: "insertPage", label: "Insert page break", group: "insert" },
|
|
877
|
+
{ command: "removePage", label: "Remove page", group: "structure" },
|
|
878
|
+
{ command: "previousPage", label: "Previous page", group: "navigation" },
|
|
879
|
+
{ command: "nextPage", label: "Next page", group: "navigation" },
|
|
880
|
+
{ command: "editHeader", label: "Edit page header", group: "navigation" },
|
|
881
|
+
{ command: "editFooter", label: "Edit page footer", group: "navigation" }
|
|
882
|
+
]
|
|
883
|
+
};
|
|
884
|
+
var richTextAiAssistantExtension = {
|
|
885
|
+
id: "ai-assistant",
|
|
886
|
+
label: "AI assistant",
|
|
887
|
+
tier: "pro",
|
|
888
|
+
availability: "registered",
|
|
889
|
+
schema: {
|
|
890
|
+
nodes: ["aiAssistantRequest", "aiSuggestion"]
|
|
891
|
+
},
|
|
892
|
+
commands: [
|
|
893
|
+
{ command: "improveWriting", label: "Improve writing", group: "assistant" },
|
|
894
|
+
{ command: "fixSpelling", label: "Fix spelling", group: "assistant" },
|
|
895
|
+
{ command: "shorten", label: "Shorten", group: "assistant" },
|
|
896
|
+
{ command: "expand", label: "Expand", group: "assistant" },
|
|
897
|
+
{ command: "summarize", label: "Summarize", group: "assistant" }
|
|
898
|
+
]
|
|
899
|
+
};
|
|
900
|
+
var richTextExtensionDefinitions = [
|
|
901
|
+
richTextTableExtension,
|
|
902
|
+
richTextMentionExtension,
|
|
903
|
+
richTextSlashCommandExtension,
|
|
904
|
+
richTextPagesExtension,
|
|
905
|
+
richTextCollaborationExtension,
|
|
906
|
+
richTextAiAssistantExtension
|
|
907
|
+
];
|
|
908
|
+
function createRichTextExtensionRegistry(extensions = richTextExtensionDefinitions) {
|
|
909
|
+
const seen = /* @__PURE__ */ new Set();
|
|
910
|
+
const registeredExtensions = [];
|
|
911
|
+
for (const extension of extensions) {
|
|
912
|
+
if (seen.has(extension.id)) {
|
|
913
|
+
throw new Error(`Duplicate rich text extension "${extension.id}".`);
|
|
914
|
+
}
|
|
915
|
+
seen.add(extension.id);
|
|
916
|
+
registeredExtensions.push(extension);
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
extensions: registeredExtensions,
|
|
920
|
+
ids: registeredExtensions.map((extension) => extension.id)
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
var richTextExtensionRegistry = createRichTextExtensionRegistry();
|
|
924
|
+
function resolveRichTextExtensions(extensionIds, registry = richTextExtensionRegistry) {
|
|
925
|
+
const extensionById = new Map(registry.extensions.map((extension) => [extension.id, extension]));
|
|
926
|
+
const resolved = [];
|
|
927
|
+
const seen = /* @__PURE__ */ new Set();
|
|
928
|
+
for (const extensionId of extensionIds) {
|
|
929
|
+
if (seen.has(extensionId)) {
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
const extension = extensionById.get(extensionId);
|
|
933
|
+
if (!extension) {
|
|
934
|
+
throw new Error(`Unknown rich text extension "${extensionId}".`);
|
|
935
|
+
}
|
|
936
|
+
seen.add(extensionId);
|
|
937
|
+
resolved.push(extension);
|
|
938
|
+
}
|
|
939
|
+
return resolved;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
// src/markdown.ts
|
|
943
|
+
var DEFAULT_MAX_MARKDOWN_LENGTH = 1e6;
|
|
944
|
+
var MAX_MARKDOWN_LENGTH = 5e6;
|
|
945
|
+
var INLINE_TOKEN_PREFIX = "\0qb-markdown-inline-";
|
|
946
|
+
var HARD_BREAK_TOKEN = "\0qb-markdown-hard-break\0";
|
|
947
|
+
function assertMarkdownOptions(options) {
|
|
948
|
+
const maxLength = options.maxLength ?? DEFAULT_MAX_MARKDOWN_LENGTH;
|
|
949
|
+
const limitBehavior = options.limitBehavior ?? "throw";
|
|
950
|
+
if (!Number.isInteger(maxLength) || maxLength < 1 || maxLength > MAX_MARKDOWN_LENGTH) {
|
|
951
|
+
throw new Error(
|
|
952
|
+
`Unsupported markdown maxLength "${String(maxLength)}". Use an integer from 1 to ${MAX_MARKDOWN_LENGTH}.`
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
if (limitBehavior !== "throw" && limitBehavior !== "truncate") {
|
|
956
|
+
throw new Error(
|
|
957
|
+
`Unsupported markdown limitBehavior "${String(limitBehavior)}". Use "throw" or "truncate".`
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
return { limitBehavior, maxLength };
|
|
961
|
+
}
|
|
962
|
+
function normalizeMarkdown(markdown, options) {
|
|
963
|
+
if (typeof markdown !== "string") {
|
|
964
|
+
throw new Error("Markdown content must be a string.");
|
|
965
|
+
}
|
|
966
|
+
const { limitBehavior, maxLength } = assertMarkdownOptions(options);
|
|
967
|
+
const normalized = markdown.replace(/\u0000/g, "").replace(/\r\n?/g, "\n");
|
|
968
|
+
if (normalized.length <= maxLength) return normalized;
|
|
969
|
+
if (limitBehavior === "truncate") return normalized.slice(0, maxLength);
|
|
970
|
+
throw new Error(
|
|
971
|
+
`Markdown content exceeds the configured ${maxLength} character limit.`
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
function escapeHtml(value) {
|
|
975
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
976
|
+
}
|
|
977
|
+
function renderInlineMarkdown(value) {
|
|
978
|
+
const tokens = [];
|
|
979
|
+
const protect = (html, text = html) => {
|
|
980
|
+
const token = `${INLINE_TOKEN_PREFIX}${tokens.length}\0`;
|
|
981
|
+
tokens.push({ html, text });
|
|
982
|
+
return token;
|
|
983
|
+
};
|
|
984
|
+
const restore = (source2, format) => source2.replace(
|
|
985
|
+
/\u0000qb-markdown-inline-(\d+)\u0000/g,
|
|
986
|
+
(_match, index) => tokens[Number(index)]?.[format] ?? ""
|
|
987
|
+
);
|
|
988
|
+
let source = value.replace(
|
|
989
|
+
/\\([\\`*_[\]{}()#+\-.!|>~])|(`+)([\s\S]*?[^`])\2(?!`)/g,
|
|
990
|
+
(_match, character, _ticks, code) => {
|
|
991
|
+
if (character !== void 0) return protect(escapeHtml(character), character);
|
|
992
|
+
const text = code.trim();
|
|
993
|
+
return protect(`<code>${escapeHtml(text)}</code>`, text);
|
|
994
|
+
}
|
|
995
|
+
).replace(
|
|
996
|
+
/(!?)\[([^\]\n]*)\]\(\s*(<[^>\n]+>|(?:[^\s()]|\([^()]*\))+)(?:\s+["']([^"']*)["'])?\s*\)/g,
|
|
997
|
+
(_match, image, label, destination, title) => {
|
|
998
|
+
const url = restore(destination.replace(/^<|>$/g, ""), "text");
|
|
999
|
+
if (image) {
|
|
1000
|
+
const alt = escapeHtml(restore(label, "text"));
|
|
1001
|
+
const description = title && escapeHtml(restore(title, "text"));
|
|
1002
|
+
return protect(
|
|
1003
|
+
isSafeRichTextImageSrc(url) ? `<img src="${escapeHtml(url)}" alt="${alt}"${description ? ` title="${description}"` : ""}>` : alt
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
const labelHtml = restore(escapeHtml(label), "html");
|
|
1007
|
+
return protect(
|
|
1008
|
+
isSafeRichTextHref(url) ? `<a href="${escapeHtml(url)}" target="_blank">${labelHtml}</a>` : labelHtml
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
).replace(
|
|
1012
|
+
/<(https?:\/\/[^<>\s]+|mailto:[^<>\s]+)>/gi,
|
|
1013
|
+
(_match, url) => protect(
|
|
1014
|
+
isSafeRichTextHref(url) ? `<a href="${escapeHtml(url)}" target="_blank">${escapeHtml(url)}</a>` : escapeHtml(url)
|
|
1015
|
+
)
|
|
1016
|
+
).replace(/https?:\/\/[^\s<>\u0000]+/gi, (candidate) => {
|
|
1017
|
+
const url = candidate.replace(/[.,!?;:)]+$/, "");
|
|
1018
|
+
return isSafeRichTextHref(url) ? protect(
|
|
1019
|
+
`<a href="${escapeHtml(url)}" target="_blank">${escapeHtml(url)}</a>`
|
|
1020
|
+
) + candidate.slice(url.length) : candidate;
|
|
1021
|
+
});
|
|
1022
|
+
source = escapeHtml(source).replace(/\*\*([^*\n]+)\*\*/g, "<strong>$1</strong>").replace(/__([^_\n]+)__/g, "<strong>$1</strong>").replace(/~~([^~\n]+)~~/g, "<s>$1</s>").replace(/(^|[^\w])\*([^*\n]+)\*(?!\*)/g, "$1<em>$2</em>").replace(/(^|[^\w])_([^_\n]+)_(?!_)/g, "$1<em>$2</em>");
|
|
1023
|
+
return restore(source, "html");
|
|
1024
|
+
}
|
|
1025
|
+
function splitTableRow(line) {
|
|
1026
|
+
const trimmed = line.trim().replace(/^\|/, "");
|
|
1027
|
+
const cells = [];
|
|
1028
|
+
let current = "";
|
|
1029
|
+
for (let index = 0; index < trimmed.length; index += 1) {
|
|
1030
|
+
const character = trimmed[index];
|
|
1031
|
+
const next = trimmed[index + 1];
|
|
1032
|
+
if (character === "\\" && (next === "\\" || next === "|")) {
|
|
1033
|
+
current += next === "|" ? next : "\\\\";
|
|
1034
|
+
index += 1;
|
|
1035
|
+
} else if (character === "|") {
|
|
1036
|
+
cells.push(current.trim());
|
|
1037
|
+
if (index === trimmed.length - 1) return cells;
|
|
1038
|
+
current = "";
|
|
1039
|
+
} else {
|
|
1040
|
+
current += character;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
cells.push(current.trim());
|
|
1044
|
+
return cells;
|
|
1045
|
+
}
|
|
1046
|
+
function isTableSeparator(line) {
|
|
1047
|
+
const cells = splitTableRow(line);
|
|
1048
|
+
return cells.length > 0 && cells.every((cell) => /^:?-{3,}:?$/.test(cell.replace(/\s+/g, "")));
|
|
1049
|
+
}
|
|
1050
|
+
function renderTable(lines, startIndex) {
|
|
1051
|
+
const header = lines[startIndex];
|
|
1052
|
+
const separator = lines[startIndex + 1];
|
|
1053
|
+
if (header === void 0 || separator === void 0 || !header.includes("|") || !isTableSeparator(separator)) {
|
|
1054
|
+
return null;
|
|
1055
|
+
}
|
|
1056
|
+
const headerCells = splitTableRow(header);
|
|
1057
|
+
const bodyRows = [];
|
|
1058
|
+
let width = headerCells.length;
|
|
1059
|
+
let nextIndex = startIndex + 2;
|
|
1060
|
+
while (nextIndex < lines.length && lines[nextIndex]?.trim() && lines[nextIndex]?.includes("|")) {
|
|
1061
|
+
const row = splitTableRow(lines[nextIndex]);
|
|
1062
|
+
bodyRows.push(row);
|
|
1063
|
+
width = Math.max(width, row.length);
|
|
1064
|
+
nextIndex += 1;
|
|
1065
|
+
}
|
|
1066
|
+
const normalizeRow = (row) => Array.from({ length: width }, (_unused, index) => row[index] ?? "");
|
|
1067
|
+
const head = normalizeRow(headerCells).map((cell) => `<th scope="col">${renderInlineMarkdown(cell)}</th>`).join("");
|
|
1068
|
+
const body = bodyRows.map(
|
|
1069
|
+
(row) => `<tr>${normalizeRow(row).map((cell) => `<td>${renderInlineMarkdown(cell)}</td>`).join("")}</tr>`
|
|
1070
|
+
).join("");
|
|
1071
|
+
return {
|
|
1072
|
+
html: `<table><thead><tr>${head}</tr></thead>${body ? `<tbody>${body}</tbody>` : ""}</table>`,
|
|
1073
|
+
nextIndex
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
var listPattern = /^( *)(?:(\d+)[.)]|[-+*]) +(.*)$/;
|
|
1077
|
+
function serializeBlocks(blocks) {
|
|
1078
|
+
return blocks.map(
|
|
1079
|
+
(block2) => block2.type === "html" ? block2.html : `<pre><code>${escapeHtml(block2.value)}</code></pre>`
|
|
1080
|
+
).join("");
|
|
1081
|
+
}
|
|
1082
|
+
function parseBlocks(lines, depth = 0) {
|
|
1083
|
+
if (depth > 48)
|
|
1084
|
+
throw new Error("Markdown nesting exceeds the supported 48 levels.");
|
|
1085
|
+
const blocks = [];
|
|
1086
|
+
const paragraph = [];
|
|
1087
|
+
const pushHtml = (html) => blocks.push({ type: "html", html: sanitizeRichTextMarkup(html) });
|
|
1088
|
+
const flush = () => {
|
|
1089
|
+
if (!paragraph.length) return;
|
|
1090
|
+
pushHtml(
|
|
1091
|
+
`<p>${paragraph.map((line) => renderInlineMarkdown(line.trim())).join(" ").split(HARD_BREAK_TOKEN).join("<br>")}</p>`
|
|
1092
|
+
);
|
|
1093
|
+
paragraph.length = 0;
|
|
1094
|
+
};
|
|
1095
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1096
|
+
const line = lines[index] ?? "";
|
|
1097
|
+
const trimmed = line.trim();
|
|
1098
|
+
if (!trimmed) {
|
|
1099
|
+
flush();
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
const fence = /^ {0,3}(`{3,}|~{3,})([^\n]*)$/.exec(line);
|
|
1103
|
+
if (fence && !(fence[1].startsWith("`") && fence[2].includes("`"))) {
|
|
1104
|
+
flush();
|
|
1105
|
+
const marker = fence[1];
|
|
1106
|
+
const language = fence[2].trim().split(/\s+/)[0];
|
|
1107
|
+
const code = [];
|
|
1108
|
+
const closing = new RegExp(`^ {0,3}${marker[0]}{${marker.length},} *$`);
|
|
1109
|
+
index += 1;
|
|
1110
|
+
for (; index < lines.length && !closing.test(lines[index]); index += 1)
|
|
1111
|
+
code.push(lines[index]);
|
|
1112
|
+
blocks.push({
|
|
1113
|
+
type: "code",
|
|
1114
|
+
value: code.join("\n"),
|
|
1115
|
+
language: language || void 0
|
|
1116
|
+
});
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
const table = renderTable(lines, index);
|
|
1120
|
+
if (table) {
|
|
1121
|
+
flush();
|
|
1122
|
+
pushHtml(table.html);
|
|
1123
|
+
index = table.nextIndex - 1;
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
const heading = /^(#{1,6})\s+(.+?)\s*#*$/.exec(trimmed);
|
|
1127
|
+
if (heading) {
|
|
1128
|
+
flush();
|
|
1129
|
+
const level = heading[1].length;
|
|
1130
|
+
pushHtml(`<h${level}>${renderInlineMarkdown(heading[2])}</h${level}>`);
|
|
1131
|
+
continue;
|
|
1132
|
+
}
|
|
1133
|
+
if (/^(?:-{3,}|\*{3,}|_{3,})$/.test(trimmed.replace(/\s+/g, ""))) {
|
|
1134
|
+
flush();
|
|
1135
|
+
pushHtml("<hr>");
|
|
1136
|
+
continue;
|
|
1137
|
+
}
|
|
1138
|
+
if (/^>/.test(trimmed)) {
|
|
1139
|
+
flush();
|
|
1140
|
+
const quote = [];
|
|
1141
|
+
while (index < lines.length && /^\s*>/.test(lines[index])) {
|
|
1142
|
+
quote.push(lines[index].replace(/^\s*> ?/, ""));
|
|
1143
|
+
index += 1;
|
|
1144
|
+
}
|
|
1145
|
+
index -= 1;
|
|
1146
|
+
pushHtml(
|
|
1147
|
+
`<blockquote>${serializeBlocks(parseBlocks(quote, depth + 1))}</blockquote>`
|
|
1148
|
+
);
|
|
1149
|
+
continue;
|
|
1150
|
+
}
|
|
1151
|
+
const firstItem = listPattern.exec(line);
|
|
1152
|
+
if (firstItem) {
|
|
1153
|
+
flush();
|
|
1154
|
+
const indent = firstItem[1].length;
|
|
1155
|
+
const ordered = firstItem[2] !== void 0;
|
|
1156
|
+
const items = [];
|
|
1157
|
+
let hasTasks = false;
|
|
1158
|
+
while (index < lines.length) {
|
|
1159
|
+
const item = listPattern.exec(lines[index]);
|
|
1160
|
+
if (!item || item[1].length !== indent || item[2] !== void 0 !== ordered)
|
|
1161
|
+
break;
|
|
1162
|
+
const contentIndent = lines[index].length - item[3].length;
|
|
1163
|
+
const task = /^\[([ xX])\] +(.*)$/.exec(item[3]);
|
|
1164
|
+
const content = task ? task[2] : item[3];
|
|
1165
|
+
if (task) hasTasks = true;
|
|
1166
|
+
const continuation = [];
|
|
1167
|
+
index += 1;
|
|
1168
|
+
for (; index < lines.length; index += 1) {
|
|
1169
|
+
const next = lines[index];
|
|
1170
|
+
if (!next.trim()) {
|
|
1171
|
+
if ((/^ */.exec(lines[index + 1] ?? "")?.[0].length ?? 0) >= contentIndent) {
|
|
1172
|
+
continuation.push("");
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1175
|
+
break;
|
|
1176
|
+
}
|
|
1177
|
+
const spaces = /^ */.exec(next)[0].length;
|
|
1178
|
+
if (spaces < contentIndent) break;
|
|
1179
|
+
continuation.push(next.slice(contentIndent));
|
|
1180
|
+
}
|
|
1181
|
+
const nested = continuation.length ? serializeBlocks(parseBlocks(continuation, depth + 1)) : "";
|
|
1182
|
+
const checked = task?.[1]?.toLowerCase() === "x";
|
|
1183
|
+
const label = task ? `<label><input type="checkbox"${checked ? ' checked=""' : ""}>${renderInlineMarkdown(content)}</label>` : renderInlineMarkdown(content);
|
|
1184
|
+
items.push(
|
|
1185
|
+
`<li${task ? ` data-checked="${checked}"` : ""}>${label}${nested}</li>`
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
index -= 1;
|
|
1189
|
+
const tag = ordered ? "ol" : "ul";
|
|
1190
|
+
pushHtml(
|
|
1191
|
+
`<${tag}${hasTasks && !ordered ? ' data-task-list="true"' : ""}>${items.join("")}</${tag}>`
|
|
1192
|
+
);
|
|
1193
|
+
continue;
|
|
1194
|
+
}
|
|
1195
|
+
paragraph.push(
|
|
1196
|
+
/ {2,}$/.test(line) ? `${line.trimEnd()}${HARD_BREAK_TOKEN}` : line
|
|
1197
|
+
);
|
|
1198
|
+
}
|
|
1199
|
+
flush();
|
|
1200
|
+
return blocks;
|
|
1201
|
+
}
|
|
1202
|
+
function markdownToRichTextBlocks(markdown, options = {}) {
|
|
1203
|
+
return parseBlocks(normalizeMarkdown(markdown, options).split("\n"));
|
|
1204
|
+
}
|
|
1205
|
+
function markdownToRichTextHtml(markdown, options = {}) {
|
|
1206
|
+
return serializeBlocks(markdownToRichTextBlocks(markdown, options));
|
|
1207
|
+
}
|
|
1208
|
+
function createRichTextDocumentFromMarkdown(markdown, options = {}) {
|
|
1209
|
+
const html = markdownToRichTextHtml(markdown, options);
|
|
1210
|
+
return {
|
|
1211
|
+
html,
|
|
1212
|
+
plainText: richTextHtmlToPlainText(html),
|
|
1213
|
+
schemaVersion: QUICK_BUILDER_RICH_TEXT_SCHEMA_VERSION
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
// src/view-tree.ts
|
|
1218
|
+
function richTextHtmlToViewTree(value) {
|
|
1219
|
+
if (value.length > 1e6)
|
|
1220
|
+
throw new RangeError("Rich text exceeds the rendering limit");
|
|
1221
|
+
const html = normalizeRichTextHtml(value).replace(/<!--[\s\S]*?-->/g, "");
|
|
1222
|
+
const root = [];
|
|
1223
|
+
const stack = [
|
|
1224
|
+
{ tag: "", children: root }
|
|
1225
|
+
];
|
|
1226
|
+
let count = 0;
|
|
1227
|
+
const tokens = /<\/?([a-z][a-z\d-]*)\b((?:"[^"]*"|'[^']*'|[^'">])*)>|([^<]+)|</gi;
|
|
1228
|
+
for (const match of html.matchAll(tokens)) {
|
|
1229
|
+
if (++count > 2e4 || stack.length > 64)
|
|
1230
|
+
throw new RangeError("Rich text exceeds the rendering complexity limit");
|
|
1231
|
+
const current = stack[stack.length - 1];
|
|
1232
|
+
if (!match[1]) {
|
|
1233
|
+
current.children.push({ type: "text", value: decodeRichTextEntities(match[3] ?? "<") });
|
|
1234
|
+
continue;
|
|
1235
|
+
}
|
|
1236
|
+
const tag = match[1].toLowerCase();
|
|
1237
|
+
if (match[0].startsWith("</")) {
|
|
1238
|
+
if (stack.length === 1 || current.tag !== tag)
|
|
1239
|
+
throw new Error(`Unbalanced rich-text closing tag ${tag}`);
|
|
1240
|
+
stack.pop();
|
|
1241
|
+
continue;
|
|
1242
|
+
}
|
|
1243
|
+
const attributes = {};
|
|
1244
|
+
for (const attribute of match[2].matchAll(/([\w-]+)="([^"]*)"/g))
|
|
1245
|
+
attributes[attribute[1]] = decodeRichTextEntities(attribute[2]);
|
|
1246
|
+
const node = {
|
|
1247
|
+
type: "element",
|
|
1248
|
+
tag,
|
|
1249
|
+
attributes,
|
|
1250
|
+
children: []
|
|
1251
|
+
};
|
|
1252
|
+
current.children.push(node);
|
|
1253
|
+
if (!richTextVoidTags.some((voidTag) => voidTag === tag)) stack.push(node);
|
|
1254
|
+
}
|
|
1255
|
+
if (stack.length !== 1) throw new Error("Unclosed rich-text element");
|
|
1256
|
+
return root;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
// src/svg-export.ts
|
|
1260
|
+
var block = /* @__PURE__ */ new Set(["p", "div", "blockquote", "pre", "li", "h1", "h2", "h3", "h4", "h5", "h6", "tr"]);
|
|
1261
|
+
var rowHeight = 24;
|
|
1262
|
+
function xml(text) {
|
|
1263
|
+
return text.replace(/[<>&"']/g, (c) => ({ "<": "<", ">": ">", "&": "&", '"': """, "'": "'" })[c]);
|
|
1264
|
+
}
|
|
1265
|
+
function richTextExportImageSources(html) {
|
|
1266
|
+
const sources = /* @__PURE__ */ new Set();
|
|
1267
|
+
function visit(nodes) {
|
|
1268
|
+
for (const node of nodes) {
|
|
1269
|
+
if (node.type !== "element") continue;
|
|
1270
|
+
if (node.tag === "img") sources.add(node.attributes.src);
|
|
1271
|
+
visit(node.children);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
visit(richTextHtmlToViewTree(html));
|
|
1275
|
+
return [...sources];
|
|
1276
|
+
}
|
|
1277
|
+
function exportRichTextSvgPages(html, options) {
|
|
1278
|
+
const width = options.width ?? 800, capacity = options.linesPerPage ?? 45;
|
|
1279
|
+
if (!Number.isInteger(width) || width < 240 || width > 2400 || !Number.isInteger(capacity) || capacity < 1 || capacity > 100)
|
|
1280
|
+
throw new RangeError("Invalid rich-text report dimensions.");
|
|
1281
|
+
const lineWidth = Math.floor((width - 64) / 8), pageHeight = capacity * rowHeight;
|
|
1282
|
+
const rows = [];
|
|
1283
|
+
let line = [], remaining = lineWidth, totalImageBytes = 0;
|
|
1284
|
+
function push(row) {
|
|
1285
|
+
rows.push(row);
|
|
1286
|
+
if (rows.length > capacity * 150) throw new RangeError("Rich text exceeds the SVG page limit.");
|
|
1287
|
+
}
|
|
1288
|
+
function nextLine() {
|
|
1289
|
+
if (!line.length) return;
|
|
1290
|
+
const content = line.map((run) => `<tspan${run.bold ? ' font-weight="700"' : ""}${run.italic ? ' font-style="italic"' : ""}${run.underline ? ' text-decoration="underline"' : ""}>${xml(run.text)}</tspan>`).join("");
|
|
1291
|
+
push({ height: rowHeight, markup: (y) => `<text x="32" y="${y + 16}" font-size="15" xml:space="preserve">${content}</text>` });
|
|
1292
|
+
line = [];
|
|
1293
|
+
remaining = lineWidth;
|
|
1294
|
+
}
|
|
1295
|
+
function add(text, marks) {
|
|
1296
|
+
for (const word of text.split(/(\s+)/u)) {
|
|
1297
|
+
if (!word) continue;
|
|
1298
|
+
const normalized = /\s/u.test(word) ? " " : word;
|
|
1299
|
+
const chars = Array.from(normalized);
|
|
1300
|
+
if (chars.length > remaining && chars.length <= lineWidth) nextLine();
|
|
1301
|
+
if (normalized === " " && remaining === lineWidth) continue;
|
|
1302
|
+
for (let offset = 0; offset < chars.length; ) {
|
|
1303
|
+
if (!remaining) nextLine();
|
|
1304
|
+
const part = chars.slice(offset, offset + remaining);
|
|
1305
|
+
const previous = line[line.length - 1], value = part.join("");
|
|
1306
|
+
if (previous && previous.bold === marks.bold && previous.italic === marks.italic && previous.underline === marks.underline)
|
|
1307
|
+
previous.text += value;
|
|
1308
|
+
else line.push({ ...marks, text: value });
|
|
1309
|
+
offset += part.length;
|
|
1310
|
+
remaining -= part.length;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
function addImage(attributes) {
|
|
1315
|
+
nextLine();
|
|
1316
|
+
const image = options.images?.get(attributes.src);
|
|
1317
|
+
if (!image || !/^data:image\/(?:png|jpeg);base64,[A-Za-z0-9+/]+={0,2}$/u.test(image.dataUrl) || !Number.isFinite(image.width) || !Number.isFinite(image.height) || image.width <= 0 || image.height <= 0)
|
|
1318
|
+
throw new Error("A document image could not be embedded in this export.");
|
|
1319
|
+
totalImageBytes += image.dataUrl.length;
|
|
1320
|
+
if (totalImageBytes > 12e6) throw new RangeError("Document image content exceeds the export limit.");
|
|
1321
|
+
const requestedWidth = attributes.width ? Number(attributes.width) : attributes.height ? Number(attributes.height) * image.width / image.height : image.width;
|
|
1322
|
+
const requestedHeight = attributes.height ? Number(attributes.height) : requestedWidth * image.height / image.width;
|
|
1323
|
+
const scale = Math.min(1, (width - 64) / requestedWidth, (pageHeight - 4) / requestedHeight);
|
|
1324
|
+
const w = requestedWidth * scale, h = requestedHeight * scale;
|
|
1325
|
+
push({ height: h + 4, markup: (y) => `<image x="32" y="${y}" width="${w}" height="${h}" href="${image.dataUrl}" preserveAspectRatio="xMinYMin meet"><title>${xml(attributes.alt ?? "")}</title></image>` });
|
|
1326
|
+
}
|
|
1327
|
+
function visit(nodes, marks = {}) {
|
|
1328
|
+
for (const node of nodes) {
|
|
1329
|
+
if (node.type === "text") {
|
|
1330
|
+
add(node.value, marks);
|
|
1331
|
+
continue;
|
|
1332
|
+
}
|
|
1333
|
+
const { tag } = node;
|
|
1334
|
+
if (block.has(tag) || tag === "br") nextLine();
|
|
1335
|
+
if (tag === "li") add("\u2022 ", marks);
|
|
1336
|
+
if (tag === "img") addImage(node.attributes);
|
|
1337
|
+
if (tag === "qb-video") {
|
|
1338
|
+
nextLine();
|
|
1339
|
+
add(`Video: ${node.attributes["data-title"]}`, { ...marks, bold: true });
|
|
1340
|
+
nextLine();
|
|
1341
|
+
}
|
|
1342
|
+
visit(node.children, {
|
|
1343
|
+
bold: marks.bold || tag === "b" || tag === "strong" || tag === "th" || /^h[1-6]$/.test(tag),
|
|
1344
|
+
italic: marks.italic || tag === "em" || tag === "i" || tag === "blockquote",
|
|
1345
|
+
underline: marks.underline || tag === "u" || tag === "a"
|
|
1346
|
+
});
|
|
1347
|
+
if (tag === "td" || tag === "th") add(" \u2502 ", marks);
|
|
1348
|
+
if (block.has(tag)) nextLine();
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
visit(richTextHtmlToViewTree(html));
|
|
1352
|
+
nextLine();
|
|
1353
|
+
const pages = [{ content: [], height: 0 }];
|
|
1354
|
+
for (const row of rows) {
|
|
1355
|
+
let page = pages[pages.length - 1];
|
|
1356
|
+
if (page.height + row.height > pageHeight) {
|
|
1357
|
+
if (pages.length === 150) throw new RangeError("Rich text exceeds the SVG page limit.");
|
|
1358
|
+
page = { content: [], height: 0 };
|
|
1359
|
+
pages.push(page);
|
|
1360
|
+
}
|
|
1361
|
+
page.content.push(row.markup(68 + page.height));
|
|
1362
|
+
page.height += row.height;
|
|
1363
|
+
}
|
|
1364
|
+
return pages.map((page) => {
|
|
1365
|
+
const height = Math.max(180, 94 + page.height);
|
|
1366
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" font-family="system-ui, sans-serif" fill="#20252e"><rect width="100%" height="100%" fill="#fff"/><text x="32" y="47" font-size="24" font-weight="700">${xml(options.title.slice(0, 200))}</text>${page.content.join("")}</svg>`;
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
export { QUICK_BUILDER_RICH_TEXT_FRAMEWORKS, QUICK_BUILDER_RICH_TEXT_SCHEMA_VERSION, applyRichTextTransaction, canRedoRichTextTransaction, canUndoRichTextTransaction, createReactRichTextBinding, createRichTextBinding, createRichTextDocumentFromMarkdown, createRichTextDocumentSnapshot, createRichTextExtensionRegistry, createRichTextHistory, createRichTextRuntimeState, createSvelteRichTextBinding, createVueRichTextBinding, createWebcomponentsRichTextBinding, defaultRichTextHistoryLimit, exportRichTextSvgPages, getRichTextCommandDefinition, isRichTextAlignment, isRichTextBlockTag, isRichTextColor, isRichTextFontFamily, isRichTextFontSize, isRichTextLineHeight, isSafeRichTextHref, isSafeRichTextImageSrc, markdownToRichTextBlocks, markdownToRichTextHtml, moveRichTextHistory, normalizeRichTextHtml, plainTextToRichTextHtml, pushRichTextHistory, resolveRichTextExtensions, rewriteRichTextImageSources, richTextAiAssistantExtension, richTextAlignmentCommandByValue, richTextAlignmentTags, richTextAllowedAlignments, richTextAllowedColors, richTextAllowedFontFamilies, richTextAllowedFontSizes, richTextAllowedLineHeights, richTextAllowedTags, richTextAllowedTargets, richTextBlockFormatByTagName, richTextBlockFormats, richTextBlockSelector, richTextCollaborationExtension, richTextCommandRegistry, richTextExportImageSources, richTextExtensionDefinitions, richTextExtensionRegistry, richTextHtmlToPlainText, richTextHtmlToViewTree, richTextMaximumImageTextLength, richTextMaximumUrlLength, richTextMentionExtension, richTextPagesExtension, richTextSlashCommandExtension, richTextTableExtension, richTextVoidTags, sanitizeRichTextMarkup, splitRichTextVideoBlocks, stripActiveMarkup, validateRichTextVideo };
|
|
1371
|
+
//# sourceMappingURL=index.mjs.map
|
|
1372
|
+
//# sourceMappingURL=index.mjs.map
|