@svadmin/ai-elements 0.7.4 → 0.9.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 +31 -4
- package/THIRD_PARTY_NOTICES.md +67 -6
- package/dist/ai-workspace-contract.d.ts +14 -0
- package/dist/ai-workspace-contract.js +14 -0
- package/dist/ai.css +2 -5
- package/dist/ai.theme.css +1 -0
- package/dist/class-properties.generated.d.ts +1 -0
- package/dist/class-properties.generated.js +2 -0
- package/dist/classnames.d.ts +8 -0
- package/dist/classnames.js +26 -0
- package/dist/components/Response.svelte +2 -2
- package/dist/components/Response.svelte.d.ts +1 -1
- package/dist/components/code/index.d.ts +9 -18
- package/dist/components/code/index.js +8 -11
- package/dist/components/code-block/highlight.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/utilities.css +3172 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +36 -11
- package/vendor/streamdown/AnimatedText.svelte +93 -0
- package/vendor/streamdown/AnimatedText.svelte.d.ts +6 -0
- package/vendor/streamdown/Block.svelte +96 -0
- package/vendor/streamdown/Block.svelte.d.ts +11 -0
- package/vendor/streamdown/Elements/Alert.svelte +69 -0
- package/vendor/streamdown/Elements/Alert.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/Citation.svelte +260 -0
- package/vendor/streamdown/Elements/Citation.svelte.d.ts +7 -0
- package/vendor/streamdown/Elements/Code.svelte +226 -0
- package/vendor/streamdown/Elements/Code.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/Element.svelte +574 -0
- package/vendor/streamdown/Elements/Element.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/FootnoteRef.svelte +30 -0
- package/vendor/streamdown/Elements/FootnoteRef.svelte.d.ts +7 -0
- package/vendor/streamdown/Elements/Footnotes.svelte +54 -0
- package/vendor/streamdown/Elements/Footnotes.svelte.d.ts +7 -0
- package/vendor/streamdown/Elements/Image.svelte +200 -0
- package/vendor/streamdown/Elements/Image.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/Link.svelte +220 -0
- package/vendor/streamdown/Elements/Link.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/LinkSafetyModal.svelte +121 -0
- package/vendor/streamdown/Elements/LinkSafetyModal.svelte.d.ts +4 -0
- package/vendor/streamdown/Elements/Math.svelte +100 -0
- package/vendor/streamdown/Elements/Math.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/Mermaid.svelte +455 -0
- package/vendor/streamdown/Elements/Mermaid.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/MermaidDownload.svelte +145 -0
- package/vendor/streamdown/Elements/MermaidDownload.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/Slot.svelte +23 -0
- package/vendor/streamdown/Elements/Slot.svelte.d.ts +10 -0
- package/vendor/streamdown/Elements/Table.svelte +195 -0
- package/vendor/streamdown/Elements/Table.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/TableDownload.svelte +221 -0
- package/vendor/streamdown/Elements/TableDownload.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/fallbacks/CodeFallback.svelte +41 -0
- package/vendor/streamdown/Elements/fallbacks/CodeFallback.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/fallbacks/MathFallback.svelte +35 -0
- package/vendor/streamdown/Elements/fallbacks/MathFallback.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/fallbacks/MermaidFallback.svelte +36 -0
- package/vendor/streamdown/Elements/fallbacks/MermaidFallback.svelte.d.ts +9 -0
- package/vendor/streamdown/Elements/fallbacks/index.d.ts +3 -0
- package/vendor/streamdown/Elements/fallbacks/index.js +3 -0
- package/vendor/streamdown/Elements/icons.d.ts +31 -0
- package/vendor/streamdown/Elements/icons.js +200 -0
- package/vendor/streamdown/Elements/index.d.ts +8 -0
- package/vendor/streamdown/Elements/index.js +8 -0
- package/vendor/streamdown/Elements/popover.svelte.d.ts +8 -0
- package/vendor/streamdown/Elements/popover.svelte.js +45 -0
- package/vendor/streamdown/Elements/stepperState.svelte.d.ts +36 -0
- package/vendor/streamdown/Elements/stepperState.svelte.js +92 -0
- package/vendor/streamdown/IconProvider.svelte +11 -0
- package/vendor/streamdown/IconProvider.svelte.d.ts +30 -0
- package/vendor/streamdown/LICENSE +202 -0
- package/vendor/streamdown/PluginProvider.svelte +11 -0
- package/vendor/streamdown/PluginProvider.svelte.d.ts +30 -0
- package/vendor/streamdown/README.svadmin.md +5 -0
- package/vendor/streamdown/Streamdown.svelte +447 -0
- package/vendor/streamdown/Streamdown.svelte.d.ts +25 -0
- package/vendor/streamdown/block/BlockTokens.svelte +45 -0
- package/vendor/streamdown/block/BlockTokens.svelte.d.ts +9 -0
- package/vendor/streamdown/block/html.d.ts +14 -0
- package/vendor/streamdown/block/html.js +27 -0
- package/vendor/streamdown/block/text.d.ts +10 -0
- package/vendor/streamdown/block/text.js +31 -0
- package/vendor/streamdown/context-key.d.ts +1 -0
- package/vendor/streamdown/context-key.js +1 -0
- package/vendor/streamdown/context.svelte.d.ts +24 -0
- package/vendor/streamdown/context.svelte.js +75 -0
- package/vendor/streamdown/contracts/plugins.d.ts +26 -0
- package/vendor/streamdown/contracts/plugins.js +1 -0
- package/vendor/streamdown/contracts/streamdown.d.ts +374 -0
- package/vendor/streamdown/contracts/streamdown.js +29 -0
- package/vendor/streamdown/detect-direction.d.ts +1 -0
- package/vendor/streamdown/detect-direction.js +1 -0
- package/vendor/streamdown/icon-context.d.ts +36 -0
- package/vendor/streamdown/icon-context.js +52 -0
- package/vendor/streamdown/incomplete-code.d.ts +6 -0
- package/vendor/streamdown/incomplete-code.js +23 -0
- package/vendor/streamdown/index.d.ts +22 -0
- package/vendor/streamdown/index.js +20 -0
- package/vendor/streamdown/markdown-parse-cache.d.ts +37 -0
- package/vendor/streamdown/markdown-parse-cache.js +109 -0
- package/vendor/streamdown/markdown.d.ts +21 -0
- package/vendor/streamdown/markdown.js +214 -0
- package/vendor/streamdown/marked/index.d.ts +44 -0
- package/vendor/streamdown/marked/index.js +228 -0
- package/vendor/streamdown/marked/marked-alert.d.ts +13 -0
- package/vendor/streamdown/marked/marked-alert.js +47 -0
- package/vendor/streamdown/marked/marked-align.d.ts +10 -0
- package/vendor/streamdown/marked/marked-align.js +45 -0
- package/vendor/streamdown/marked/marked-br.d.ts +6 -0
- package/vendor/streamdown/marked/marked-br.js +15 -0
- package/vendor/streamdown/marked/marked-citations.d.ts +8 -0
- package/vendor/streamdown/marked/marked-citations.js +53 -0
- package/vendor/streamdown/marked/marked-cjk.d.ts +2 -0
- package/vendor/streamdown/marked/marked-cjk.js +165 -0
- package/vendor/streamdown/marked/marked-dl.d.ts +23 -0
- package/vendor/streamdown/marked/marked-dl.js +44 -0
- package/vendor/streamdown/marked/marked-footnotes.d.ts +26 -0
- package/vendor/streamdown/marked/marked-footnotes.js +101 -0
- package/vendor/streamdown/marked/marked-hr.d.ts +6 -0
- package/vendor/streamdown/marked/marked-hr.js +18 -0
- package/vendor/streamdown/marked/marked-list.d.ts +31 -0
- package/vendor/streamdown/marked/marked-list.js +330 -0
- package/vendor/streamdown/marked/marked-math.d.ts +13 -0
- package/vendor/streamdown/marked/marked-math.js +129 -0
- package/vendor/streamdown/marked/marked-mdx.d.ts +12 -0
- package/vendor/streamdown/marked/marked-mdx.js +124 -0
- package/vendor/streamdown/marked/marked-subsup.d.ts +22 -0
- package/vendor/streamdown/marked/marked-subsup.js +40 -0
- package/vendor/streamdown/marked/marked-table.d.ts +80 -0
- package/vendor/streamdown/marked/marked-table.js +527 -0
- package/vendor/streamdown/plugin-context.d.ts +19 -0
- package/vendor/streamdown/plugin-context.js +40 -0
- package/vendor/streamdown/plugins/cjk-shared.d.ts +1 -0
- package/vendor/streamdown/plugins/cjk-shared.js +1 -0
- package/vendor/streamdown/plugins.d.ts +19 -0
- package/vendor/streamdown/plugins.js +107 -0
- package/vendor/streamdown/provenance.json +229 -0
- package/vendor/streamdown/remend.d.ts +2 -0
- package/vendor/streamdown/remend.js +1 -0
- package/vendor/streamdown/security/html.d.ts +19 -0
- package/vendor/streamdown/security/html.js +329 -0
- package/vendor/streamdown/security/preprocess-custom-tags.d.ts +1 -0
- package/vendor/streamdown/security/preprocess-custom-tags.js +20 -0
- package/vendor/streamdown/security/preprocess-literal-tag-content.d.ts +1 -0
- package/vendor/streamdown/security/preprocess-literal-tag-content.js +16 -0
- package/vendor/streamdown/security/types.d.ts +1 -0
- package/vendor/streamdown/security/types.js +1 -0
- package/vendor/streamdown/site/SiteHeader.svelte +94 -0
- package/vendor/streamdown/site/SiteHeader.svelte.d.ts +6 -0
- package/vendor/streamdown/site/home/HomeDemo.svelte +122 -0
- package/vendor/streamdown/site/home/HomeDemo.svelte.d.ts +3 -0
- package/vendor/streamdown/site/home/HomePage.svelte +293 -0
- package/vendor/streamdown/site/home/HomePage.svelte.d.ts +3 -0
- package/vendor/streamdown/site/home/content.d.ts +69 -0
- package/vendor/streamdown/site/home/content.js +144 -0
- package/vendor/streamdown/site/metadata.d.ts +4 -0
- package/vendor/streamdown/site/metadata.js +4 -0
- package/vendor/streamdown/site/navigation.d.ts +6 -0
- package/vendor/streamdown/site/navigation.js +22 -0
- package/vendor/streamdown/streamdown/config.d.ts +41 -0
- package/vendor/streamdown/streamdown/config.js +125 -0
- package/vendor/streamdown/streamdown/context.d.ts +12 -0
- package/vendor/streamdown/streamdown/context.js +2 -0
- package/vendor/streamdown/streamdown/incomplete-markdown.d.ts +5 -0
- package/vendor/streamdown/streamdown/incomplete-markdown.js +79 -0
- package/vendor/streamdown/streamdown/pipeline.d.ts +95 -0
- package/vendor/streamdown/streamdown/pipeline.js +138 -0
- package/vendor/streamdown/streamdown/runtime.d.ts +59 -0
- package/vendor/streamdown/streamdown/runtime.js +112 -0
- package/vendor/streamdown/streaming.d.ts +5 -0
- package/vendor/streamdown/streaming.js +5 -0
- package/vendor/streamdown/theme.d.ts +486 -0
- package/vendor/streamdown/theme.js +370 -0
- package/vendor/streamdown/translations.d.ts +43 -0
- package/vendor/streamdown/translations.js +54 -0
- package/vendor/streamdown/url-policy.d.ts +1 -0
- package/vendor/streamdown/url-policy.js +1 -0
- package/vendor/streamdown/utils/bind.d.ts +8 -0
- package/vendor/streamdown/utils/bind.js +26 -0
- package/vendor/streamdown/utils/bundledLanguages.d.ts +1 -0
- package/vendor/streamdown/utils/bundledLanguages.js +1 -0
- package/vendor/streamdown/utils/code-block.d.ts +8 -0
- package/vendor/streamdown/utils/code-block.js +38 -0
- package/vendor/streamdown/utils/copy.svelte.d.ts +7 -0
- package/vendor/streamdown/utils/copy.svelte.js +38 -0
- package/vendor/streamdown/utils/darkMode.svelte.d.ts +3 -0
- package/vendor/streamdown/utils/darkMode.svelte.js +49 -0
- package/vendor/streamdown/utils/detectDirection.d.ts +1 -0
- package/vendor/streamdown/utils/detectDirection.js +19 -0
- package/vendor/streamdown/utils/get.d.ts +1 -0
- package/vendor/streamdown/utils/get.js +25 -0
- package/vendor/streamdown/utils/hightlighter.svelte.d.ts +30 -0
- package/vendor/streamdown/utils/hightlighter.svelte.js +524 -0
- package/vendor/streamdown/utils/index.d.ts +3 -0
- package/vendor/streamdown/utils/index.js +3 -0
- package/vendor/streamdown/utils/mermaid.d.ts +13 -0
- package/vendor/streamdown/utils/mermaid.js +49 -0
- package/vendor/streamdown/utils/panzoom.svelte.d.ts +35 -0
- package/vendor/streamdown/utils/panzoom.svelte.js +556 -0
- package/vendor/streamdown/utils/parse-incomplete-markdown.d.ts +2 -0
- package/vendor/streamdown/utils/parse-incomplete-markdown.js +2 -0
- package/vendor/streamdown/utils/runtime-env.d.ts +1 -0
- package/vendor/streamdown/utils/runtime-env.js +13 -0
- package/vendor/streamdown/utils/save.d.ts +1 -0
- package/vendor/streamdown/utils/save.js +14 -0
- package/vendor/streamdown/utils/scroll-lock.d.ts +2 -0
- package/vendor/streamdown/utils/scroll-lock.js +35 -0
- package/vendor/streamdown/utils/table.d.ts +8 -0
- package/vendor/streamdown/utils/table.js +176 -0
- package/vendor/streamdown/utils/url.d.ts +22 -0
- package/vendor/streamdown/utils/url.js +98 -0
- package/vendor/streamdown/utils/useClickOutside.svelte.d.ts +8 -0
- package/vendor/streamdown/utils/useClickOutside.svelte.js +41 -0
- package/vendor/streamdown/utils/useKeyDown.svelte.d.ts +5 -0
- package/vendor/streamdown/utils/useKeyDown.svelte.js +23 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
const escapeCSV = (value) => {
|
|
2
|
+
let needsEscaping = false;
|
|
3
|
+
let hasQuote = false;
|
|
4
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
5
|
+
const char = value[index];
|
|
6
|
+
if (char === '"') {
|
|
7
|
+
needsEscaping = true;
|
|
8
|
+
hasQuote = true;
|
|
9
|
+
break;
|
|
10
|
+
}
|
|
11
|
+
if (char === ',' || char === '\n') {
|
|
12
|
+
needsEscaping = true;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (!needsEscaping) {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
if (hasQuote) {
|
|
19
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
20
|
+
}
|
|
21
|
+
return `"${value}"`;
|
|
22
|
+
};
|
|
23
|
+
const escapeTSV = (value) => {
|
|
24
|
+
let needsEscaping = false;
|
|
25
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
26
|
+
const char = value[index];
|
|
27
|
+
if (char === '\t' || char === '\n' || char === '\r') {
|
|
28
|
+
needsEscaping = true;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!needsEscaping) {
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
const parts = new Array(value.length);
|
|
36
|
+
let partIndex = 0;
|
|
37
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
38
|
+
const char = value[index];
|
|
39
|
+
if (char === '\t') {
|
|
40
|
+
parts[partIndex] = '\\t';
|
|
41
|
+
}
|
|
42
|
+
else if (char === '\n') {
|
|
43
|
+
parts[partIndex] = '\\n';
|
|
44
|
+
}
|
|
45
|
+
else if (char === '\r') {
|
|
46
|
+
parts[partIndex] = '\\r';
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
parts[partIndex] = char;
|
|
50
|
+
}
|
|
51
|
+
partIndex += 1;
|
|
52
|
+
}
|
|
53
|
+
return parts.join('');
|
|
54
|
+
};
|
|
55
|
+
const escapeMarkdownTableCell = (cell) => {
|
|
56
|
+
let needsEscaping = false;
|
|
57
|
+
for (let index = 0; index < cell.length; index += 1) {
|
|
58
|
+
const char = cell[index];
|
|
59
|
+
if (char === '\\' || char === '|') {
|
|
60
|
+
needsEscaping = true;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!needsEscaping) {
|
|
65
|
+
return cell;
|
|
66
|
+
}
|
|
67
|
+
const parts = new Array(cell.length);
|
|
68
|
+
let partIndex = 0;
|
|
69
|
+
for (let index = 0; index < cell.length; index += 1) {
|
|
70
|
+
const char = cell[index];
|
|
71
|
+
if (char === '\\') {
|
|
72
|
+
parts[partIndex] = '\\\\';
|
|
73
|
+
}
|
|
74
|
+
else if (char === '|') {
|
|
75
|
+
parts[partIndex] = '\\|';
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
parts[partIndex] = char;
|
|
79
|
+
}
|
|
80
|
+
partIndex += 1;
|
|
81
|
+
}
|
|
82
|
+
return parts.join('');
|
|
83
|
+
};
|
|
84
|
+
const formatDelimitedRow = (row, delimiter, escapeCell) => {
|
|
85
|
+
if (row.length === 0) {
|
|
86
|
+
return '';
|
|
87
|
+
}
|
|
88
|
+
const formattedCells = new Array(row.length);
|
|
89
|
+
for (let index = 0; index < row.length; index += 1) {
|
|
90
|
+
formattedCells[index] = escapeCell(row[index] ?? '');
|
|
91
|
+
}
|
|
92
|
+
return formattedCells.join(delimiter);
|
|
93
|
+
};
|
|
94
|
+
export const extractTableDataFromElement = (tableElement) => {
|
|
95
|
+
const headers = [];
|
|
96
|
+
const rows = [];
|
|
97
|
+
const headerCells = tableElement.querySelectorAll('thead th');
|
|
98
|
+
for (const cell of headerCells) {
|
|
99
|
+
headers.push(cell.textContent?.trim() || '');
|
|
100
|
+
}
|
|
101
|
+
const bodyRows = tableElement.querySelectorAll('tbody tr');
|
|
102
|
+
for (const row of bodyRows) {
|
|
103
|
+
const rowData = [];
|
|
104
|
+
const cells = row.querySelectorAll('td');
|
|
105
|
+
for (const cell of cells) {
|
|
106
|
+
rowData.push(cell.textContent?.trim() || '');
|
|
107
|
+
}
|
|
108
|
+
rows.push(rowData);
|
|
109
|
+
}
|
|
110
|
+
return { headers, rows };
|
|
111
|
+
};
|
|
112
|
+
export const tableDataToCSV = ({ headers, rows }) => {
|
|
113
|
+
const totalRows = headers.length > 0 ? rows.length + 1 : rows.length;
|
|
114
|
+
const csvRows = new Array(totalRows);
|
|
115
|
+
let rowIndex = 0;
|
|
116
|
+
if (headers.length > 0) {
|
|
117
|
+
csvRows[rowIndex] = formatDelimitedRow(headers, ',', escapeCSV);
|
|
118
|
+
rowIndex += 1;
|
|
119
|
+
}
|
|
120
|
+
for (const row of rows) {
|
|
121
|
+
csvRows[rowIndex] = formatDelimitedRow(row, ',', escapeCSV);
|
|
122
|
+
rowIndex += 1;
|
|
123
|
+
}
|
|
124
|
+
return csvRows.join('\n');
|
|
125
|
+
};
|
|
126
|
+
export const tableDataToTSV = ({ headers, rows }) => {
|
|
127
|
+
const totalRows = headers.length > 0 ? rows.length + 1 : rows.length;
|
|
128
|
+
const tsvRows = new Array(totalRows);
|
|
129
|
+
let rowIndex = 0;
|
|
130
|
+
if (headers.length > 0) {
|
|
131
|
+
tsvRows[rowIndex] = formatDelimitedRow(headers, '\t', escapeTSV);
|
|
132
|
+
rowIndex += 1;
|
|
133
|
+
}
|
|
134
|
+
for (const row of rows) {
|
|
135
|
+
tsvRows[rowIndex] = formatDelimitedRow(row, '\t', escapeTSV);
|
|
136
|
+
rowIndex += 1;
|
|
137
|
+
}
|
|
138
|
+
return tsvRows.join('\n');
|
|
139
|
+
};
|
|
140
|
+
export const tableDataToMarkdown = ({ headers, rows }) => {
|
|
141
|
+
if (headers.length === 0) {
|
|
142
|
+
return '';
|
|
143
|
+
}
|
|
144
|
+
const markdownRows = new Array(rows.length + 2);
|
|
145
|
+
let rowIndex = 0;
|
|
146
|
+
const escapedHeaders = new Array(headers.length);
|
|
147
|
+
for (let index = 0; index < headers.length; index += 1) {
|
|
148
|
+
escapedHeaders[index] = escapeMarkdownTableCell(headers[index]);
|
|
149
|
+
}
|
|
150
|
+
markdownRows[rowIndex] = `| ${escapedHeaders.join(' | ')} |`;
|
|
151
|
+
rowIndex += 1;
|
|
152
|
+
const separatorParts = new Array(headers.length);
|
|
153
|
+
for (let i = 0; i < headers.length; i += 1) {
|
|
154
|
+
separatorParts[i] = '---';
|
|
155
|
+
}
|
|
156
|
+
markdownRows[rowIndex] = `| ${separatorParts.join(' | ')} |`;
|
|
157
|
+
rowIndex += 1;
|
|
158
|
+
for (const row of rows) {
|
|
159
|
+
if (row.length < headers.length) {
|
|
160
|
+
const paddedRow = new Array(headers.length);
|
|
161
|
+
for (let i = 0; i < headers.length; i += 1) {
|
|
162
|
+
paddedRow[i] = i < row.length ? escapeMarkdownTableCell(row[i]) : '';
|
|
163
|
+
}
|
|
164
|
+
markdownRows[rowIndex] = `| ${paddedRow.join(' | ')} |`;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
const escapedRow = new Array(row.length);
|
|
168
|
+
for (let i = 0; i < row.length; i += 1) {
|
|
169
|
+
escapedRow[i] = escapeMarkdownTableCell(row[i]);
|
|
170
|
+
}
|
|
171
|
+
markdownRows[rowIndex] = `| ${escapedRow.join(' | ')} |`;
|
|
172
|
+
}
|
|
173
|
+
rowIndex += 1;
|
|
174
|
+
}
|
|
175
|
+
return markdownRows.join('\n');
|
|
176
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const parseUrl: (url: unknown, defaultOrigin?: string) => URL | null;
|
|
2
|
+
export declare const isPathRelativeUrl: (url: unknown) => boolean;
|
|
3
|
+
export type UrlPolicyKind = 'image' | 'link';
|
|
4
|
+
export type TransformUrlOptions = {
|
|
5
|
+
kind?: UrlPolicyKind;
|
|
6
|
+
};
|
|
7
|
+
type WildcardAllowedUrlPrefix = {
|
|
8
|
+
type: 'wildcard';
|
|
9
|
+
};
|
|
10
|
+
type ProtocolAllowedUrlPrefix = {
|
|
11
|
+
type: 'protocol';
|
|
12
|
+
protocol: string;
|
|
13
|
+
};
|
|
14
|
+
type AbsoluteAllowedUrlPrefix = {
|
|
15
|
+
type: 'url';
|
|
16
|
+
url: URL;
|
|
17
|
+
};
|
|
18
|
+
export type AllowedUrlPrefix = WildcardAllowedUrlPrefix | ProtocolAllowedUrlPrefix | AbsoluteAllowedUrlPrefix;
|
|
19
|
+
export declare const parseAllowedUrlPrefixes: (allowedPrefixes: readonly unknown[], defaultOrigin?: string) => AllowedUrlPrefix[];
|
|
20
|
+
export declare const transformParsedUrl: (url: unknown, allowedPrefixes: readonly AllowedUrlPrefix[], defaultOrigin?: string, { kind }?: TransformUrlOptions) => string | null;
|
|
21
|
+
export declare const transformUrl: (url: unknown, allowedPrefixes: string[], defaultOrigin?: string, { kind }?: TransformUrlOptions) => string | null;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export const parseUrl = (url, defaultOrigin) => {
|
|
2
|
+
if (typeof url !== 'string')
|
|
3
|
+
return null;
|
|
4
|
+
try {
|
|
5
|
+
// Try to parse as absolute URL first
|
|
6
|
+
const urlObject = new URL(url);
|
|
7
|
+
return urlObject;
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
// If that fails and we have a defaultOrigin, try with it
|
|
11
|
+
if (defaultOrigin) {
|
|
12
|
+
try {
|
|
13
|
+
const urlObject = new URL(url, defaultOrigin);
|
|
14
|
+
return urlObject;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export const isPathRelativeUrl = (url) => {
|
|
24
|
+
if (typeof url !== 'string')
|
|
25
|
+
return false;
|
|
26
|
+
return ((url.startsWith('/') && !url.startsWith('//')) ||
|
|
27
|
+
url.startsWith('./') ||
|
|
28
|
+
url.startsWith('../') ||
|
|
29
|
+
url.startsWith('#') ||
|
|
30
|
+
url.startsWith('?'));
|
|
31
|
+
};
|
|
32
|
+
const WILDCARD_PROTOCOLS = {
|
|
33
|
+
link: new Set(['http:', 'https:', 'mailto:', 'tel:']),
|
|
34
|
+
image: new Set(['http:', 'https:'])
|
|
35
|
+
};
|
|
36
|
+
const PROTOCOL_ONLY_PREFIX_PATTERN = /^(?<protocol>[A-Za-z][A-Za-z\d+.-]*:)(\/\/)?$/;
|
|
37
|
+
function isAllowedByWildcard(url, kind) {
|
|
38
|
+
if (WILDCARD_PROTOCOLS[kind].has(url.protocol)) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return kind === 'image' && url.protocol === 'data:' && /^data:image\//i.test(url.href);
|
|
42
|
+
}
|
|
43
|
+
const parseAllowedUrlPrefix = (prefix, defaultOrigin) => {
|
|
44
|
+
if (prefix === '*') {
|
|
45
|
+
return { type: 'wildcard' };
|
|
46
|
+
}
|
|
47
|
+
if (typeof prefix !== 'string') {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const protocolOnlyMatch = prefix.match(PROTOCOL_ONLY_PREFIX_PATTERN);
|
|
51
|
+
if (protocolOnlyMatch?.groups?.protocol) {
|
|
52
|
+
return {
|
|
53
|
+
type: 'protocol',
|
|
54
|
+
protocol: protocolOnlyMatch.groups.protocol.toLowerCase()
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const parsedPrefix = parseUrl(prefix, defaultOrigin);
|
|
58
|
+
if (!parsedPrefix) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
type: 'url',
|
|
63
|
+
url: parsedPrefix
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export const parseAllowedUrlPrefixes = (allowedPrefixes, defaultOrigin) => allowedPrefixes
|
|
67
|
+
.map((prefix) => parseAllowedUrlPrefix(prefix, defaultOrigin))
|
|
68
|
+
.filter((prefix) => prefix !== null);
|
|
69
|
+
function isAllowedByPrefix(url, prefix, kind) {
|
|
70
|
+
switch (prefix.type) {
|
|
71
|
+
case 'wildcard':
|
|
72
|
+
return isAllowedByWildcard(url, kind);
|
|
73
|
+
case 'protocol':
|
|
74
|
+
return url.protocol.toLowerCase() === prefix.protocol;
|
|
75
|
+
case 'url':
|
|
76
|
+
return prefix.url.origin === url.origin && url.href.startsWith(prefix.url.href);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export const transformParsedUrl = (url, allowedPrefixes, defaultOrigin, { kind = 'link' } = {}) => {
|
|
80
|
+
if (!url)
|
|
81
|
+
return null;
|
|
82
|
+
if (typeof url !== 'string')
|
|
83
|
+
return null;
|
|
84
|
+
const inputWasRelative = isPathRelativeUrl(url);
|
|
85
|
+
const parsedUrl = parseUrl(url, defaultOrigin);
|
|
86
|
+
if (!parsedUrl)
|
|
87
|
+
return null;
|
|
88
|
+
if (allowedPrefixes.some((prefix) => isAllowedByPrefix(parsedUrl, prefix, kind))) {
|
|
89
|
+
if (inputWasRelative) {
|
|
90
|
+
return url;
|
|
91
|
+
}
|
|
92
|
+
return parsedUrl.href;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
};
|
|
96
|
+
export const transformUrl = (url, allowedPrefixes, defaultOrigin, { kind = 'link' } = {}) => transformParsedUrl(url, parseAllowedUrlPrefixes(allowedPrefixes, defaultOrigin), defaultOrigin, {
|
|
97
|
+
kind
|
|
98
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { SvelteSet } from 'svelte/reactivity';
|
|
2
|
+
import { on } from 'svelte/events';
|
|
3
|
+
export const useClickOutside = (props) => {
|
|
4
|
+
const refs = new SvelteSet();
|
|
5
|
+
let listener = null;
|
|
6
|
+
const onClickOutside = (e) => {
|
|
7
|
+
let inRef = false;
|
|
8
|
+
if (props.isActive) {
|
|
9
|
+
const path = e.composedPath();
|
|
10
|
+
path.forEach((node) => {
|
|
11
|
+
refs.forEach((ref) => {
|
|
12
|
+
if (ref instanceof Node && node instanceof Node && ref?.isSameNode(node)) {
|
|
13
|
+
inRef = true;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
if (!inRef) {
|
|
18
|
+
props.callback();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
get attachment() {
|
|
24
|
+
return props.isActive
|
|
25
|
+
? (node) => {
|
|
26
|
+
refs.add(node);
|
|
27
|
+
if (!listener) {
|
|
28
|
+
listener = on(node.ownerDocument, 'pointerdown', onClickOutside);
|
|
29
|
+
}
|
|
30
|
+
return () => {
|
|
31
|
+
refs.delete(node);
|
|
32
|
+
if (listener && refs.size === 0) {
|
|
33
|
+
listener();
|
|
34
|
+
listener = null;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
: null;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { on } from 'svelte/events';
|
|
2
|
+
export const useKeyDown = (opts) => {
|
|
3
|
+
let listener;
|
|
4
|
+
const eventCallback = (event) => {
|
|
5
|
+
if (opts.keys.includes(event.key)) {
|
|
6
|
+
event.preventDefault();
|
|
7
|
+
opts.callback();
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
$effect(() => {
|
|
11
|
+
if (opts.isActive && !listener) {
|
|
12
|
+
listener = on(window, 'keydown', eventCallback);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
listener?.();
|
|
16
|
+
listener = null;
|
|
17
|
+
}
|
|
18
|
+
return () => {
|
|
19
|
+
listener?.();
|
|
20
|
+
listener = null;
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
};
|