@pallasoft/ps-utilities 2.22.0 → 2.23.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/dist/common/constants/html-entity-names.d.ts +1 -0
- package/dist/common/constants/html-entity-names.js +34 -0
- package/dist/common/constants/html-heading-styles.d.ts +1 -0
- package/dist/common/constants/html-heading-styles.js +11 -0
- package/dist/common/constants/html-text-config.d.ts +76 -0
- package/dist/common/constants/html-text-config.js +136 -0
- package/dist/common/constants/html-token-kind.d.ts +5 -0
- package/dist/common/constants/html-token-kind.js +8 -0
- package/dist/common/constants/portable-text-config.d.ts +16 -0
- package/dist/common/constants/portable-text-config.js +15 -1
- package/dist/common/html-entity-service.d.ts +6 -0
- package/dist/common/html-entity-service.js +34 -0
- package/dist/common/html-style-service.d.ts +12 -0
- package/dist/common/html-style-service.js +92 -0
- package/dist/common/html-token-service.d.ts +8 -0
- package/dist/common/html-token-service.js +90 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js +1 -0
- package/dist/common/portable-text-html-service.d.ts +28 -0
- package/dist/common/portable-text-html-service.js +392 -0
- package/dist/common/portable-text-link-service.d.ts +7 -0
- package/dist/common/portable-text-link-service.js +42 -0
- package/dist/common/portable-text-service.d.ts +7 -4
- package/dist/common/portable-text-service.js +15 -10
- package/dist/common/portable-text-tracking-service.d.ts +7 -0
- package/dist/common/portable-text-tracking-service.js +45 -0
- package/dist/esm/common/constants/html-entity-names.js +31 -0
- package/dist/esm/common/constants/html-heading-styles.js +8 -0
- package/dist/esm/common/constants/html-text-config.js +133 -0
- package/dist/esm/common/constants/html-token-kind.js +5 -0
- package/dist/esm/common/constants/portable-text-config.js +15 -1
- package/dist/esm/common/html-entity-service.js +25 -0
- package/dist/esm/common/html-style-service.js +85 -0
- package/dist/esm/common/html-token-service.js +83 -0
- package/dist/esm/common/index.js +1 -0
- package/dist/esm/common/portable-text-html-service.js +375 -0
- package/dist/esm/common/portable-text-link-service.js +35 -0
- package/dist/esm/common/portable-text-service.js +14 -9
- package/dist/esm/common/portable-text-tracking-service.js +34 -0
- package/dist/esm/models/html-attributes-data.js +1 -0
- package/dist/esm/models/html-close-token-data.js +1 -0
- package/dist/esm/models/html-context-data.js +1 -0
- package/dist/esm/models/html-conversion-data.js +1 -0
- package/dist/esm/models/html-declarations-data.js +1 -0
- package/dist/esm/models/html-frame-data.js +1 -0
- package/dist/esm/models/html-open-token-data.js +1 -0
- package/dist/esm/models/html-run-data.js +1 -0
- package/dist/esm/models/html-text-token-data.js +1 -0
- package/dist/esm/models/html-token-data.js +1 -0
- package/dist/esm/models/portable-text-block-link.js +1 -0
- package/dist/esm/models/portable-text-content-data.js +1 -0
- package/dist/esm/models/portable-text-image-data.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/models/html-attributes-data.d.ts +1 -0
- package/dist/models/html-attributes-data.js +2 -0
- package/dist/models/html-close-token-data.d.ts +5 -0
- package/dist/models/html-close-token-data.js +2 -0
- package/dist/models/html-context-data.d.ts +6 -0
- package/dist/models/html-context-data.js +2 -0
- package/dist/models/html-conversion-data.d.ts +12 -0
- package/dist/models/html-conversion-data.js +2 -0
- package/dist/models/html-declarations-data.d.ts +1 -0
- package/dist/models/html-declarations-data.js +2 -0
- package/dist/models/html-frame-data.d.ts +13 -0
- package/dist/models/html-frame-data.js +2 -0
- package/dist/models/html-open-token-data.d.ts +8 -0
- package/dist/models/html-open-token-data.js +2 -0
- package/dist/models/html-run-data.d.ts +9 -0
- package/dist/models/html-run-data.js +2 -0
- package/dist/models/html-text-token-data.d.ts +5 -0
- package/dist/models/html-text-token-data.js +2 -0
- package/dist/models/html-token-data.d.ts +4 -0
- package/dist/models/html-token-data.js +2 -0
- package/dist/models/portable-text-block-link.d.ts +7 -0
- package/dist/models/portable-text-block-link.js +2 -0
- package/dist/models/portable-text-content-data.d.ts +3 -0
- package/dist/models/portable-text-content-data.js +2 -0
- package/dist/models/portable-text-image-data.d.ts +7 -0
- package/dist/models/portable-text-image-data.js +2 -0
- package/package.json +2 -2
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.PortableTextHtmlService = void 0;
|
|
15
|
+
var html_heading_styles_1 = require("./constants/html-heading-styles");
|
|
16
|
+
var html_text_config_1 = require("./constants/html-text-config");
|
|
17
|
+
var html_token_kind_1 = require("./constants/html-token-kind");
|
|
18
|
+
var portable_text_config_1 = require("./constants/portable-text-config");
|
|
19
|
+
var html_entity_service_1 = require("./html-entity-service");
|
|
20
|
+
var html_style_service_1 = require("./html-style-service");
|
|
21
|
+
var html_token_service_1 = require("./html-token-service");
|
|
22
|
+
var portable_text_link_service_1 = require("./portable-text-link-service");
|
|
23
|
+
var portable_text_tracking_service_1 = require("./portable-text-tracking-service");
|
|
24
|
+
var PortableTextHtmlService = /** @class */ (function () {
|
|
25
|
+
function PortableTextHtmlService() {
|
|
26
|
+
}
|
|
27
|
+
PortableTextHtmlService.fromHtml = function (html) {
|
|
28
|
+
var state = {
|
|
29
|
+
blocks: [],
|
|
30
|
+
counter: 0,
|
|
31
|
+
lists: [],
|
|
32
|
+
pending: [],
|
|
33
|
+
skipDepth: 0,
|
|
34
|
+
skipName: null,
|
|
35
|
+
stack: [PortableTextHtmlService.rootFrame()],
|
|
36
|
+
};
|
|
37
|
+
html_token_service_1.HtmlTokenService.tokenize(html).forEach(function (token) {
|
|
38
|
+
return PortableTextHtmlService.consume(state, token);
|
|
39
|
+
});
|
|
40
|
+
PortableTextHtmlService.flush(state);
|
|
41
|
+
return portable_text_tracking_service_1.PortableTextTrackingService.stripUrlLines(state.blocks);
|
|
42
|
+
};
|
|
43
|
+
PortableTextHtmlService.rootFrame = function () {
|
|
44
|
+
return {
|
|
45
|
+
code: false,
|
|
46
|
+
em: false,
|
|
47
|
+
heading: null,
|
|
48
|
+
href: null,
|
|
49
|
+
isQuote: false,
|
|
50
|
+
listLevel: null,
|
|
51
|
+
name: html_text_config_1.HtmlTextConfig.rootName,
|
|
52
|
+
ordered: false,
|
|
53
|
+
size: null,
|
|
54
|
+
strong: false,
|
|
55
|
+
underline: false,
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
PortableTextHtmlService.consume = function (state, token) {
|
|
59
|
+
if (token.kind === html_token_kind_1.HtmlTokenKind.Text) {
|
|
60
|
+
if (state.skipName === null)
|
|
61
|
+
PortableTextHtmlService.addText(state, token.text);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (token.kind === html_token_kind_1.HtmlTokenKind.Open)
|
|
65
|
+
PortableTextHtmlService.open(state, token);
|
|
66
|
+
else
|
|
67
|
+
PortableTextHtmlService.close(state, token.name);
|
|
68
|
+
};
|
|
69
|
+
PortableTextHtmlService.isVoid = function (token) {
|
|
70
|
+
return html_text_config_1.HtmlTextConfig.voidTags.includes(token.name) || token.selfClosing;
|
|
71
|
+
};
|
|
72
|
+
PortableTextHtmlService.isBlockTag = function (name) {
|
|
73
|
+
return (html_text_config_1.HtmlTextConfig.blockTags.includes(name) ||
|
|
74
|
+
Object.prototype.hasOwnProperty.call(html_heading_styles_1.HtmlHeadingStyles, name) ||
|
|
75
|
+
name === html_text_config_1.HtmlTextConfig.quoteTag ||
|
|
76
|
+
name === html_text_config_1.HtmlTextConfig.listItemTag ||
|
|
77
|
+
name === html_text_config_1.HtmlTextConfig.orderedListTag ||
|
|
78
|
+
name === html_text_config_1.HtmlTextConfig.unorderedListTag);
|
|
79
|
+
};
|
|
80
|
+
PortableTextHtmlService.isHidden = function (token, declarations) {
|
|
81
|
+
var attributes = token.attributes;
|
|
82
|
+
return (html_text_config_1.HtmlTextConfig.skipTags.includes(token.name) ||
|
|
83
|
+
Object.prototype.hasOwnProperty.call(attributes, html_text_config_1.HtmlTextConfig.hiddenAttribute) ||
|
|
84
|
+
attributes[html_text_config_1.HtmlTextConfig.aria] === html_text_config_1.HtmlTextConfig.hiddenAria ||
|
|
85
|
+
html_text_config_1.HtmlTextConfig.hiddenClassPattern.test(PortableTextHtmlService.attribute(attributes, html_text_config_1.HtmlTextConfig.classAttribute)) ||
|
|
86
|
+
html_style_service_1.HtmlStyleService.isHidden(declarations));
|
|
87
|
+
};
|
|
88
|
+
PortableTextHtmlService.attribute = function (attributes, name) {
|
|
89
|
+
return Object.prototype.hasOwnProperty.call(attributes, name) ? attributes[name] : '';
|
|
90
|
+
};
|
|
91
|
+
PortableTextHtmlService.open = function (state, token) {
|
|
92
|
+
if (state.skipName !== null) {
|
|
93
|
+
if (token.name === state.skipName && !PortableTextHtmlService.isVoid(token))
|
|
94
|
+
state.skipDepth += 1;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
var declarations = html_style_service_1.HtmlStyleService.parse(PortableTextHtmlService.attribute(token.attributes, html_text_config_1.HtmlTextConfig.styleAttribute));
|
|
98
|
+
if (PortableTextHtmlService.isHidden(token, declarations)) {
|
|
99
|
+
if (!PortableTextHtmlService.isVoid(token)) {
|
|
100
|
+
state.skipName = token.name;
|
|
101
|
+
state.skipDepth = 1;
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (token.name === html_text_config_1.HtmlTextConfig.breakTag) {
|
|
106
|
+
PortableTextHtmlService.lineBreak(state);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (token.name === html_text_config_1.HtmlTextConfig.imageTag) {
|
|
110
|
+
PortableTextHtmlService.image(state, token, declarations);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
var isBlock = PortableTextHtmlService.isBlockTag(token.name) || token.name === html_text_config_1.HtmlTextConfig.ruleTag;
|
|
114
|
+
if (isBlock)
|
|
115
|
+
PortableTextHtmlService.flush(state);
|
|
116
|
+
if (PortableTextHtmlService.isVoid(token))
|
|
117
|
+
return;
|
|
118
|
+
if (token.name === html_text_config_1.HtmlTextConfig.listItemTag)
|
|
119
|
+
PortableTextHtmlService.closeOpenItem(state);
|
|
120
|
+
if (token.name === html_text_config_1.HtmlTextConfig.orderedListTag)
|
|
121
|
+
state.lists.push(true);
|
|
122
|
+
if (token.name === html_text_config_1.HtmlTextConfig.unorderedListTag)
|
|
123
|
+
state.lists.push(false);
|
|
124
|
+
state.stack.push(PortableTextHtmlService.frame(state, token, declarations));
|
|
125
|
+
};
|
|
126
|
+
PortableTextHtmlService.closeOpenItem = function (state) {
|
|
127
|
+
while (state.stack[state.stack.length - 1].name === html_text_config_1.HtmlTextConfig.listItemTag)
|
|
128
|
+
state.stack.pop();
|
|
129
|
+
};
|
|
130
|
+
PortableTextHtmlService.frame = function (state, token, declarations) {
|
|
131
|
+
var parent = state.stack[state.stack.length - 1];
|
|
132
|
+
var name = token.name;
|
|
133
|
+
var isLink = name === html_text_config_1.HtmlTextConfig.linkTag;
|
|
134
|
+
var hasHref = Object.prototype.hasOwnProperty.call(token.attributes, html_text_config_1.HtmlTextConfig.hrefAttribute);
|
|
135
|
+
var size = html_style_service_1.HtmlStyleService.fontSize(declarations);
|
|
136
|
+
var isItem = name === html_text_config_1.HtmlTextConfig.listItemTag;
|
|
137
|
+
return {
|
|
138
|
+
code: html_text_config_1.HtmlTextConfig.codeTags.includes(name) || parent.code,
|
|
139
|
+
em: PortableTextHtmlService.resolve(parent.em, html_text_config_1.HtmlTextConfig.emTags.includes(name), html_style_service_1.HtmlStyleService.italic(declarations)),
|
|
140
|
+
heading: Object.prototype.hasOwnProperty.call(html_heading_styles_1.HtmlHeadingStyles, name)
|
|
141
|
+
? html_heading_styles_1.HtmlHeadingStyles[name]
|
|
142
|
+
: null,
|
|
143
|
+
href: isLink && hasHref
|
|
144
|
+
? portable_text_link_service_1.PortableTextLinkService.normalize(token.attributes[html_text_config_1.HtmlTextConfig.hrefAttribute])
|
|
145
|
+
: parent.href,
|
|
146
|
+
isQuote: name === html_text_config_1.HtmlTextConfig.quoteTag,
|
|
147
|
+
listLevel: isItem
|
|
148
|
+
? Math.min(Math.max(state.lists.length, html_text_config_1.HtmlTextConfig.bodyListLevel), portable_text_config_1.PortableTextConfig.maxListLevel)
|
|
149
|
+
: null,
|
|
150
|
+
name: name,
|
|
151
|
+
ordered: isItem && state.lists.length > 0 && state.lists[state.lists.length - 1],
|
|
152
|
+
size: size !== null ? size : parent.size,
|
|
153
|
+
strong: PortableTextHtmlService.resolve(parent.strong, html_text_config_1.HtmlTextConfig.strongTags.includes(name), html_style_service_1.HtmlStyleService.bold(declarations)),
|
|
154
|
+
underline: isLink
|
|
155
|
+
? parent.underline
|
|
156
|
+
: PortableTextHtmlService.resolve(parent.underline, html_text_config_1.HtmlTextConfig.underlineTags.includes(name), html_style_service_1.HtmlStyleService.underline(declarations)),
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
PortableTextHtmlService.resolve = function (inherited, byTag, byStyle) {
|
|
160
|
+
if (byStyle !== null)
|
|
161
|
+
return byStyle;
|
|
162
|
+
return byTag || inherited;
|
|
163
|
+
};
|
|
164
|
+
PortableTextHtmlService.close = function (state, name) {
|
|
165
|
+
if (state.skipName !== null) {
|
|
166
|
+
if (name === state.skipName) {
|
|
167
|
+
state.skipDepth -= 1;
|
|
168
|
+
if (state.skipDepth === 0)
|
|
169
|
+
state.skipName = null;
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
var index = state.stack.length - 1;
|
|
174
|
+
while (index > 0 && state.stack[index].name !== name)
|
|
175
|
+
index -= 1;
|
|
176
|
+
if (index === 0)
|
|
177
|
+
return;
|
|
178
|
+
if (PortableTextHtmlService.isBlockTag(name))
|
|
179
|
+
PortableTextHtmlService.flush(state);
|
|
180
|
+
if (name === html_text_config_1.HtmlTextConfig.orderedListTag || name === html_text_config_1.HtmlTextConfig.unorderedListTag)
|
|
181
|
+
state.lists.pop();
|
|
182
|
+
state.stack.length = index;
|
|
183
|
+
};
|
|
184
|
+
PortableTextHtmlService.addText = function (state, raw) {
|
|
185
|
+
var isPre = state.stack.some(function (frame) { return frame.name === html_text_config_1.HtmlTextConfig.preTag; });
|
|
186
|
+
var cleaned = html_entity_service_1.HtmlEntityService.decode(raw).replace(html_text_config_1.HtmlTextConfig.invisiblePattern, '');
|
|
187
|
+
var text = isPre
|
|
188
|
+
? cleaned
|
|
189
|
+
.replace(html_text_config_1.HtmlTextConfig.preLineBreakPattern, html_text_config_1.HtmlTextConfig.newline)
|
|
190
|
+
.replace(html_text_config_1.HtmlTextConfig.nonBreakingPattern, html_text_config_1.HtmlTextConfig.space)
|
|
191
|
+
: cleaned.replace(html_text_config_1.HtmlTextConfig.whitespacePattern, html_text_config_1.HtmlTextConfig.space);
|
|
192
|
+
if (!isPre) {
|
|
193
|
+
var last = state.pending[state.pending.length - 1];
|
|
194
|
+
var atStart = last === undefined ||
|
|
195
|
+
last.text.endsWith(html_text_config_1.HtmlTextConfig.space) ||
|
|
196
|
+
last.text.endsWith(html_text_config_1.HtmlTextConfig.newline);
|
|
197
|
+
if (atStart)
|
|
198
|
+
text = text.replace(html_text_config_1.HtmlTextConfig.leadingSpacePattern, '');
|
|
199
|
+
}
|
|
200
|
+
if (text !== '')
|
|
201
|
+
PortableTextHtmlService.push(state, text);
|
|
202
|
+
};
|
|
203
|
+
PortableTextHtmlService.push = function (state, text) {
|
|
204
|
+
var frame = state.stack[state.stack.length - 1];
|
|
205
|
+
state.pending.push({
|
|
206
|
+
code: frame.code,
|
|
207
|
+
em: frame.em,
|
|
208
|
+
href: frame.href,
|
|
209
|
+
size: frame.size,
|
|
210
|
+
strong: frame.strong,
|
|
211
|
+
text: text,
|
|
212
|
+
underline: frame.underline,
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
PortableTextHtmlService.lineBreak = function (state) {
|
|
216
|
+
var last = state.pending[state.pending.length - 1];
|
|
217
|
+
if (last === undefined)
|
|
218
|
+
return;
|
|
219
|
+
if (last.text.endsWith(html_text_config_1.HtmlTextConfig.newline)) {
|
|
220
|
+
PortableTextHtmlService.flush(state);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
last.text = last.text.replace(html_text_config_1.HtmlTextConfig.trailingSpacePattern, '');
|
|
224
|
+
PortableTextHtmlService.push(state, html_text_config_1.HtmlTextConfig.newline);
|
|
225
|
+
};
|
|
226
|
+
PortableTextHtmlService.image = function (state, token, declarations) {
|
|
227
|
+
var src = portable_text_link_service_1.PortableTextLinkService.normalizeImage(PortableTextHtmlService.attribute(token.attributes, html_text_config_1.HtmlTextConfig.srcAttribute));
|
|
228
|
+
if (src === null || PortableTextHtmlService.isTiny(token.attributes, declarations))
|
|
229
|
+
return;
|
|
230
|
+
var alt = html_entity_service_1.HtmlEntityService.decode(PortableTextHtmlService.attribute(token.attributes, html_text_config_1.HtmlTextConfig.altAttribute))
|
|
231
|
+
.replace(html_text_config_1.HtmlTextConfig.invisiblePattern, '')
|
|
232
|
+
.replace(html_text_config_1.HtmlTextConfig.whitespacePattern, html_text_config_1.HtmlTextConfig.space)
|
|
233
|
+
.trim()
|
|
234
|
+
.slice(0, portable_text_config_1.PortableTextConfig.maxImageAltLength);
|
|
235
|
+
PortableTextHtmlService.flush(state);
|
|
236
|
+
state.blocks.push({
|
|
237
|
+
_key: PortableTextHtmlService.key(state, portable_text_config_1.PortableTextConfig.keyPrefix.image),
|
|
238
|
+
_type: portable_text_config_1.PortableTextConfig.imageType,
|
|
239
|
+
alt: alt,
|
|
240
|
+
src: src,
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
PortableTextHtmlService.isTiny = function (attributes, declarations) {
|
|
244
|
+
var sizes = [html_text_config_1.HtmlTextConfig.widthAttribute, html_text_config_1.HtmlTextConfig.heightAttribute].map(function (name) {
|
|
245
|
+
return Number.parseFloat(PortableTextHtmlService.attribute(attributes, name));
|
|
246
|
+
});
|
|
247
|
+
return (sizes.some(function (size) { return !Number.isNaN(size) && size <= html_text_config_1.HtmlTextConfig.tinyImageSize; }) ||
|
|
248
|
+
html_style_service_1.HtmlStyleService.isTiny(declarations));
|
|
249
|
+
};
|
|
250
|
+
PortableTextHtmlService.key = function (state, prefix) {
|
|
251
|
+
var key = "".concat(prefix).concat(state.counter);
|
|
252
|
+
state.counter += 1;
|
|
253
|
+
return key;
|
|
254
|
+
};
|
|
255
|
+
PortableTextHtmlService.context = function (state) {
|
|
256
|
+
var context = {
|
|
257
|
+
heading: null,
|
|
258
|
+
isQuote: false,
|
|
259
|
+
listLevel: null,
|
|
260
|
+
ordered: false,
|
|
261
|
+
};
|
|
262
|
+
for (var index = state.stack.length - 1; index > 0; index -= 1) {
|
|
263
|
+
var frame = state.stack[index];
|
|
264
|
+
if (context.heading === null && frame.heading !== null)
|
|
265
|
+
context.heading = frame.heading;
|
|
266
|
+
if (frame.isQuote)
|
|
267
|
+
context.isQuote = true;
|
|
268
|
+
if (context.listLevel === null && frame.listLevel !== null) {
|
|
269
|
+
context.listLevel = frame.listLevel;
|
|
270
|
+
context.ordered = frame.ordered;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return context;
|
|
274
|
+
};
|
|
275
|
+
PortableTextHtmlService.trimmed = function (runs) {
|
|
276
|
+
var kept = runs.filter(function (run) { return run.text !== ''; });
|
|
277
|
+
while (kept.length > 0) {
|
|
278
|
+
var last = kept[kept.length - 1];
|
|
279
|
+
last.text = last.text.replace(html_text_config_1.HtmlTextConfig.trailingBreakPattern, '');
|
|
280
|
+
if (last.text !== '')
|
|
281
|
+
break;
|
|
282
|
+
kept.pop();
|
|
283
|
+
}
|
|
284
|
+
while (kept.length > 0) {
|
|
285
|
+
var first = kept[0];
|
|
286
|
+
first.text = first.text.replace(html_text_config_1.HtmlTextConfig.leadingBreakPattern, '');
|
|
287
|
+
if (first.text !== '')
|
|
288
|
+
break;
|
|
289
|
+
kept.shift();
|
|
290
|
+
}
|
|
291
|
+
return kept;
|
|
292
|
+
};
|
|
293
|
+
PortableTextHtmlService.flush = function (state) {
|
|
294
|
+
var runs = PortableTextHtmlService.trimmed(state.pending);
|
|
295
|
+
state.pending = [];
|
|
296
|
+
if (runs.length === 0)
|
|
297
|
+
return;
|
|
298
|
+
var context = PortableTextHtmlService.context(state);
|
|
299
|
+
var heading = PortableTextHtmlService.headingOf(runs, context);
|
|
300
|
+
var markDefs = [];
|
|
301
|
+
var children = PortableTextHtmlService.merge(runs, heading !== null).map(function (span) { return ({
|
|
302
|
+
_key: PortableTextHtmlService.key(state, portable_text_config_1.PortableTextConfig.keyPrefix.span),
|
|
303
|
+
_type: portable_text_config_1.PortableTextConfig.spanType,
|
|
304
|
+
marks: PortableTextHtmlService.marks(state, span, markDefs),
|
|
305
|
+
text: span.text,
|
|
306
|
+
}); });
|
|
307
|
+
var level = context.listLevel;
|
|
308
|
+
var base = {
|
|
309
|
+
_key: PortableTextHtmlService.key(state, portable_text_config_1.PortableTextConfig.keyPrefix.block),
|
|
310
|
+
_type: portable_text_config_1.PortableTextConfig.blockType,
|
|
311
|
+
children: children,
|
|
312
|
+
markDefs: markDefs,
|
|
313
|
+
style: PortableTextHtmlService.styleOf(context, heading),
|
|
314
|
+
};
|
|
315
|
+
state.blocks.push(level !== null && heading === null
|
|
316
|
+
? __assign(__assign({}, base), { level: level, listItem: context.ordered
|
|
317
|
+
? portable_text_config_1.PortableTextConfig.numberItem
|
|
318
|
+
: portable_text_config_1.PortableTextConfig.bulletItem }) : base);
|
|
319
|
+
};
|
|
320
|
+
PortableTextHtmlService.styleOf = function (context, heading) {
|
|
321
|
+
if (heading !== null)
|
|
322
|
+
return heading;
|
|
323
|
+
if (context.listLevel !== null)
|
|
324
|
+
return portable_text_config_1.PortableTextConfig.normalStyle;
|
|
325
|
+
return context.isQuote ? portable_text_config_1.PortableTextConfig.quoteStyle : portable_text_config_1.PortableTextConfig.normalStyle;
|
|
326
|
+
};
|
|
327
|
+
PortableTextHtmlService.headingOf = function (runs, context) {
|
|
328
|
+
if (context.heading !== null)
|
|
329
|
+
return context.heading;
|
|
330
|
+
if (context.listLevel !== null || context.isQuote)
|
|
331
|
+
return null;
|
|
332
|
+
var text = runs.map(function (run) { return run.text; }).join('');
|
|
333
|
+
if (text.includes(html_text_config_1.HtmlTextConfig.newline) || text.length > html_text_config_1.HtmlTextConfig.largeText.maxLength)
|
|
334
|
+
return null;
|
|
335
|
+
var filled = runs.filter(function (run) { return run.text.trim() !== ''; });
|
|
336
|
+
var sizes = filled.map(function (run) { return run.size; });
|
|
337
|
+
if (sizes.every(function (size) { return size !== null; })) {
|
|
338
|
+
var smallest = Math.min.apply(Math, sizes);
|
|
339
|
+
if (smallest >= html_text_config_1.HtmlTextConfig.largeText.heading1)
|
|
340
|
+
return portable_text_config_1.PortableTextConfig.headingStyles[0];
|
|
341
|
+
if (smallest >= html_text_config_1.HtmlTextConfig.largeText.heading2)
|
|
342
|
+
return portable_text_config_1.PortableTextConfig.headingStyles[1];
|
|
343
|
+
if (smallest >= html_text_config_1.HtmlTextConfig.largeText.heading3)
|
|
344
|
+
return portable_text_config_1.PortableTextConfig.headingStyles[2];
|
|
345
|
+
}
|
|
346
|
+
var isBold = filled.every(function (run) { return run.strong && run.href === null; }) &&
|
|
347
|
+
text.length <= html_text_config_1.HtmlTextConfig.boldHeadingMaxLength &&
|
|
348
|
+
!html_text_config_1.HtmlTextConfig.headingEndPattern.test(text.trim());
|
|
349
|
+
return isBold ? portable_text_config_1.PortableTextConfig.headingStyles[2] : null;
|
|
350
|
+
};
|
|
351
|
+
PortableTextHtmlService.merge = function (runs, dropStrong) {
|
|
352
|
+
var merged = [];
|
|
353
|
+
runs.forEach(function (run) {
|
|
354
|
+
var next = dropStrong ? __assign(__assign({}, run), { strong: false }) : __assign({}, run);
|
|
355
|
+
var last = merged[merged.length - 1];
|
|
356
|
+
if (last !== undefined &&
|
|
357
|
+
last.strong === next.strong &&
|
|
358
|
+
last.em === next.em &&
|
|
359
|
+
last.underline === next.underline &&
|
|
360
|
+
last.code === next.code &&
|
|
361
|
+
last.href === next.href) {
|
|
362
|
+
last.text += next.text;
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
merged.push(next);
|
|
366
|
+
});
|
|
367
|
+
return merged;
|
|
368
|
+
};
|
|
369
|
+
PortableTextHtmlService.marks = function (state, span, markDefs) {
|
|
370
|
+
var marks = [];
|
|
371
|
+
if (span.strong)
|
|
372
|
+
marks.push(portable_text_config_1.PortableTextConfig.decorators[0]);
|
|
373
|
+
if (span.em)
|
|
374
|
+
marks.push(portable_text_config_1.PortableTextConfig.decorators[1]);
|
|
375
|
+
if (span.underline)
|
|
376
|
+
marks.push(portable_text_config_1.PortableTextConfig.decorators[2]);
|
|
377
|
+
if (span.code)
|
|
378
|
+
marks.push(portable_text_config_1.PortableTextConfig.decorators[3]);
|
|
379
|
+
if (span.href === null)
|
|
380
|
+
return marks;
|
|
381
|
+
var existing = markDefs.find(function (definition) { return definition.href === span.href; });
|
|
382
|
+
var key = existing !== undefined
|
|
383
|
+
? existing._key
|
|
384
|
+
: PortableTextHtmlService.key(state, portable_text_config_1.PortableTextConfig.keyPrefix.link);
|
|
385
|
+
if (existing === undefined)
|
|
386
|
+
markDefs.push({ _key: key, _type: portable_text_config_1.PortableTextConfig.linkType, href: span.href });
|
|
387
|
+
marks.push(key);
|
|
388
|
+
return marks;
|
|
389
|
+
};
|
|
390
|
+
return PortableTextHtmlService;
|
|
391
|
+
}());
|
|
392
|
+
exports.PortableTextHtmlService = PortableTextHtmlService;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class PortableTextLinkService {
|
|
2
|
+
static isSafe(href: string): boolean;
|
|
3
|
+
static normalize(raw: string | undefined): string | null;
|
|
4
|
+
static isSafeImage(src: string): boolean;
|
|
5
|
+
static normalizeImage(raw: string | undefined): string | null;
|
|
6
|
+
private static isControl;
|
|
7
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortableTextLinkService = void 0;
|
|
4
|
+
var portable_text_config_1 = require("./constants/portable-text-config");
|
|
5
|
+
var PortableTextLinkService = /** @class */ (function () {
|
|
6
|
+
function PortableTextLinkService() {
|
|
7
|
+
}
|
|
8
|
+
PortableTextLinkService.isSafe = function (href) {
|
|
9
|
+
try {
|
|
10
|
+
return portable_text_config_1.PortableTextConfig.linkProtocols.includes(new URL(href).protocol);
|
|
11
|
+
}
|
|
12
|
+
catch (_a) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
PortableTextLinkService.normalize = function (raw) {
|
|
17
|
+
if (typeof raw !== 'string')
|
|
18
|
+
return null;
|
|
19
|
+
var href = Array.from(raw)
|
|
20
|
+
.filter(function (character) { return !PortableTextLinkService.isControl(character); })
|
|
21
|
+
.join('')
|
|
22
|
+
.trim();
|
|
23
|
+
return href !== '' && PortableTextLinkService.isSafe(href) ? href : null;
|
|
24
|
+
};
|
|
25
|
+
PortableTextLinkService.isSafeImage = function (src) {
|
|
26
|
+
return PortableTextLinkService.normalizeImage(src) === src;
|
|
27
|
+
};
|
|
28
|
+
PortableTextLinkService.normalizeImage = function (raw) {
|
|
29
|
+
var src = PortableTextLinkService.normalize(raw);
|
|
30
|
+
return src !== null &&
|
|
31
|
+
portable_text_config_1.PortableTextConfig.imageProtocolPattern.test(src) &&
|
|
32
|
+
src.length <= portable_text_config_1.PortableTextConfig.maxImageSrcLength
|
|
33
|
+
? src
|
|
34
|
+
: null;
|
|
35
|
+
};
|
|
36
|
+
PortableTextLinkService.isControl = function (character) {
|
|
37
|
+
var code = character.charCodeAt(0);
|
|
38
|
+
return code <= portable_text_config_1.PortableTextConfig.maxControlCode || code === portable_text_config_1.PortableTextConfig.deleteCode;
|
|
39
|
+
};
|
|
40
|
+
return PortableTextLinkService;
|
|
41
|
+
}());
|
|
42
|
+
exports.PortableTextLinkService = PortableTextLinkService;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { PortableTextBlock } from '@portabletext/types';
|
|
2
|
+
import type { PortableTextContentData } from '../models/portable-text-content-data';
|
|
2
3
|
export declare class PortableTextService {
|
|
3
|
-
static toPlainText(blocks:
|
|
4
|
+
static toPlainText(blocks: PortableTextContentData[]): string;
|
|
4
5
|
static fromPlainText(text: string): PortableTextBlock[];
|
|
5
|
-
static
|
|
6
|
-
static
|
|
6
|
+
static fromHtml(html: string): PortableTextContentData[];
|
|
7
|
+
static isEmpty(blocks: PortableTextContentData[] | null | undefined): boolean;
|
|
8
|
+
static toHtml(blocks: PortableTextContentData[]): string;
|
|
7
9
|
private static blockText;
|
|
8
|
-
|
|
10
|
+
static isSafeLink(href: string): boolean;
|
|
11
|
+
static isSafeImage(src: string): boolean;
|
|
9
12
|
}
|
|
@@ -4,6 +4,8 @@ exports.PortableTextService = void 0;
|
|
|
4
4
|
var ps_models_1 = require("@pallasoft/ps-models");
|
|
5
5
|
var to_html_1 = require("@portabletext/to-html");
|
|
6
6
|
var portable_text_config_1 = require("./constants/portable-text-config");
|
|
7
|
+
var portable_text_html_service_1 = require("./portable-text-html-service");
|
|
8
|
+
var portable_text_link_service_1 = require("./portable-text-link-service");
|
|
7
9
|
var PortableTextService = /** @class */ (function () {
|
|
8
10
|
function PortableTextService() {
|
|
9
11
|
}
|
|
@@ -13,7 +15,7 @@ var PortableTextService = /** @class */ (function () {
|
|
|
13
15
|
var counters = {};
|
|
14
16
|
var previousWasList = false;
|
|
15
17
|
blocks.forEach(function (block) {
|
|
16
|
-
if (block._type !== ps_models_1.Common.PortableTextConfig.blockType)
|
|
18
|
+
if (!('children' in block) || block._type !== ps_models_1.Common.PortableTextConfig.blockType)
|
|
17
19
|
return;
|
|
18
20
|
var text = _this.blockText(block);
|
|
19
21
|
var level = typeof block.level === 'number' ? block.level : 1;
|
|
@@ -65,20 +67,25 @@ var PortableTextService = /** @class */ (function () {
|
|
|
65
67
|
],
|
|
66
68
|
}); });
|
|
67
69
|
};
|
|
70
|
+
PortableTextService.fromHtml = function (html) {
|
|
71
|
+
return portable_text_html_service_1.PortableTextHtmlService.fromHtml(html);
|
|
72
|
+
};
|
|
68
73
|
PortableTextService.isEmpty = function (blocks) {
|
|
69
74
|
var _this = this;
|
|
70
|
-
return (!Array.isArray(blocks) ||
|
|
75
|
+
return (!Array.isArray(blocks) ||
|
|
76
|
+
blocks.every(function (block) { return !('children' in block) || _this.blockText(block).trim().length === 0; }));
|
|
71
77
|
};
|
|
72
78
|
PortableTextService.toHtml = function (blocks) {
|
|
73
|
-
var
|
|
79
|
+
var _a;
|
|
74
80
|
return (0, to_html_1.toHTML)(blocks, {
|
|
75
81
|
onMissingComponent: false,
|
|
76
82
|
components: {
|
|
83
|
+
types: (_a = {}, _a[portable_text_config_1.PortableTextConfig.imageType] = function () { return ''; }, _a),
|
|
77
84
|
marks: {
|
|
78
85
|
link: function (_a) {
|
|
79
86
|
var children = _a.children, value = _a.value;
|
|
80
87
|
var href = typeof (value === null || value === void 0 ? void 0 : value.href) === 'string' ? value.href : '';
|
|
81
|
-
return
|
|
88
|
+
return PortableTextService.isSafeLink(href)
|
|
82
89
|
? "<a href=\"".concat((0, to_html_1.escapeHTML)(href), "\" rel=\"").concat(portable_text_config_1.PortableTextConfig.linkRel, "\" target=\"").concat(portable_text_config_1.PortableTextConfig.linkTarget, "\">").concat(children, "</a>")
|
|
83
90
|
: children;
|
|
84
91
|
},
|
|
@@ -98,12 +105,10 @@ var PortableTextService = /** @class */ (function () {
|
|
|
98
105
|
: '';
|
|
99
106
|
};
|
|
100
107
|
PortableTextService.isSafeLink = function (href) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
108
|
+
return portable_text_link_service_1.PortableTextLinkService.isSafe(href);
|
|
109
|
+
};
|
|
110
|
+
PortableTextService.isSafeImage = function (src) {
|
|
111
|
+
return portable_text_link_service_1.PortableTextLinkService.isSafeImage(src);
|
|
107
112
|
};
|
|
108
113
|
return PortableTextService;
|
|
109
114
|
}());
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PortableTextContentData } from '../models/portable-text-content-data';
|
|
2
|
+
export declare class PortableTextTrackingService {
|
|
3
|
+
static stripUrlLines(blocks: PortableTextContentData[]): PortableTextContentData[];
|
|
4
|
+
private static hrefs;
|
|
5
|
+
private static isDuplicate;
|
|
6
|
+
private static matches;
|
|
7
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortableTextTrackingService = void 0;
|
|
4
|
+
var html_text_config_1 = require("./constants/html-text-config");
|
|
5
|
+
var portable_text_config_1 = require("./constants/portable-text-config");
|
|
6
|
+
var PortableTextTrackingService = /** @class */ (function () {
|
|
7
|
+
function PortableTextTrackingService() {
|
|
8
|
+
}
|
|
9
|
+
PortableTextTrackingService.stripUrlLines = function (blocks) {
|
|
10
|
+
var hrefs = PortableTextTrackingService.hrefs(blocks);
|
|
11
|
+
return blocks.filter(function (block) { return !PortableTextTrackingService.isDuplicate(block, hrefs); });
|
|
12
|
+
};
|
|
13
|
+
PortableTextTrackingService.hrefs = function (blocks) {
|
|
14
|
+
return blocks.flatMap(function (block) {
|
|
15
|
+
return 'children' in block && Array.isArray(block.markDefs)
|
|
16
|
+
? block.markDefs.flatMap(function (definition) {
|
|
17
|
+
return typeof definition.href === 'string' ? [definition.href] : [];
|
|
18
|
+
})
|
|
19
|
+
: [];
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
PortableTextTrackingService.isDuplicate = function (block, hrefs) {
|
|
23
|
+
if (!('children' in block) || !Array.isArray(block.children))
|
|
24
|
+
return false;
|
|
25
|
+
if (block.style !== portable_text_config_1.PortableTextConfig.normalStyle || typeof block.listItem === 'string')
|
|
26
|
+
return false;
|
|
27
|
+
var text = block.children
|
|
28
|
+
.map(function (child) { return (typeof child.text === 'string' ? child.text : ''); })
|
|
29
|
+
.join('')
|
|
30
|
+
.trim();
|
|
31
|
+
var match = html_text_config_1.HtmlTextConfig.trackingLinePattern.exec(text);
|
|
32
|
+
return (match !== null && hrefs.some(function (href) { return PortableTextTrackingService.matches(href, match[1]); }));
|
|
33
|
+
};
|
|
34
|
+
PortableTextTrackingService.matches = function (href, shown) {
|
|
35
|
+
var ascii = shown.indexOf(html_text_config_1.HtmlTextConfig.trackingEllipsis);
|
|
36
|
+
var unicode = shown.indexOf(html_text_config_1.HtmlTextConfig.trackingEllipsisCharacter);
|
|
37
|
+
var index = ascii !== -1 ? ascii : unicode;
|
|
38
|
+
if (index === -1)
|
|
39
|
+
return href === shown || href.startsWith(shown);
|
|
40
|
+
var width = ascii !== -1 ? html_text_config_1.HtmlTextConfig.trackingEllipsis.length : 1;
|
|
41
|
+
return href.startsWith(shown.slice(0, index)) && href.endsWith(shown.slice(index + width));
|
|
42
|
+
};
|
|
43
|
+
return PortableTextTrackingService;
|
|
44
|
+
}());
|
|
45
|
+
exports.PortableTextTrackingService = PortableTextTrackingService;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const HtmlEntityNames = {
|
|
2
|
+
amp: '&',
|
|
3
|
+
lt: '<',
|
|
4
|
+
gt: '>',
|
|
5
|
+
quot: '"',
|
|
6
|
+
apos: "'",
|
|
7
|
+
nbsp: ' ',
|
|
8
|
+
ndash: '\u2013',
|
|
9
|
+
mdash: '\u2014',
|
|
10
|
+
hellip: '\u2026',
|
|
11
|
+
rsquo: '\u2019',
|
|
12
|
+
lsquo: '\u2018',
|
|
13
|
+
rdquo: '\u201D',
|
|
14
|
+
ldquo: '\u201C',
|
|
15
|
+
copy: '\u00A9',
|
|
16
|
+
reg: '\u00AE',
|
|
17
|
+
trade: '\u2122',
|
|
18
|
+
bull: '\u2022',
|
|
19
|
+
middot: '\u00B7',
|
|
20
|
+
euro: '\u20AC',
|
|
21
|
+
pound: '\u00A3',
|
|
22
|
+
yen: '\u00A5',
|
|
23
|
+
cent: '\u00A2',
|
|
24
|
+
laquo: '\u00AB',
|
|
25
|
+
raquo: '\u00BB',
|
|
26
|
+
times: '\u00D7',
|
|
27
|
+
deg: '\u00B0',
|
|
28
|
+
zwnj: '',
|
|
29
|
+
zwj: '',
|
|
30
|
+
shy: '',
|
|
31
|
+
};
|