@vettvangur/ai-sdk 0.14.0 → 0.16.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/README.md +63 -11
- package/dist/binmx.d.ts +1 -1
- package/dist/chat.d.ts +132 -0
- package/dist/chat.js +33 -0
- package/dist/contracts.d.ts +16 -9
- package/dist/dom.d.ts +20 -0
- package/dist/events.js +76 -0
- package/dist/index.d.ts +15 -138
- package/dist/index.js +3 -33
- package/dist/live-chat/aichat.d.ts +1 -1
- package/dist/markdown.js +230 -0
- package/dist/mount.d.ts +1 -1
- package/dist/omnichannel.js +1 -0
- package/dist/styles.css +43 -2
- package/dist/survey.js +3 -230
- package/dist/window.js +59 -81
- package/package.json +6 -2
package/dist/index.js
CHANGED
|
@@ -1,34 +1,4 @@
|
|
|
1
1
|
import { a as isSafeLinkUrl, n as createSafeImage, r as createSafeLink, t as createPart } from "./dom.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Tells the AI what the visitor entered into a form embedded in the thread. Whenever `element`
|
|
7
|
-
* changes and holds `options.form`, its filled-in `fields` are summarised as `label: value` and
|
|
8
|
-
* sent with every later question through `binding.setFormContext`, framed as context rather than
|
|
9
|
-
* something the visitor said. Returns a function that stops watching.
|
|
10
|
-
*/
|
|
11
|
-
function watchFormContext(binding, element, options) {
|
|
12
|
-
const record = () => {
|
|
13
|
-
const form = element.querySelector(options.form);
|
|
14
|
-
if (!form) return;
|
|
15
|
-
const values = new FormData(form);
|
|
16
|
-
const parts = options.fields.map(([field, label]) => {
|
|
17
|
-
const raw = String(values.get(field) ?? "").trim();
|
|
18
|
-
const value = options.values?.[raw] ?? raw;
|
|
19
|
-
return value ? `${label}: ${value}` : "";
|
|
20
|
-
}).filter(Boolean);
|
|
21
|
-
if (!parts.length) return;
|
|
22
|
-
const note = options.note ? ` ${options.note}` : "";
|
|
23
|
-
binding.setFormContext(options.name, `Context, not a message from the visitor: they filled in ${options.title} with ${parts.join(", ")}.${note}`, options.followUp);
|
|
24
|
-
};
|
|
25
|
-
const observer = new MutationObserver(record);
|
|
26
|
-
observer.observe(element, {
|
|
27
|
-
childList: true,
|
|
28
|
-
subtree: true
|
|
29
|
-
});
|
|
30
|
-
record();
|
|
31
|
-
return () => observer.disconnect();
|
|
32
|
-
}
|
|
33
|
-
//#endregion
|
|
34
|
-
export { ACTIONS_CLASS, AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_CLASS, CARD_LIST_CLASS, CHAT_CLASS, HANDOVER_TOOL, LIVE_CHAT_ACTION, MAX_ANSWER_CARD_FACTS, MAX_ANSWER_CARD_LINKS, MAX_ANSWER_CARD_SECTIONS, MAX_ANSWER_CARD_TABLES, MAX_ANSWER_CARD_TABLE_CELLS, MAX_ANSWER_CARD_TABLE_ROWS, MAX_ANSWER_CARD_TEXT_LENGTH, MAX_ANSWER_CARD_TOTAL_STRUCTURAL_ITEMS, MAX_ANSWER_CARD_TOTAL_TABLE_CELLS, MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS, MAX_ANSWER_CARD_URL_LENGTH, assertAnswerCardContract, bindChat, bindChatWindow, bindLauncherTeaser, closeHandoverCard, createAiChatRegistry, createChatSession, createPart, createSafeImage, createSafeLink, createSingleSurface, createTemplateItemSurface, isAnswerCard, isHandoverPayload, isSafeLinkUrl, parseStreamEvent, readEventStream, renderActions, renderAnswerCard, renderCardList, renderHandoverCard, watchFormContext };
|
|
2
|
+
import { t as readServerEvents } from "./events.js";
|
|
3
|
+
import { r as renderMarkdown, t as MAX_MARKDOWN_CHARACTERS } from "./markdown.js";
|
|
4
|
+
export { MAX_MARKDOWN_CHARACTERS, createPart, createSafeImage, createSafeLink, isSafeLinkUrl, readServerEvents, renderMarkdown };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { l as AiLiveChatSurveyField, o as AiLiveChatProvider } from "../provider.js";
|
|
2
|
-
import {
|
|
2
|
+
import { N as AiLiveChatFactoryContext } from "../contracts.js";
|
|
3
3
|
//#region src/live-chat/aichat.d.ts
|
|
4
4
|
type AiChatLiveConfig = {
|
|
5
5
|
/** The prefix the site maps the chat under with `MapAiChat`, such as `/webapi/aichat`. */
|
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { a as isSafeLinkUrl, r as createSafeLink } from "./dom.js";
|
|
2
|
+
//#region src/markdown.ts
|
|
3
|
+
const MAX_MARKDOWN_CHARACTERS = 8e3;
|
|
4
|
+
function isWordCharacter(character) {
|
|
5
|
+
return character !== void 0 && /[\p{L}\p{N}]/u.test(character);
|
|
6
|
+
}
|
|
7
|
+
function delimitedToken(source, start, marker, type) {
|
|
8
|
+
if (!source.startsWith(marker, start)) return null;
|
|
9
|
+
const contentStart = start + marker.length;
|
|
10
|
+
const closing = source.indexOf(marker, contentStart);
|
|
11
|
+
if (closing === -1 || closing === contentStart) return null;
|
|
12
|
+
return {
|
|
13
|
+
type,
|
|
14
|
+
content: source.slice(contentStart, closing),
|
|
15
|
+
end: closing + marker.length
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function linkToken(source, start) {
|
|
19
|
+
if (source[start] !== "[") return null;
|
|
20
|
+
let labelEnd = start + 1;
|
|
21
|
+
while (labelEnd < source.length) {
|
|
22
|
+
if (source[labelEnd] === "[") return null;
|
|
23
|
+
if (source[labelEnd] === "]" && source[labelEnd + 1] === "(") break;
|
|
24
|
+
labelEnd += 1;
|
|
25
|
+
}
|
|
26
|
+
if (labelEnd === source.length) return {
|
|
27
|
+
type: "text",
|
|
28
|
+
content: source.slice(start),
|
|
29
|
+
end: source.length
|
|
30
|
+
};
|
|
31
|
+
if (labelEnd === start + 1) return null;
|
|
32
|
+
let depth = 1;
|
|
33
|
+
let end = labelEnd + 2;
|
|
34
|
+
while (end < source.length && depth > 0) {
|
|
35
|
+
if (source[end] === "(") depth += 1;
|
|
36
|
+
else if (source[end] === ")") depth -= 1;
|
|
37
|
+
end += 1;
|
|
38
|
+
}
|
|
39
|
+
if (depth !== 0) return {
|
|
40
|
+
type: "text",
|
|
41
|
+
content: source.slice(start),
|
|
42
|
+
end: source.length
|
|
43
|
+
};
|
|
44
|
+
const url = source.slice(labelEnd + 2, end - 1);
|
|
45
|
+
if (!isSafeLinkUrl(url)) return {
|
|
46
|
+
type: "text",
|
|
47
|
+
content: source.slice(start, end),
|
|
48
|
+
end
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
type: "link",
|
|
52
|
+
label: source.slice(start + 1, labelEnd),
|
|
53
|
+
url,
|
|
54
|
+
end
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function inlineToken(source, start) {
|
|
58
|
+
const link = linkToken(source, start);
|
|
59
|
+
if (link) return link;
|
|
60
|
+
const code = delimitedToken(source, start, "`", "code");
|
|
61
|
+
if (code) return code;
|
|
62
|
+
const bold = delimitedToken(source, start, "**", "bold") ?? delimitedToken(source, start, "__", "bold");
|
|
63
|
+
if (bold) return bold;
|
|
64
|
+
const marker = source[start];
|
|
65
|
+
if ((marker === "*" || marker === "_") && !isWordCharacter(source[start - 1])) return delimitedToken(source, start, marker, "italic");
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
function inlineStructuralItemCount(source) {
|
|
69
|
+
let count = 0;
|
|
70
|
+
let index = 0;
|
|
71
|
+
while (index < source.length) {
|
|
72
|
+
const token = inlineToken(source, index);
|
|
73
|
+
if (!token) {
|
|
74
|
+
index += 1;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
switch (token.type) {
|
|
78
|
+
case "bold":
|
|
79
|
+
case "italic":
|
|
80
|
+
count += 1 + inlineStructuralItemCount(token.content);
|
|
81
|
+
break;
|
|
82
|
+
case "code":
|
|
83
|
+
case "link": count += 1;
|
|
84
|
+
}
|
|
85
|
+
index = token.end;
|
|
86
|
+
}
|
|
87
|
+
return count;
|
|
88
|
+
}
|
|
89
|
+
function appendInlineMarkdown(document, parent, source) {
|
|
90
|
+
let textStart = 0;
|
|
91
|
+
let index = 0;
|
|
92
|
+
while (index < source.length) {
|
|
93
|
+
const token = inlineToken(source, index);
|
|
94
|
+
if (!token) {
|
|
95
|
+
index += 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (textStart < index) parent.append(document.createTextNode(source.slice(textStart, index)));
|
|
99
|
+
switch (token.type) {
|
|
100
|
+
case "code": {
|
|
101
|
+
const code = document.createElement("code");
|
|
102
|
+
code.append(document.createTextNode(token.content));
|
|
103
|
+
parent.append(code);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "bold": {
|
|
107
|
+
const strong = document.createElement("strong");
|
|
108
|
+
appendInlineMarkdown(document, strong, token.content);
|
|
109
|
+
parent.append(strong);
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
case "italic": {
|
|
113
|
+
const emphasis = document.createElement("em");
|
|
114
|
+
appendInlineMarkdown(document, emphasis, token.content);
|
|
115
|
+
parent.append(emphasis);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case "link": {
|
|
119
|
+
const link = createSafeLink(document, token.label, token.url);
|
|
120
|
+
if (link) parent.append(link);
|
|
121
|
+
else parent.append(document.createTextNode(source.slice(index, token.end)));
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case "text": parent.append(document.createTextNode(token.content));
|
|
125
|
+
}
|
|
126
|
+
index = token.end;
|
|
127
|
+
textStart = index;
|
|
128
|
+
}
|
|
129
|
+
if (textStart < source.length) parent.append(document.createTextNode(source.slice(textStart)));
|
|
130
|
+
}
|
|
131
|
+
function unorderedListItem(line) {
|
|
132
|
+
return /^\s*[-+*]\s+(.+)$/.exec(line)?.[1] ?? null;
|
|
133
|
+
}
|
|
134
|
+
function orderedListItem(line) {
|
|
135
|
+
return /^\s*\d+[.)]\s+(.+)$/.exec(line)?.[1] ?? null;
|
|
136
|
+
}
|
|
137
|
+
function heading(line) {
|
|
138
|
+
return /^\s{0,3}#{1,6}\s+(.+?)\s*#*\s*$/.exec(line)?.[1] ?? null;
|
|
139
|
+
}
|
|
140
|
+
function countMarkdownStructuralItems(source) {
|
|
141
|
+
const lines = source.slice(0, MAX_MARKDOWN_CHARACTERS).split(/\r\n?|\n/);
|
|
142
|
+
let count = 0;
|
|
143
|
+
let index = 0;
|
|
144
|
+
while (index < lines.length) {
|
|
145
|
+
const line = lines[index];
|
|
146
|
+
if (line.trim().length === 0) {
|
|
147
|
+
index += 1;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const headingText = heading(line);
|
|
151
|
+
if (headingText !== null) {
|
|
152
|
+
count += 2 + inlineStructuralItemCount(headingText);
|
|
153
|
+
index += 1;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const unorderedItem = unorderedListItem(line);
|
|
157
|
+
const orderedItem = orderedListItem(line);
|
|
158
|
+
if (unorderedItem !== null || orderedItem !== null) {
|
|
159
|
+
const itemForLine = unorderedItem === null ? orderedListItem : unorderedListItem;
|
|
160
|
+
count += 1;
|
|
161
|
+
while (index < lines.length) {
|
|
162
|
+
const item = itemForLine(lines[index]);
|
|
163
|
+
if (item === null) break;
|
|
164
|
+
count += 1 + inlineStructuralItemCount(item);
|
|
165
|
+
index += 1;
|
|
166
|
+
}
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const paragraphLines = [];
|
|
170
|
+
while (index < lines.length) {
|
|
171
|
+
const paragraphLine = lines[index];
|
|
172
|
+
if (paragraphLine.trim().length === 0 || heading(paragraphLine) !== null || unorderedListItem(paragraphLine) !== null || orderedListItem(paragraphLine) !== null) break;
|
|
173
|
+
paragraphLines.push(paragraphLine);
|
|
174
|
+
index += 1;
|
|
175
|
+
}
|
|
176
|
+
count += 1 + inlineStructuralItemCount(paragraphLines.join(" "));
|
|
177
|
+
}
|
|
178
|
+
return count;
|
|
179
|
+
}
|
|
180
|
+
function renderMarkdown(document, source) {
|
|
181
|
+
const fragment = document.createDocumentFragment();
|
|
182
|
+
const lines = source.slice(0, MAX_MARKDOWN_CHARACTERS).split(/\r\n?|\n/);
|
|
183
|
+
let index = 0;
|
|
184
|
+
while (index < lines.length) {
|
|
185
|
+
const line = lines[index];
|
|
186
|
+
if (line.trim().length === 0) {
|
|
187
|
+
index += 1;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const headingText = heading(line);
|
|
191
|
+
if (headingText !== null) {
|
|
192
|
+
const paragraph = document.createElement("p");
|
|
193
|
+
const strong = document.createElement("strong");
|
|
194
|
+
appendInlineMarkdown(document, strong, headingText);
|
|
195
|
+
paragraph.append(strong);
|
|
196
|
+
fragment.append(paragraph);
|
|
197
|
+
index += 1;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const unorderedItem = unorderedListItem(line);
|
|
201
|
+
const orderedItem = orderedListItem(line);
|
|
202
|
+
if (unorderedItem !== null || orderedItem !== null) {
|
|
203
|
+
const list = document.createElement(unorderedItem === null ? "ol" : "ul");
|
|
204
|
+
const itemForLine = unorderedItem === null ? orderedListItem : unorderedListItem;
|
|
205
|
+
while (index < lines.length) {
|
|
206
|
+
const item = itemForLine(lines[index]);
|
|
207
|
+
if (item === null) break;
|
|
208
|
+
const listItem = document.createElement("li");
|
|
209
|
+
appendInlineMarkdown(document, listItem, item);
|
|
210
|
+
list.append(listItem);
|
|
211
|
+
index += 1;
|
|
212
|
+
}
|
|
213
|
+
fragment.append(list);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
const paragraphLines = [];
|
|
217
|
+
while (index < lines.length) {
|
|
218
|
+
const paragraphLine = lines[index];
|
|
219
|
+
if (paragraphLine.trim().length === 0 || heading(paragraphLine) !== null || unorderedListItem(paragraphLine) !== null || orderedListItem(paragraphLine) !== null) break;
|
|
220
|
+
paragraphLines.push(paragraphLine);
|
|
221
|
+
index += 1;
|
|
222
|
+
}
|
|
223
|
+
const paragraph = document.createElement("p");
|
|
224
|
+
appendInlineMarkdown(document, paragraph, paragraphLines.join(" "));
|
|
225
|
+
fragment.append(paragraph);
|
|
226
|
+
}
|
|
227
|
+
return fragment;
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
export { countMarkdownStructuralItems as n, renderMarkdown as r, MAX_MARKDOWN_CHARACTERS as t };
|
package/dist/mount.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { o as AiLiveChatProvider } from "./provider.js";
|
|
2
|
-
import {
|
|
2
|
+
import { N as AiLiveChatFactoryContext, l as AiChatBinding, u as AiChatBindings } from "./contracts.js";
|
|
3
3
|
//#region src/mount.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* The provider a person answers through, chosen by the root's attributes: Omnichannel when
|
package/dist/omnichannel.js
CHANGED
|
@@ -171,6 +171,7 @@ function createOmnichannelProvider(config) {
|
|
|
171
171
|
function load() {
|
|
172
172
|
if (chat) return chat;
|
|
173
173
|
const pending = (async () => {
|
|
174
|
+
globalThis.global ??= globalThis;
|
|
174
175
|
const module = await import("@microsoft/omnichannel-chat-sdk").catch((error) => {
|
|
175
176
|
throw new Error("Omnichannel needs @microsoft/omnichannel-chat-sdk installed.", { cause: error });
|
|
176
177
|
});
|
package/dist/styles.css
CHANGED
|
@@ -58,6 +58,9 @@
|
|
|
58
58
|
--vv-ai-chat-window-header-hover: color-mix(in srgb, var(--vv-ai-chat-window-header-text) 15%, transparent);
|
|
59
59
|
/* An image in the circle beside each of the assistant's bubbles in the window. */
|
|
60
60
|
--vv-ai-chat-assistant-avatar: none;
|
|
61
|
+
/* Buttons and fields in the window's thread, the host's own included. */
|
|
62
|
+
--vv-ai-chat-window-control-height: 2.25rem;
|
|
63
|
+
--vv-ai-chat-window-control-font-size: 0.875rem;
|
|
61
64
|
--vv-ai-launcher-shadow: 0 0.375rem 1.25rem rgb(16 40 80 / 0.25);
|
|
62
65
|
--vv-ai-launcher-teaser-shadow: 0 0.5rem 1.5rem rgb(16 40 80 / 0.15);
|
|
63
66
|
--vv-ai-menu-shadow: 0 0.5rem 1.5rem rgb(16 40 80 / 0.18);
|
|
@@ -649,8 +652,10 @@ body:has(.vv-ai-chat:not(.vv-ai-chat--window)) {
|
|
|
649
652
|
animation-delay: 0.3s;
|
|
650
653
|
}
|
|
651
654
|
|
|
652
|
-
/* The typing indicator's text is for screen readers; the dots show it.
|
|
653
|
-
.
|
|
655
|
+
/* The typing indicator's text is for screen readers; the dots show it. So is the announcer, which
|
|
656
|
+
reads out each finished answer. */
|
|
657
|
+
.vv-ai-chat__typing-label,
|
|
658
|
+
.vv-ai-chat__announcer {
|
|
654
659
|
position: absolute;
|
|
655
660
|
width: 1px;
|
|
656
661
|
height: 1px;
|
|
@@ -1385,6 +1390,42 @@ body:has(.vv-ai-chat:not(.vv-ai-chat--window)) {
|
|
|
1385
1390
|
line-height: 1.4;
|
|
1386
1391
|
}
|
|
1387
1392
|
|
|
1393
|
+
/*
|
|
1394
|
+
* Buttons and fields in the window's thread stay small. A component the host embeds there brings
|
|
1395
|
+
* the site's buttons and fields, sized for a page, so the common button classes are caught here as
|
|
1396
|
+
* well as the package's own. The composer below the thread keeps its own.
|
|
1397
|
+
*/
|
|
1398
|
+
.vv-ai-chat--window .vv-ai-chat__thread-inner :is(
|
|
1399
|
+
.button, [class^="button-"], [class*=" button-"],
|
|
1400
|
+
.btn, [class^="btn-"], [class*=" btn-"],
|
|
1401
|
+
.bttn, [class^="bttn-"], [class*=" bttn-"],
|
|
1402
|
+
.vv-ai-actions__action,
|
|
1403
|
+
.vv-ai-survey__submit
|
|
1404
|
+
) {
|
|
1405
|
+
min-height: var(--vv-ai-chat-window-control-height);
|
|
1406
|
+
padding-block: 0.375rem;
|
|
1407
|
+
padding-inline: 1rem;
|
|
1408
|
+
font-size: var(--vv-ai-chat-window-control-font-size);
|
|
1409
|
+
line-height: 1.5;
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
.vv-ai-chat--window .vv-ai-chat__thread-inner :is(
|
|
1413
|
+
input:not([type="checkbox"], [type="radio"], [type="range"], [type="hidden"], [type="file"]),
|
|
1414
|
+
select
|
|
1415
|
+
) {
|
|
1416
|
+
box-sizing: border-box;
|
|
1417
|
+
height: var(--vv-ai-chat-window-control-height);
|
|
1418
|
+
padding-block: 0.5rem;
|
|
1419
|
+
font-size: var(--vv-ai-chat-window-control-font-size);
|
|
1420
|
+
line-height: 1.2;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
.vv-ai-chat--window .vv-ai-chat__thread-inner textarea {
|
|
1424
|
+
padding-block: 0.5rem;
|
|
1425
|
+
font-size: var(--vv-ai-chat-window-control-font-size);
|
|
1426
|
+
line-height: 1.4;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1388
1429
|
.vv-ai-chat--window .vv-ai-chat__composer-inner {
|
|
1389
1430
|
max-width: none;
|
|
1390
1431
|
padding: 0.75rem 0.75rem 0.5rem;
|
package/dist/survey.js
CHANGED
|
@@ -1,232 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
const MAX_MARKDOWN_CHARACTERS = 8e3;
|
|
4
|
-
function isWordCharacter(character) {
|
|
5
|
-
return character !== void 0 && /[\p{L}\p{N}]/u.test(character);
|
|
6
|
-
}
|
|
7
|
-
function delimitedToken(source, start, marker, type) {
|
|
8
|
-
if (!source.startsWith(marker, start)) return null;
|
|
9
|
-
const contentStart = start + marker.length;
|
|
10
|
-
const closing = source.indexOf(marker, contentStart);
|
|
11
|
-
if (closing === -1 || closing === contentStart) return null;
|
|
12
|
-
return {
|
|
13
|
-
type,
|
|
14
|
-
content: source.slice(contentStart, closing),
|
|
15
|
-
end: closing + marker.length
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
function linkToken(source, start) {
|
|
19
|
-
if (source[start] !== "[") return null;
|
|
20
|
-
let labelEnd = start + 1;
|
|
21
|
-
while (labelEnd < source.length) {
|
|
22
|
-
if (source[labelEnd] === "[") return null;
|
|
23
|
-
if (source[labelEnd] === "]" && source[labelEnd + 1] === "(") break;
|
|
24
|
-
labelEnd += 1;
|
|
25
|
-
}
|
|
26
|
-
if (labelEnd === source.length) return {
|
|
27
|
-
type: "text",
|
|
28
|
-
content: source.slice(start),
|
|
29
|
-
end: source.length
|
|
30
|
-
};
|
|
31
|
-
if (labelEnd === start + 1) return null;
|
|
32
|
-
let depth = 1;
|
|
33
|
-
let end = labelEnd + 2;
|
|
34
|
-
while (end < source.length && depth > 0) {
|
|
35
|
-
if (source[end] === "(") depth += 1;
|
|
36
|
-
else if (source[end] === ")") depth -= 1;
|
|
37
|
-
end += 1;
|
|
38
|
-
}
|
|
39
|
-
if (depth !== 0) return {
|
|
40
|
-
type: "text",
|
|
41
|
-
content: source.slice(start),
|
|
42
|
-
end: source.length
|
|
43
|
-
};
|
|
44
|
-
const url = source.slice(labelEnd + 2, end - 1);
|
|
45
|
-
if (!isSafeLinkUrl(url)) return {
|
|
46
|
-
type: "text",
|
|
47
|
-
content: source.slice(start, end),
|
|
48
|
-
end
|
|
49
|
-
};
|
|
50
|
-
return {
|
|
51
|
-
type: "link",
|
|
52
|
-
label: source.slice(start + 1, labelEnd),
|
|
53
|
-
url,
|
|
54
|
-
end
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
function inlineToken(source, start) {
|
|
58
|
-
const link = linkToken(source, start);
|
|
59
|
-
if (link) return link;
|
|
60
|
-
const code = delimitedToken(source, start, "`", "code");
|
|
61
|
-
if (code) return code;
|
|
62
|
-
const bold = delimitedToken(source, start, "**", "bold") ?? delimitedToken(source, start, "__", "bold");
|
|
63
|
-
if (bold) return bold;
|
|
64
|
-
const marker = source[start];
|
|
65
|
-
if ((marker === "*" || marker === "_") && !isWordCharacter(source[start - 1])) return delimitedToken(source, start, marker, "italic");
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
function inlineStructuralItemCount(source) {
|
|
69
|
-
let count = 0;
|
|
70
|
-
let index = 0;
|
|
71
|
-
while (index < source.length) {
|
|
72
|
-
const token = inlineToken(source, index);
|
|
73
|
-
if (!token) {
|
|
74
|
-
index += 1;
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
switch (token.type) {
|
|
78
|
-
case "bold":
|
|
79
|
-
case "italic":
|
|
80
|
-
count += 1 + inlineStructuralItemCount(token.content);
|
|
81
|
-
break;
|
|
82
|
-
case "code":
|
|
83
|
-
case "link": count += 1;
|
|
84
|
-
}
|
|
85
|
-
index = token.end;
|
|
86
|
-
}
|
|
87
|
-
return count;
|
|
88
|
-
}
|
|
89
|
-
function appendInlineMarkdown(document, parent, source) {
|
|
90
|
-
let textStart = 0;
|
|
91
|
-
let index = 0;
|
|
92
|
-
while (index < source.length) {
|
|
93
|
-
const token = inlineToken(source, index);
|
|
94
|
-
if (!token) {
|
|
95
|
-
index += 1;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (textStart < index) parent.append(document.createTextNode(source.slice(textStart, index)));
|
|
99
|
-
switch (token.type) {
|
|
100
|
-
case "code": {
|
|
101
|
-
const code = document.createElement("code");
|
|
102
|
-
code.append(document.createTextNode(token.content));
|
|
103
|
-
parent.append(code);
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
case "bold": {
|
|
107
|
-
const strong = document.createElement("strong");
|
|
108
|
-
appendInlineMarkdown(document, strong, token.content);
|
|
109
|
-
parent.append(strong);
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
case "italic": {
|
|
113
|
-
const emphasis = document.createElement("em");
|
|
114
|
-
appendInlineMarkdown(document, emphasis, token.content);
|
|
115
|
-
parent.append(emphasis);
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
case "link": {
|
|
119
|
-
const link = createSafeLink(document, token.label, token.url);
|
|
120
|
-
if (link) parent.append(link);
|
|
121
|
-
else parent.append(document.createTextNode(source.slice(index, token.end)));
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
124
|
-
case "text": parent.append(document.createTextNode(token.content));
|
|
125
|
-
}
|
|
126
|
-
index = token.end;
|
|
127
|
-
textStart = index;
|
|
128
|
-
}
|
|
129
|
-
if (textStart < source.length) parent.append(document.createTextNode(source.slice(textStart)));
|
|
130
|
-
}
|
|
131
|
-
function unorderedListItem(line) {
|
|
132
|
-
return /^\s*[-+*]\s+(.+)$/.exec(line)?.[1] ?? null;
|
|
133
|
-
}
|
|
134
|
-
function orderedListItem(line) {
|
|
135
|
-
return /^\s*\d+[.)]\s+(.+)$/.exec(line)?.[1] ?? null;
|
|
136
|
-
}
|
|
137
|
-
function heading(line) {
|
|
138
|
-
return /^\s{0,3}#{1,6}\s+(.+?)\s*#*\s*$/.exec(line)?.[1] ?? null;
|
|
139
|
-
}
|
|
140
|
-
function countMarkdownStructuralItems(source) {
|
|
141
|
-
const lines = source.slice(0, MAX_MARKDOWN_CHARACTERS).split(/\r\n?|\n/);
|
|
142
|
-
let count = 0;
|
|
143
|
-
let index = 0;
|
|
144
|
-
while (index < lines.length) {
|
|
145
|
-
const line = lines[index];
|
|
146
|
-
if (line.trim().length === 0) {
|
|
147
|
-
index += 1;
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
const headingText = heading(line);
|
|
151
|
-
if (headingText !== null) {
|
|
152
|
-
count += 2 + inlineStructuralItemCount(headingText);
|
|
153
|
-
index += 1;
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
const unorderedItem = unorderedListItem(line);
|
|
157
|
-
const orderedItem = orderedListItem(line);
|
|
158
|
-
if (unorderedItem !== null || orderedItem !== null) {
|
|
159
|
-
const itemForLine = unorderedItem === null ? orderedListItem : unorderedListItem;
|
|
160
|
-
count += 1;
|
|
161
|
-
while (index < lines.length) {
|
|
162
|
-
const item = itemForLine(lines[index]);
|
|
163
|
-
if (item === null) break;
|
|
164
|
-
count += 1 + inlineStructuralItemCount(item);
|
|
165
|
-
index += 1;
|
|
166
|
-
}
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
const paragraphLines = [];
|
|
170
|
-
while (index < lines.length) {
|
|
171
|
-
const paragraphLine = lines[index];
|
|
172
|
-
if (paragraphLine.trim().length === 0 || heading(paragraphLine) !== null || unorderedListItem(paragraphLine) !== null || orderedListItem(paragraphLine) !== null) break;
|
|
173
|
-
paragraphLines.push(paragraphLine);
|
|
174
|
-
index += 1;
|
|
175
|
-
}
|
|
176
|
-
count += 1 + inlineStructuralItemCount(paragraphLines.join(" "));
|
|
177
|
-
}
|
|
178
|
-
return count;
|
|
179
|
-
}
|
|
180
|
-
function renderMarkdown(document, source) {
|
|
181
|
-
const fragment = document.createDocumentFragment();
|
|
182
|
-
const lines = source.slice(0, MAX_MARKDOWN_CHARACTERS).split(/\r\n?|\n/);
|
|
183
|
-
let index = 0;
|
|
184
|
-
while (index < lines.length) {
|
|
185
|
-
const line = lines[index];
|
|
186
|
-
if (line.trim().length === 0) {
|
|
187
|
-
index += 1;
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
const headingText = heading(line);
|
|
191
|
-
if (headingText !== null) {
|
|
192
|
-
const paragraph = document.createElement("p");
|
|
193
|
-
const strong = document.createElement("strong");
|
|
194
|
-
appendInlineMarkdown(document, strong, headingText);
|
|
195
|
-
paragraph.append(strong);
|
|
196
|
-
fragment.append(paragraph);
|
|
197
|
-
index += 1;
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
const unorderedItem = unorderedListItem(line);
|
|
201
|
-
const orderedItem = orderedListItem(line);
|
|
202
|
-
if (unorderedItem !== null || orderedItem !== null) {
|
|
203
|
-
const list = document.createElement(unorderedItem === null ? "ol" : "ul");
|
|
204
|
-
const itemForLine = unorderedItem === null ? orderedListItem : unorderedListItem;
|
|
205
|
-
while (index < lines.length) {
|
|
206
|
-
const item = itemForLine(lines[index]);
|
|
207
|
-
if (item === null) break;
|
|
208
|
-
const listItem = document.createElement("li");
|
|
209
|
-
appendInlineMarkdown(document, listItem, item);
|
|
210
|
-
list.append(listItem);
|
|
211
|
-
index += 1;
|
|
212
|
-
}
|
|
213
|
-
fragment.append(list);
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
const paragraphLines = [];
|
|
217
|
-
while (index < lines.length) {
|
|
218
|
-
const paragraphLine = lines[index];
|
|
219
|
-
if (paragraphLine.trim().length === 0 || heading(paragraphLine) !== null || unorderedListItem(paragraphLine) !== null || orderedListItem(paragraphLine) !== null) break;
|
|
220
|
-
paragraphLines.push(paragraphLine);
|
|
221
|
-
index += 1;
|
|
222
|
-
}
|
|
223
|
-
const paragraph = document.createElement("p");
|
|
224
|
-
appendInlineMarkdown(document, paragraph, paragraphLines.join(" "));
|
|
225
|
-
fragment.append(paragraph);
|
|
226
|
-
}
|
|
227
|
-
return fragment;
|
|
228
|
-
}
|
|
229
|
-
//#endregion
|
|
1
|
+
import { t as createPart } from "./dom.js";
|
|
2
|
+
import { r as renderMarkdown } from "./markdown.js";
|
|
230
3
|
//#region src/live-chat/survey.ts
|
|
231
4
|
/** Every element of the survey form carries a class under this block. */
|
|
232
5
|
const SURVEY_CLASS = "vv-ai-survey";
|
|
@@ -389,4 +162,4 @@ function renderSurveyForm(document, fields, options) {
|
|
|
389
162
|
};
|
|
390
163
|
}
|
|
391
164
|
//#endregion
|
|
392
|
-
export {
|
|
165
|
+
export { renderSurveyForm as n, SURVEY_CLASS as t };
|