@takeshape/util 9.80.4 → 9.81.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/arrays.js +0 -10
- package/dist/async-noop.js +0 -2
- package/dist/billing.js +8 -4
- package/dist/browser.js +0 -2
- package/dist/clone.js +0 -1
- package/dist/common.js +0 -50
- package/dist/delay.js +3 -6
- package/dist/draftjs-templates.js +1 -11
- package/dist/draftjs.js +37 -196
- package/dist/encryption.js +2 -11
- package/dist/get-image-url.js +2 -3
- package/dist/gzip.js +0 -4
- package/dist/highlight-code.js +1 -19
- package/dist/http.js +0 -6
- package/dist/index.js +0 -8
- package/dist/map.js +0 -1
- package/dist/merge.js +3 -20
- package/dist/mime.js +4 -12
- package/dist/predicate.js +0 -3
- package/dist/search-params.js +0 -6
- package/dist/set-in.js +0 -2
- package/dist/sets.js +0 -5
- package/dist/sleep.js +0 -1
- package/dist/sort-object.js +4 -4
- package/dist/strings.js +4 -21
- package/dist/templates.js +1 -25
- package/dist/timezone.js +2 -4
- package/dist/types.js +8 -10
- package/dist/unix-to-iso.js +0 -2
- package/dist/value.js +0 -2
- package/dist/visit.js +1 -15
- package/es/arrays.js +0 -4
- package/es/billing.js +10 -0
- package/es/delay.js +3 -4
- package/es/draftjs-templates.js +1 -1
- package/es/draftjs.js +39 -173
- package/es/encryption.js +2 -3
- package/es/get-image-url.js +2 -1
- package/es/highlight-code.js +0 -1
- package/es/http.js +0 -4
- package/es/index.js +2 -2
- package/es/merge.js +4 -8
- package/es/mime.js +4 -5
- package/es/search-params.js +0 -2
- package/es/set-in.js +1 -1
- package/es/sets.js +0 -3
- package/es/sort-object.js +4 -2
- package/es/strings.js +4 -8
- package/es/templates.js +0 -14
- package/es/timezone.js +2 -2
- package/es/types.js +8 -3
- package/es/visit.js +1 -12
- package/package.json +3 -3
package/es/draftjs.js
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* The intention is to move all this to the client package in the end, but right now it's here
|
|
3
3
|
* to be shared between the draftjs / mdx implementations, which span the client / server differently.
|
|
4
4
|
*/
|
|
5
|
+
|
|
5
6
|
import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js';
|
|
6
|
-
import { imageTemplate, imageTemplateMdx, oembedTemplate, oembedTemplateMdx } from './templates';
|
|
7
|
+
import { imageTemplate, imageTemplateMdx, oembedTemplate, oembedTemplateMdx } from './templates';
|
|
8
|
+
// With @types/htmlparser2 it says parseDOM is not exported; excluding types for now
|
|
7
9
|
// @ts-expect-error Untyped
|
|
8
|
-
|
|
9
10
|
import { parseDOM } from 'htmlparser2';
|
|
10
11
|
import Url from 'url-parse';
|
|
11
12
|
import render from 'dom-serializer';
|
|
@@ -22,7 +23,6 @@ const SUBSCRIPT_MARKER = 'TEMPORARY_SUBSCRIPT_MARKER_K5VrdPEzyQyy2RcY';
|
|
|
22
23
|
const INSERT_MARKER = 'TEMPORARY_INSERT_MARKER_FSYdr8m8CS7YLb8y';
|
|
23
24
|
const EXTERNAL_LINK_MARKER = 'TEMPORARY_EXTERNAL_LINK_MARKER_HP3vprmERkc9ZAss';
|
|
24
25
|
const pullquoteStyle = 'font-style: italic; margin: 2em 2.5em;';
|
|
25
|
-
|
|
26
26
|
const getDraftjsEmpty = (depth = 0) => {
|
|
27
27
|
return {
|
|
28
28
|
key: shortid.generate(),
|
|
@@ -33,9 +33,7 @@ const getDraftjsEmpty = (depth = 0) => {
|
|
|
33
33
|
inlineStyleRanges: []
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
-
|
|
37
36
|
const REMOVAL_MARKER_TYPE = 'removal-marker';
|
|
38
|
-
|
|
39
37
|
const getDraftjsRemovalMarker = (depth = 0) => {
|
|
40
38
|
return {
|
|
41
39
|
key: shortid.generate(),
|
|
@@ -46,35 +44,31 @@ const getDraftjsRemovalMarker = (depth = 0) => {
|
|
|
46
44
|
inlineStyleRanges: []
|
|
47
45
|
};
|
|
48
46
|
};
|
|
49
|
-
|
|
50
47
|
const mdxShortcodePrefix = tagName => `TS${tagName}`;
|
|
51
|
-
|
|
52
48
|
const handleMultiword = (markdown, marker, markdownSyntax, markdownRegex) => {
|
|
53
49
|
const parts = markdown.replace(markdownRegex, `\\${markdownSyntax}`).split(marker);
|
|
54
50
|
let result = '';
|
|
55
51
|
let open = false;
|
|
56
|
-
|
|
57
52
|
for (const [i, part] of parts.entries()) {
|
|
58
53
|
if (open) {
|
|
59
54
|
result += part.replace(/\s+/g, match => `${markdownSyntax}${match}${markdownSyntax}`);
|
|
60
55
|
} else {
|
|
61
56
|
result += part;
|
|
62
57
|
}
|
|
63
|
-
|
|
64
58
|
if (i !== parts.length - 1) {
|
|
65
59
|
result += markdownSyntax;
|
|
66
60
|
open = !open;
|
|
67
61
|
}
|
|
68
62
|
}
|
|
69
|
-
|
|
70
63
|
return result;
|
|
71
|
-
};
|
|
72
|
-
|
|
64
|
+
};
|
|
73
65
|
|
|
66
|
+
// Workaround inability to reasonably process child content using draftToMarkdown
|
|
74
67
|
const handleExternalLinks = markdown => {
|
|
75
68
|
const regex = /(<TSExternalLink text="TEMPORARY_EXTERNAL_LINK_MARKER_HP3vprmERkc9ZAss" href="[^"]+">)([^<]+)<\/TSExternalLink>/g;
|
|
76
69
|
return markdown.replace(regex, (match, openTag, content) => match.replace(EXTERNAL_LINK_MARKER, content));
|
|
77
70
|
};
|
|
71
|
+
|
|
78
72
|
/**
|
|
79
73
|
* Adjust entity ranges to insert `amount` space starting at `after`.
|
|
80
74
|
* For example, take this visualization of a range with offset 5 and length 5, where
|
|
@@ -89,14 +83,14 @@ const handleExternalLinks = markdown => {
|
|
|
89
83
|
* If you insert amount -2 after 7 you get this:
|
|
90
84
|
* -----|-|-----
|
|
91
85
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
94
86
|
export const adjustEntityRanges = (entityRanges, after, amount) => {
|
|
95
|
-
return entityRanges.map(entityRange => ({
|
|
87
|
+
return entityRanges.map(entityRange => ({
|
|
88
|
+
...entityRange,
|
|
96
89
|
offset: entityRange.offset > after ? entityRange.offset + amount : entityRange.offset,
|
|
97
90
|
length: entityRange.offset <= after && entityRange.offset + entityRange.length > after ? entityRange.length + amount : entityRange.length
|
|
98
91
|
}));
|
|
99
92
|
};
|
|
93
|
+
|
|
100
94
|
/**
|
|
101
95
|
* Encode HTML entities in this draftjs state.
|
|
102
96
|
* We adjust entity ranges so that their offsets and lengths are still correct with the
|
|
@@ -105,9 +99,9 @@ export const adjustEntityRanges = (entityRanges, after, amount) => {
|
|
|
105
99
|
* the link entity's offset needs to be increased by 5 when the text is encoded.
|
|
106
100
|
* This also applies to the entity's length if there are escaped characters inside the entity.
|
|
107
101
|
*/
|
|
108
|
-
|
|
109
102
|
export const encodeHtmlEntities = draftjs => {
|
|
110
|
-
const result = {
|
|
103
|
+
const result = {
|
|
104
|
+
...draftjs,
|
|
111
105
|
blocks: draftjs.blocks.map(block => {
|
|
112
106
|
let {
|
|
113
107
|
text
|
|
@@ -115,11 +109,9 @@ export const encodeHtmlEntities = draftjs => {
|
|
|
115
109
|
let {
|
|
116
110
|
entityRanges
|
|
117
111
|
} = block;
|
|
118
|
-
|
|
119
112
|
for (let i = 0; i < text.length; i++) {
|
|
120
113
|
const char = text.charAt(i);
|
|
121
114
|
const encodedChar = he.encode(char);
|
|
122
|
-
|
|
123
115
|
if (encodedChar !== char) {
|
|
124
116
|
const offset = encodedChar.length - char.length;
|
|
125
117
|
entityRanges = adjustEntityRanges(entityRanges, i, offset);
|
|
@@ -127,8 +119,8 @@ export const encodeHtmlEntities = draftjs => {
|
|
|
127
119
|
i += offset;
|
|
128
120
|
}
|
|
129
121
|
}
|
|
130
|
-
|
|
131
|
-
|
|
122
|
+
return {
|
|
123
|
+
...block,
|
|
132
124
|
entityRanges,
|
|
133
125
|
text
|
|
134
126
|
};
|
|
@@ -143,54 +135,43 @@ export function fromDraftjs(draftjs, styleItems, entityItems) {
|
|
|
143
135
|
open() {
|
|
144
136
|
return '***';
|
|
145
137
|
},
|
|
146
|
-
|
|
147
138
|
close() {
|
|
148
139
|
return '';
|
|
149
140
|
}
|
|
150
|
-
|
|
151
141
|
},
|
|
152
142
|
SUP: {
|
|
153
143
|
open() {
|
|
154
144
|
return SUPERSCRIPT_MARKER;
|
|
155
145
|
},
|
|
156
|
-
|
|
157
146
|
close() {
|
|
158
147
|
return SUPERSCRIPT_MARKER;
|
|
159
148
|
}
|
|
160
|
-
|
|
161
149
|
},
|
|
162
150
|
SUB: {
|
|
163
151
|
open() {
|
|
164
152
|
return SUBSCRIPT_MARKER;
|
|
165
153
|
},
|
|
166
|
-
|
|
167
154
|
close() {
|
|
168
155
|
return SUBSCRIPT_MARKER;
|
|
169
156
|
}
|
|
170
|
-
|
|
171
157
|
},
|
|
172
158
|
UNDERLINE: {
|
|
173
159
|
open() {
|
|
174
160
|
return INSERT_MARKER;
|
|
175
161
|
},
|
|
176
|
-
|
|
177
162
|
close() {
|
|
178
163
|
return INSERT_MARKER;
|
|
179
164
|
}
|
|
180
|
-
|
|
181
165
|
},
|
|
182
166
|
'code-block': {
|
|
183
167
|
open(entity) {
|
|
184
168
|
var _entity$data;
|
|
185
|
-
|
|
186
169
|
const lang = (entity === null || entity === void 0 ? void 0 : (_entity$data = entity.data) === null || _entity$data === void 0 ? void 0 : _entity$data.lang) ?? '';
|
|
187
170
|
return `\`\`\`${lang}\n`;
|
|
188
171
|
},
|
|
189
|
-
|
|
190
172
|
close() {
|
|
191
173
|
return '\n```';
|
|
192
174
|
}
|
|
193
|
-
|
|
194
175
|
},
|
|
195
176
|
...styleItems
|
|
196
177
|
},
|
|
@@ -198,35 +179,28 @@ export function fromDraftjs(draftjs, styleItems, entityItems) {
|
|
|
198
179
|
LINK: {
|
|
199
180
|
open(entity) {
|
|
200
181
|
var _entity$data2;
|
|
201
|
-
|
|
202
182
|
if ((entity === null || entity === void 0 ? void 0 : (_entity$data2 = entity.data) === null || _entity$data2 === void 0 ? void 0 : _entity$data2.target) === '_blank') {
|
|
203
183
|
var _entity$data3, _entity$data4;
|
|
204
|
-
|
|
205
184
|
const url = escape((entity === null || entity === void 0 ? void 0 : (_entity$data3 = entity.data) === null || _entity$data3 === void 0 ? void 0 : _entity$data3.url) || (entity === null || entity === void 0 ? void 0 : (_entity$data4 = entity.data) === null || _entity$data4 === void 0 ? void 0 : _entity$data4.href) || '');
|
|
206
185
|
return `<TSExternalLink text="${EXTERNAL_LINK_MARKER}" href="${url}">`;
|
|
207
186
|
}
|
|
208
|
-
|
|
209
187
|
return '[';
|
|
210
188
|
},
|
|
211
|
-
|
|
212
189
|
close(entity) {
|
|
213
190
|
var _entity$data5, _entity$data6, _entity$data7;
|
|
214
|
-
|
|
215
191
|
if ((entity === null || entity === void 0 ? void 0 : (_entity$data5 = entity.data) === null || _entity$data5 === void 0 ? void 0 : _entity$data5.target) === '_blank') {
|
|
216
192
|
return '</TSExternalLink>';
|
|
217
193
|
}
|
|
218
|
-
|
|
219
194
|
return ']('.concat((entity === null || entity === void 0 ? void 0 : (_entity$data6 = entity.data) === null || _entity$data6 === void 0 ? void 0 : _entity$data6.url) || (entity === null || entity === void 0 ? void 0 : (_entity$data7 = entity.data) === null || _entity$data7 === void 0 ? void 0 : _entity$data7.href) || '', ')');
|
|
220
195
|
}
|
|
221
|
-
|
|
222
196
|
},
|
|
223
197
|
...entityItems
|
|
224
198
|
}
|
|
225
199
|
});
|
|
226
200
|
markdown = handleExternalLinks(markdown);
|
|
227
201
|
markdown = handleMultiword(markdown, SUPERSCRIPT_MARKER, '^', /\^/g);
|
|
228
|
-
markdown = handleMultiword(markdown, SUBSCRIPT_MARKER, '~', /~/g);
|
|
229
|
-
|
|
202
|
+
markdown = handleMultiword(markdown, SUBSCRIPT_MARKER, '~', /~/g);
|
|
203
|
+
// eslint-disable-next-line security-node/non-literal-reg-expr
|
|
230
204
|
markdown = markdown.replace(/\+/g, '\\+').replace(new RegExp(INSERT_MARKER, 'g'), '++');
|
|
231
205
|
return markdown;
|
|
232
206
|
}
|
|
@@ -236,50 +210,42 @@ export function draftjsToMd(draftjs, assets) {
|
|
|
236
210
|
open() {
|
|
237
211
|
return `<aside style="${pullquoteStyle}">`;
|
|
238
212
|
},
|
|
239
|
-
|
|
240
213
|
close() {
|
|
241
214
|
return '</aside>';
|
|
242
215
|
}
|
|
243
|
-
|
|
244
216
|
}
|
|
245
217
|
}, {
|
|
246
218
|
oembed: {
|
|
247
219
|
open() {
|
|
248
220
|
return '';
|
|
249
221
|
},
|
|
250
|
-
|
|
251
222
|
close(entity) {
|
|
252
223
|
if (!(entity !== null && entity !== void 0 && entity.data)) {
|
|
253
224
|
return '';
|
|
254
225
|
}
|
|
255
|
-
|
|
256
226
|
const {
|
|
257
227
|
data
|
|
258
228
|
} = entity;
|
|
259
229
|
return oembedTemplate(str => str, data);
|
|
260
230
|
}
|
|
261
|
-
|
|
262
231
|
},
|
|
263
232
|
image: {
|
|
264
233
|
open() {
|
|
265
234
|
return '';
|
|
266
235
|
},
|
|
267
|
-
|
|
268
236
|
close(entity) {
|
|
269
237
|
if (!(entity !== null && entity !== void 0 && entity.data)) {
|
|
270
238
|
return '';
|
|
271
239
|
}
|
|
272
|
-
|
|
273
240
|
const {
|
|
274
241
|
data
|
|
275
242
|
} = entity;
|
|
276
243
|
const asset = assets[data.id];
|
|
277
|
-
|
|
278
244
|
if (asset === undefined) {
|
|
279
245
|
return '';
|
|
280
246
|
}
|
|
281
|
-
|
|
282
|
-
|
|
247
|
+
const imageTemplateData = {
|
|
248
|
+
...data,
|
|
283
249
|
alignment: data.alignment,
|
|
284
250
|
size: data.size,
|
|
285
251
|
asset,
|
|
@@ -289,82 +255,66 @@ export function draftjsToMd(draftjs, assets) {
|
|
|
289
255
|
};
|
|
290
256
|
return imageTemplate(str => str, imageTemplateData);
|
|
291
257
|
}
|
|
292
|
-
|
|
293
258
|
}
|
|
294
259
|
});
|
|
295
260
|
}
|
|
296
|
-
|
|
297
261
|
function getTextFromDraftjs(draftjs) {
|
|
298
262
|
var _draftjs$blocks, _draftjs$blocks$;
|
|
299
|
-
|
|
300
263
|
return draftjs === null || draftjs === void 0 ? void 0 : (_draftjs$blocks = draftjs.blocks) === null || _draftjs$blocks === void 0 ? void 0 : (_draftjs$blocks$ = _draftjs$blocks[0]) === null || _draftjs$blocks$ === void 0 ? void 0 : _draftjs$blocks$.text;
|
|
301
264
|
}
|
|
302
|
-
|
|
303
265
|
function getAssetField(data, asset, field) {
|
|
304
266
|
const dataText = getTextFromDraftjs(data[field]);
|
|
305
|
-
|
|
306
267
|
if (dataText) {
|
|
307
268
|
return dataText;
|
|
308
269
|
}
|
|
309
|
-
|
|
310
270
|
if (asset[field]) {
|
|
311
271
|
return getTextFromDraftjs(asset[field]);
|
|
312
272
|
}
|
|
313
|
-
|
|
314
273
|
return '';
|
|
315
274
|
}
|
|
316
|
-
|
|
317
275
|
export function draftjsToMdx(draftjs, assets, prefix = mdxShortcodePrefix) {
|
|
318
276
|
return fromDraftjs(draftjs, {
|
|
319
277
|
pullquote: {
|
|
320
278
|
open() {
|
|
321
279
|
return `<${prefix('Pullquote')}>`;
|
|
322
280
|
},
|
|
323
|
-
|
|
324
281
|
close() {
|
|
325
282
|
return `</${prefix('Pullquote')}>`;
|
|
326
283
|
}
|
|
327
|
-
|
|
328
284
|
}
|
|
329
285
|
}, {
|
|
330
286
|
oembed: {
|
|
331
287
|
open() {
|
|
332
288
|
return '';
|
|
333
289
|
},
|
|
334
|
-
|
|
335
290
|
close(entity) {
|
|
336
291
|
if (!(entity !== null && entity !== void 0 && entity.data)) {
|
|
337
292
|
return '';
|
|
338
293
|
}
|
|
339
|
-
|
|
340
294
|
const {
|
|
341
295
|
data
|
|
342
296
|
} = entity;
|
|
343
297
|
return oembedTemplateMdx(prefix, data);
|
|
344
298
|
}
|
|
345
|
-
|
|
346
299
|
},
|
|
347
300
|
image: {
|
|
348
301
|
open() {
|
|
349
302
|
return '';
|
|
350
303
|
},
|
|
351
|
-
|
|
352
304
|
close(entity) {
|
|
353
305
|
var _entity$data8, _data$link, _data$link2;
|
|
354
|
-
|
|
355
306
|
const asset = assets[entity === null || entity === void 0 ? void 0 : (_entity$data8 = entity.data) === null || _entity$data8 === void 0 ? void 0 : _entity$data8.id];
|
|
356
|
-
|
|
357
307
|
if (!(entity !== null && entity !== void 0 && entity.data) || asset === undefined) {
|
|
358
308
|
return '';
|
|
359
309
|
}
|
|
360
|
-
|
|
361
310
|
const {
|
|
362
311
|
data
|
|
363
312
|
} = entity;
|
|
364
313
|
const imageUrlOptions = process.env.NODE_ENV === 'production' ? {} : {
|
|
365
314
|
baseUrl: 'https://images.dev.takeshape.io'
|
|
366
315
|
};
|
|
367
|
-
return imageTemplateMdx(prefix, {
|
|
316
|
+
return imageTemplateMdx(prefix, {
|
|
317
|
+
...data,
|
|
368
318
|
assetId: data.id,
|
|
369
319
|
assetPath: asset.path,
|
|
370
320
|
credit: getAssetField(data, asset, 'credit'),
|
|
@@ -374,15 +324,12 @@ export function draftjsToMdx(draftjs, assets, prefix = mdxShortcodePrefix) {
|
|
|
374
324
|
src: getImageUrl(asset.path, data.imageParams, imageUrlOptions)
|
|
375
325
|
});
|
|
376
326
|
}
|
|
377
|
-
|
|
378
327
|
}
|
|
379
328
|
});
|
|
380
329
|
}
|
|
381
|
-
|
|
382
330
|
function getAssetIdFromImageSrc(src) {
|
|
383
331
|
return new Url(src).pathname.split('/')[3];
|
|
384
332
|
}
|
|
385
|
-
|
|
386
333
|
function parseFigureClass(className) {
|
|
387
334
|
if (className === undefined) {
|
|
388
335
|
return {
|
|
@@ -390,56 +337,44 @@ function parseFigureClass(className) {
|
|
|
390
337
|
size: undefined
|
|
391
338
|
};
|
|
392
339
|
}
|
|
393
|
-
|
|
394
340
|
const parts = className.split(' ');
|
|
395
341
|
return {
|
|
396
342
|
alignment: parts[0],
|
|
397
343
|
size: parts[1]
|
|
398
344
|
};
|
|
399
|
-
}
|
|
400
|
-
|
|
345
|
+
}
|
|
401
346
|
|
|
347
|
+
// eslint-disable-next-line complexity
|
|
402
348
|
function htmlDomToDraftjsImage(dom, item, entityKeyGenerator) {
|
|
403
349
|
for (const figure of dom) {
|
|
404
350
|
if (figure.type === 'tag' && figure.name === 'figure') {
|
|
405
351
|
var _figure$children, _figure$children2, _figcaption, _figcaption2, _caption, _credit, _link, _link2;
|
|
406
|
-
|
|
407
352
|
let link;
|
|
408
353
|
const figureChildOne = (_figure$children = figure.children) === null || _figure$children === void 0 ? void 0 : _figure$children[0];
|
|
409
354
|
const figureChildTwo = (_figure$children2 = figure.children) === null || _figure$children2 === void 0 ? void 0 : _figure$children2[1];
|
|
410
|
-
|
|
411
355
|
if ((figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.type) === 'tag' && (figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.name) === 'a') {
|
|
412
356
|
link = figureChildOne;
|
|
413
357
|
}
|
|
414
|
-
|
|
415
358
|
let figcaption;
|
|
416
|
-
|
|
417
359
|
if ((figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.type) === 'tag' && (figureChildOne === null || figureChildOne === void 0 ? void 0 : figureChildOne.name) === 'figcaption') {
|
|
418
360
|
figcaption = figureChildOne;
|
|
419
361
|
} else if ((figureChildTwo === null || figureChildTwo === void 0 ? void 0 : figureChildTwo.type) === 'tag' && (figureChildTwo === null || figureChildTwo === void 0 ? void 0 : figureChildTwo.name) === 'figcaption') {
|
|
420
362
|
figcaption = figure.children[1];
|
|
421
363
|
}
|
|
422
|
-
|
|
423
364
|
const img = link ? link.children[0] : figure.children[0];
|
|
424
|
-
|
|
425
365
|
if ((img === null || img === void 0 ? void 0 : img.type) !== 'tag' || (img === null || img === void 0 ? void 0 : img.name) !== 'img') {
|
|
426
366
|
return;
|
|
427
367
|
}
|
|
428
|
-
|
|
429
368
|
const figcaptionChild = (_figcaption = figcaption) === null || _figcaption === void 0 ? void 0 : _figcaption.children[0];
|
|
430
369
|
let caption;
|
|
431
|
-
|
|
432
370
|
if ((figcaptionChild === null || figcaptionChild === void 0 ? void 0 : figcaptionChild.type) === 'tag' && (figcaptionChild === null || figcaptionChild === void 0 ? void 0 : figcaptionChild.name) === 'span' && (figcaptionChild === null || figcaptionChild === void 0 ? void 0 : figcaptionChild.attribs.class) === 'caption') {
|
|
433
371
|
caption = figcaptionChild;
|
|
434
372
|
}
|
|
435
|
-
|
|
436
373
|
const captionChildThree = (_figcaption2 = figcaption) === null || _figcaption2 === void 0 ? void 0 : _figcaption2.children[2];
|
|
437
374
|
let credit;
|
|
438
|
-
|
|
439
375
|
if ((captionChildThree === null || captionChildThree === void 0 ? void 0 : captionChildThree.type) === 'tag' && (captionChildThree === null || captionChildThree === void 0 ? void 0 : captionChildThree.name) === 'span' && (captionChildThree === null || captionChildThree === void 0 ? void 0 : captionChildThree.attribs.class) === 'credit') {
|
|
440
376
|
credit = captionChildThree;
|
|
441
377
|
}
|
|
442
|
-
|
|
443
378
|
const {
|
|
444
379
|
alignment,
|
|
445
380
|
size
|
|
@@ -459,22 +394,17 @@ function htmlDomToDraftjsImage(dom, item, entityKeyGenerator) {
|
|
|
459
394
|
}
|
|
460
395
|
}
|
|
461
396
|
}
|
|
462
|
-
|
|
463
397
|
function htmlDomToOembed(dom, item, entityKeyGenerator) {
|
|
464
398
|
for (const oembedDiv of dom) {
|
|
465
399
|
if (oembedDiv.type === 'tag' && oembedDiv.name === 'div' && oembedDiv.attribs.class === 'oembed') {
|
|
466
400
|
const blockquote = oembedDiv.children[0];
|
|
467
|
-
|
|
468
401
|
if ((blockquote === null || blockquote === void 0 ? void 0 : blockquote.type) !== 'tag' || (blockquote === null || blockquote === void 0 ? void 0 : blockquote.name) !== 'blockquote') {
|
|
469
402
|
return;
|
|
470
403
|
}
|
|
471
|
-
|
|
472
404
|
const script = oembedDiv.children[2];
|
|
473
|
-
|
|
474
405
|
if ((script === null || script === void 0 ? void 0 : script.type) !== 'script' || (script === null || script === void 0 ? void 0 : script.name) !== 'script') {
|
|
475
406
|
return;
|
|
476
407
|
}
|
|
477
|
-
|
|
478
408
|
return getDraftjsOembed({
|
|
479
409
|
html: `${render(blockquote)}\n${render(script)}\n`,
|
|
480
410
|
key: entityKeyGenerator(),
|
|
@@ -483,7 +413,6 @@ function htmlDomToOembed(dom, item, entityKeyGenerator) {
|
|
|
483
413
|
}
|
|
484
414
|
}
|
|
485
415
|
}
|
|
486
|
-
|
|
487
416
|
function mdxToDraftjsOembed(item, entityKeyGenerator) {
|
|
488
417
|
if (item.content.startsWith(`<${mdxShortcodePrefix('Oembed')}`)) {
|
|
489
418
|
const dom = parseDOM(item.content)[0];
|
|
@@ -504,16 +433,13 @@ function mdxToDraftjsOembed(item, entityKeyGenerator) {
|
|
|
504
433
|
});
|
|
505
434
|
}
|
|
506
435
|
}
|
|
507
|
-
|
|
508
436
|
function htmlDomToPullquote(dom, item) {
|
|
509
437
|
for (const pullquote of dom) {
|
|
510
438
|
if (pullquote.type === 'tag' && pullquote.name === 'aside' && pullquote.attribs.style === pullquoteStyle) {
|
|
511
439
|
const text = pullquote.children[0];
|
|
512
|
-
|
|
513
440
|
if ((text === null || text === void 0 ? void 0 : text.type) !== 'text') {
|
|
514
441
|
return;
|
|
515
442
|
}
|
|
516
|
-
|
|
517
443
|
return getDraftjsPullquote({
|
|
518
444
|
text: text.data,
|
|
519
445
|
depth: item.level
|
|
@@ -521,7 +447,6 @@ function htmlDomToPullquote(dom, item) {
|
|
|
521
447
|
}
|
|
522
448
|
}
|
|
523
449
|
}
|
|
524
|
-
|
|
525
450
|
function fromMd(md, blockEntities, blockTypes) {
|
|
526
451
|
md = md.replace(/\\\+/g, '+').replace(/\\~/g, '~').replace(/\\\^/g, '^');
|
|
527
452
|
return markdownToDraft(md, {
|
|
@@ -540,7 +465,6 @@ function fromMd(md, blockEntities, blockTypes) {
|
|
|
540
465
|
if (!item) {
|
|
541
466
|
return getDraftjsEmpty();
|
|
542
467
|
}
|
|
543
|
-
|
|
544
468
|
return {
|
|
545
469
|
key: shortid.generate(),
|
|
546
470
|
text: ' ',
|
|
@@ -551,12 +475,10 @@ function fromMd(md, blockEntities, blockTypes) {
|
|
|
551
475
|
data: {}
|
|
552
476
|
};
|
|
553
477
|
},
|
|
554
|
-
|
|
555
478
|
fence(item) {
|
|
556
479
|
if (!item) {
|
|
557
480
|
return getDraftjsEmpty();
|
|
558
481
|
}
|
|
559
|
-
|
|
560
482
|
return {
|
|
561
483
|
type: 'code-block',
|
|
562
484
|
data: {
|
|
@@ -569,54 +491,44 @@ function fromMd(md, blockEntities, blockTypes) {
|
|
|
569
491
|
inlineStyleRanges: []
|
|
570
492
|
};
|
|
571
493
|
},
|
|
572
|
-
|
|
573
494
|
...blockTypes
|
|
574
495
|
}
|
|
575
496
|
});
|
|
576
497
|
}
|
|
577
|
-
|
|
578
498
|
export function mdToDraftjs(mdx) {
|
|
579
|
-
const entities = {};
|
|
499
|
+
const entities = {};
|
|
580
500
|
|
|
501
|
+
// Start really high to avoid conflicts with keys created by markdown-draft-js
|
|
581
502
|
let currentEntityKey = 1000000;
|
|
582
|
-
|
|
583
503
|
function entityKeyGenerator() {
|
|
584
504
|
return currentEntityKey++;
|
|
585
505
|
}
|
|
586
|
-
|
|
587
506
|
const result = fromMd(mdx, {}, {
|
|
588
507
|
htmlblock(item) {
|
|
589
508
|
if (item === undefined) {
|
|
590
509
|
return getDraftjsEmpty();
|
|
591
510
|
}
|
|
592
|
-
|
|
593
511
|
const dom = parseDOM(item.content);
|
|
594
512
|
const image = htmlDomToDraftjsImage(dom, item, entityKeyGenerator);
|
|
595
|
-
|
|
596
513
|
if (image) {
|
|
597
514
|
Object.assign(entities, image.entities);
|
|
598
515
|
return image.contentBlock;
|
|
599
516
|
}
|
|
600
|
-
|
|
601
517
|
const oembed = htmlDomToOembed(dom, item, entityKeyGenerator);
|
|
602
|
-
|
|
603
518
|
if (oembed) {
|
|
604
519
|
Object.assign(entities, oembed.entities);
|
|
605
520
|
return oembed.contentBlock;
|
|
606
521
|
}
|
|
607
|
-
|
|
608
522
|
const pullquote = htmlDomToPullquote(dom, item);
|
|
609
|
-
|
|
610
523
|
if (pullquote) {
|
|
611
524
|
Object.assign(entities, pullquote.entities);
|
|
612
525
|
return pullquote.contentBlock;
|
|
613
526
|
}
|
|
614
|
-
|
|
615
527
|
return getDraftjsEmpty(item.level);
|
|
616
528
|
}
|
|
617
|
-
|
|
618
529
|
});
|
|
619
|
-
result.entityMap = {
|
|
530
|
+
result.entityMap = {
|
|
531
|
+
...result.entityMap,
|
|
620
532
|
...entities
|
|
621
533
|
};
|
|
622
534
|
return result;
|
|
@@ -624,7 +536,6 @@ export function mdToDraftjs(mdx) {
|
|
|
624
536
|
export function getImagePathFromUrl(url) {
|
|
625
537
|
return new Url(url).pathname.substr(1);
|
|
626
538
|
}
|
|
627
|
-
|
|
628
539
|
function mdxToBr(item) {
|
|
629
540
|
if (item.content.startsWith('<br/>')) {
|
|
630
541
|
return {
|
|
@@ -637,7 +548,6 @@ function mdxToBr(item) {
|
|
|
637
548
|
};
|
|
638
549
|
}
|
|
639
550
|
}
|
|
640
|
-
|
|
641
551
|
function mdxToLinkData(item) {
|
|
642
552
|
if (item.content.startsWith('<TSExternalLink')) {
|
|
643
553
|
const dom = parseDOM(item.content)[0];
|
|
@@ -648,7 +558,6 @@ function mdxToLinkData(item) {
|
|
|
648
558
|
};
|
|
649
559
|
}
|
|
650
560
|
}
|
|
651
|
-
|
|
652
561
|
function mdxToDraftjsImage(item, entityKeyGenerator) {
|
|
653
562
|
if (item.content.startsWith(`<${mdxShortcodePrefix('Image')}`)) {
|
|
654
563
|
const dom = parseDOM(item.content)[0];
|
|
@@ -666,7 +575,6 @@ function mdxToDraftjsImage(item, entityKeyGenerator) {
|
|
|
666
575
|
});
|
|
667
576
|
}
|
|
668
577
|
}
|
|
669
|
-
|
|
670
578
|
function mdxToDraftjsPullquote(item) {
|
|
671
579
|
if (item.content.startsWith(`<${mdxShortcodePrefix('Pullquote')}`)) {
|
|
672
580
|
const dom = parseDOM(item.content)[0];
|
|
@@ -676,7 +584,6 @@ function mdxToDraftjsPullquote(item) {
|
|
|
676
584
|
});
|
|
677
585
|
}
|
|
678
586
|
}
|
|
679
|
-
|
|
680
587
|
/**
|
|
681
588
|
* Mutates result to replace empty entities with links,
|
|
682
589
|
* assuming there is nothing else they could be...
|
|
@@ -684,35 +591,27 @@ function mdxToDraftjsPullquote(item) {
|
|
|
684
591
|
function addLinks(state, links) {
|
|
685
592
|
let linkNumber = 0;
|
|
686
593
|
let linkOpen = false;
|
|
687
|
-
|
|
688
594
|
for (const block of state.blocks) {
|
|
689
595
|
let blockIsCustom = false;
|
|
690
|
-
|
|
691
596
|
for (const [i, entityRange] of block.entityRanges.entries()) {
|
|
692
597
|
var _entity$data9;
|
|
693
|
-
|
|
694
598
|
const entity = state.entityMap[entityRange.key];
|
|
695
|
-
|
|
696
599
|
if (entity === undefined) {
|
|
697
600
|
throw new Error('Missing entity');
|
|
698
601
|
}
|
|
699
|
-
|
|
700
602
|
if (i === 0 && (_entity$data9 = entity.data) !== null && _entity$data9 !== void 0 && _entity$data9.marker) {
|
|
701
603
|
blockIsCustom = true;
|
|
702
604
|
continue;
|
|
703
605
|
}
|
|
704
|
-
|
|
705
606
|
if (blockIsCustom && i === Object.keys(block.entityRanges).length - 1) {
|
|
706
607
|
continue;
|
|
707
|
-
}
|
|
708
|
-
|
|
608
|
+
}
|
|
709
609
|
|
|
610
|
+
// Ignore line breaks that were inserted around blocks
|
|
710
611
|
if (entity.depth === undefined) {
|
|
711
612
|
continue;
|
|
712
613
|
}
|
|
713
|
-
|
|
714
614
|
const link = links[linkNumber];
|
|
715
|
-
|
|
716
615
|
if (link && !linkOpen) {
|
|
717
616
|
entity.type = 'LINK';
|
|
718
617
|
entity.mutability = 'MUTABLE';
|
|
@@ -722,66 +621,54 @@ function addLinks(state, links) {
|
|
|
722
621
|
entityRange.length += decodedText.length;
|
|
723
622
|
linkNumber++;
|
|
724
623
|
}
|
|
725
|
-
|
|
726
624
|
linkOpen = !linkOpen;
|
|
727
625
|
}
|
|
728
626
|
}
|
|
729
627
|
}
|
|
628
|
+
|
|
730
629
|
/**
|
|
731
630
|
* Mutate state to replace blocks with our custom versions
|
|
732
631
|
* Return a list of entity keys that should be removed
|
|
733
632
|
*/
|
|
734
|
-
|
|
735
|
-
|
|
736
633
|
function replaceBlocks(state, replacementBlocks) {
|
|
737
634
|
let entityKeysToExclude = [];
|
|
738
|
-
|
|
739
635
|
for (let i = 0; i < state.blocks.length; i++) {
|
|
740
636
|
const block = state.blocks[i];
|
|
741
|
-
|
|
742
637
|
for (const entityKey of Object.keys(state.entityMap)) {
|
|
743
638
|
let removeBlockEntityKeys = false;
|
|
744
639
|
const blockEntityKeys = block.entityRanges.map(range => range.key);
|
|
745
|
-
|
|
746
640
|
if (blockEntityKeys.includes(Number(entityKey))) {
|
|
747
641
|
var _entity$data10;
|
|
748
|
-
|
|
749
642
|
const entity = state.entityMap[entityKey];
|
|
750
643
|
const markerKey = (_entity$data10 = entity.data) === null || _entity$data10 === void 0 ? void 0 : _entity$data10.marker;
|
|
751
644
|
const replacementBlock = replacementBlocks[markerKey];
|
|
752
|
-
|
|
753
645
|
if (replacementBlock && !removeBlockEntityKeys) {
|
|
754
646
|
const originalBlock = state.blocks[i];
|
|
755
647
|
const originalBlockText = state.blocks[i].text;
|
|
756
648
|
replacementBlock.text = originalBlockText !== '' ? originalBlockText : replacementBlock.text;
|
|
757
649
|
replacementBlock.inlineStyleRanges = originalBlock.inlineStyleRanges;
|
|
758
|
-
const removeBlockEntities = entity.data.type === 'image' || entity.data.type === 'oembed';
|
|
759
|
-
|
|
650
|
+
const removeBlockEntities = entity.data.type === 'image' || entity.data.type === 'oembed';
|
|
651
|
+
// eslint-disable-next-line max-depth
|
|
760
652
|
if (!removeBlockEntities) {
|
|
761
653
|
replacementBlock.entityRanges = originalBlock.entityRanges.filter(entityRange => {
|
|
762
654
|
var _state$entityMap$enti;
|
|
763
|
-
|
|
764
655
|
return ((_state$entityMap$enti = state.entityMap[entityRange.key].data) === null || _state$entityMap$enti === void 0 ? void 0 : _state$entityMap$enti.marker) === undefined;
|
|
765
656
|
});
|
|
766
657
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
658
|
+
state.blocks[i] = replacementBlock;
|
|
659
|
+
// eslint-disable-next-line max-depth
|
|
770
660
|
if (removeBlockEntities) {
|
|
771
661
|
removeBlockEntityKeys = true;
|
|
772
662
|
}
|
|
773
663
|
}
|
|
774
664
|
}
|
|
775
|
-
|
|
776
665
|
if (removeBlockEntityKeys) {
|
|
777
666
|
entityKeysToExclude = entityKeysToExclude.concat(blockEntityKeys);
|
|
778
667
|
}
|
|
779
668
|
}
|
|
780
669
|
}
|
|
781
|
-
|
|
782
670
|
return entityKeysToExclude;
|
|
783
671
|
}
|
|
784
|
-
|
|
785
672
|
const blockStarts = [{
|
|
786
673
|
regex: ' <TSImage',
|
|
787
674
|
replacement: ' <TSImage'
|
|
@@ -809,58 +696,51 @@ const blockEnds = [{
|
|
|
809
696
|
regex: '```',
|
|
810
697
|
replacement: '```'
|
|
811
698
|
}];
|
|
812
|
-
|
|
813
699
|
const getLookBehindValue = (end, str) => {
|
|
814
700
|
if (end.lookBehind) {
|
|
815
701
|
// eslint-disable-next-line security-node/non-literal-reg-expr
|
|
816
702
|
const behindMatch = new RegExp(end.lookBehind).exec(str);
|
|
817
|
-
|
|
818
703
|
if (behindMatch) {
|
|
819
704
|
return behindMatch[0];
|
|
820
705
|
}
|
|
821
706
|
}
|
|
822
|
-
|
|
823
707
|
return '';
|
|
824
708
|
};
|
|
709
|
+
|
|
825
710
|
/**
|
|
826
711
|
* Make sure there is a place to put the cursor around block-level items such as images and oembeds
|
|
827
712
|
*/
|
|
828
|
-
|
|
829
|
-
|
|
830
713
|
export function insertBreaksAroundBlocks(mdx) {
|
|
831
714
|
for (const start of blockStarts) {
|
|
832
715
|
// eslint-disable-next-line security-node/non-literal-reg-expr
|
|
833
716
|
mdx = mdx.replace(new RegExp(`^${start.regex}`, 'g'), `<br/>\n\n${start.replacement}`);
|
|
834
717
|
}
|
|
835
|
-
|
|
836
718
|
for (const end of blockEnds) {
|
|
837
719
|
// eslint-disable-next-line security-node/non-literal-reg-expr
|
|
838
720
|
mdx = mdx.replace(new RegExp(`${end.lookBehind ?? ''}${end.regex}\\s*$`, 'g'), match => {
|
|
839
721
|
return `${getLookBehindValue(end, match)}${end.replacement}\n\n<br/>`;
|
|
840
722
|
});
|
|
841
|
-
|
|
842
723
|
for (const start of blockStarts) {
|
|
843
|
-
mdx = mdx.replace(
|
|
724
|
+
mdx = mdx.replace(
|
|
725
|
+
// eslint-disable-next-line security-node/non-literal-reg-expr
|
|
844
726
|
new RegExp(`${end.lookBehind ?? ''}${end.regex}\\s*${start.regex}`, 'g'), match => {
|
|
845
727
|
return `${getLookBehindValue(end, match)}${end.replacement ?? 'FFF'}\n\n<br/>\n\n${start.replacement}`;
|
|
846
728
|
});
|
|
847
729
|
}
|
|
848
730
|
}
|
|
849
|
-
|
|
850
731
|
return mdx;
|
|
851
732
|
}
|
|
852
733
|
export function mdxToDraftjs(mdx) {
|
|
853
734
|
const replacementBlocks = {};
|
|
854
735
|
const entities = {};
|
|
855
736
|
const oembedKeyToHtml = {};
|
|
856
|
-
let currentOembedKey;
|
|
737
|
+
let currentOembedKey;
|
|
857
738
|
|
|
739
|
+
// Start really high to avoid conflicts with keys created by markdown-draft-js
|
|
858
740
|
let currentEntityKey = 1000000;
|
|
859
|
-
|
|
860
741
|
function entityKeyGenerator() {
|
|
861
742
|
return currentEntityKey++;
|
|
862
743
|
}
|
|
863
|
-
|
|
864
744
|
mdx = insertBreaksAroundBlocks(mdx);
|
|
865
745
|
const links = [];
|
|
866
746
|
const result = fromMd(mdx, {
|
|
@@ -868,21 +748,15 @@ export function mdxToDraftjs(mdx) {
|
|
|
868
748
|
if (item === undefined) {
|
|
869
749
|
return getDraftjsEmpty();
|
|
870
750
|
}
|
|
871
|
-
|
|
872
751
|
const br = mdxToBr(item);
|
|
873
|
-
|
|
874
752
|
if (br) {
|
|
875
753
|
return br;
|
|
876
754
|
}
|
|
877
|
-
|
|
878
755
|
const linkData = mdxToLinkData(item);
|
|
879
|
-
|
|
880
756
|
if (linkData) {
|
|
881
757
|
links.push(linkData);
|
|
882
758
|
}
|
|
883
|
-
|
|
884
759
|
const image = mdxToDraftjsImage(item, entityKeyGenerator);
|
|
885
|
-
|
|
886
760
|
if (image) {
|
|
887
761
|
replacementBlocks[image.contentBlock.key] = image.contentBlock;
|
|
888
762
|
Object.assign(entities, image.entities);
|
|
@@ -893,9 +767,7 @@ export function mdxToDraftjs(mdx) {
|
|
|
893
767
|
}
|
|
894
768
|
};
|
|
895
769
|
}
|
|
896
|
-
|
|
897
770
|
const oembed = mdxToDraftjsOembed(item, entityKeyGenerator);
|
|
898
|
-
|
|
899
771
|
if (oembed) {
|
|
900
772
|
currentOembedKey = Object.keys(oembed.entities)[0];
|
|
901
773
|
replacementBlocks[oembed.contentBlock.key] = oembed.contentBlock;
|
|
@@ -907,9 +779,7 @@ export function mdxToDraftjs(mdx) {
|
|
|
907
779
|
}
|
|
908
780
|
};
|
|
909
781
|
}
|
|
910
|
-
|
|
911
782
|
const pullquote = mdxToDraftjsPullquote(item);
|
|
912
|
-
|
|
913
783
|
if (pullquote) {
|
|
914
784
|
replacementBlocks[pullquote.contentBlock.key] = pullquote.contentBlock;
|
|
915
785
|
Object.assign(entities, pullquote.entities);
|
|
@@ -920,20 +790,16 @@ export function mdxToDraftjs(mdx) {
|
|
|
920
790
|
}
|
|
921
791
|
};
|
|
922
792
|
}
|
|
923
|
-
|
|
924
793
|
return getDraftjsEmpty(item.level);
|
|
925
794
|
}
|
|
926
|
-
|
|
927
795
|
}, {
|
|
928
796
|
htmlblock(item) {
|
|
929
797
|
if (item && currentOembedKey) {
|
|
930
798
|
oembedKeyToHtml[currentOembedKey] = item.content.replace(`</${mdxShortcodePrefix('Oembed')}>\n`, '');
|
|
931
799
|
currentOembedKey = undefined;
|
|
932
800
|
}
|
|
933
|
-
|
|
934
801
|
return getDraftjsRemovalMarker();
|
|
935
802
|
}
|
|
936
|
-
|
|
937
803
|
});
|
|
938
804
|
addLinks(result, links);
|
|
939
805
|
const entityKeysToExclude = replaceBlocks(result, replacementBlocks);
|
|
@@ -941,11 +807,11 @@ export function mdxToDraftjs(mdx) {
|
|
|
941
807
|
forEach(oembedKeyToHtml, (html, key) => {
|
|
942
808
|
entities[key].data.html = html;
|
|
943
809
|
});
|
|
944
|
-
result.entityMap = pickBy({
|
|
810
|
+
result.entityMap = pickBy({
|
|
811
|
+
...result.entityMap,
|
|
945
812
|
...entities
|
|
946
813
|
}, (entity, key) => {
|
|
947
814
|
var _entity$data11;
|
|
948
|
-
|
|
949
815
|
return ((_entity$data11 = entity.data) === null || _entity$data11 === void 0 ? void 0 : _entity$data11.marker) === undefined && !entityKeysToExclude.includes(Number(key));
|
|
950
816
|
});
|
|
951
817
|
return result;
|