@formepdf/react 0.23.0 → 0.24.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/serialize.js +108 -56
- package/package.json +2 -2
package/dist/serialize.js
CHANGED
|
@@ -479,27 +479,38 @@ function serializeText(element) {
|
|
|
479
479
|
node.bookmark = props.bookmark;
|
|
480
480
|
return node;
|
|
481
481
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
482
|
+
const MAIN_CTX = {
|
|
483
|
+
flatten: (c) => flattenChildren(c),
|
|
484
|
+
style: (s) => mapStyle(s),
|
|
485
|
+
textContent: (c) => flattenTextContent(c),
|
|
486
|
+
isRunChild: (c) => inlineDefaults(c.type) !== null,
|
|
487
|
+
buildRuns: (c) => buildTextRuns(c),
|
|
488
|
+
child: (c, parent) => serializeChild(c, parent),
|
|
489
|
+
sourceLocation: (e) => extractSourceLocation(e),
|
|
490
|
+
};
|
|
491
|
+
const TEMPLATE_CTX = {
|
|
492
|
+
flatten: (c) => flattenTemplateChildren(c),
|
|
493
|
+
style: (s) => mapTemplateStyle(s),
|
|
494
|
+
textContent: (c) => flattenTemplateTextContent(c),
|
|
495
|
+
// The template path's own convention for Text, kept exactly: a nested
|
|
496
|
+
// <Text> becomes a run. (<Strong>/<Em> inside a <Text> still flatten to
|
|
497
|
+
// plain content on this path — a fidelity gap, tracked separately, not
|
|
498
|
+
// the deletion this refactor fixes.)
|
|
499
|
+
isRunChild: (c) => c.type === Text,
|
|
500
|
+
buildRuns: (c) => buildTemplateTextRuns(c),
|
|
501
|
+
child: (c, parent) => serializeTemplateChild(c, parent),
|
|
502
|
+
sourceLocation: () => undefined,
|
|
503
|
+
};
|
|
504
|
+
function buildHeading(element, level, ctx) {
|
|
490
505
|
const props = element.props;
|
|
491
|
-
const childElements =
|
|
492
|
-
const
|
|
493
|
-
const kind = {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
content: '',
|
|
497
|
-
};
|
|
498
|
-
if (hasInlineChild) {
|
|
499
|
-
kind.runs = buildTextRuns(props.children);
|
|
506
|
+
const childElements = ctx.flatten(props.children);
|
|
507
|
+
const hasRunChild = childElements.some((c) => isValidElement(c) && ctx.isRunChild(c));
|
|
508
|
+
const kind = { type: 'Heading', level, content: '' };
|
|
509
|
+
if (hasRunChild) {
|
|
510
|
+
kind.runs = ctx.buildRuns(props.children);
|
|
500
511
|
}
|
|
501
512
|
else {
|
|
502
|
-
kind.content =
|
|
513
|
+
kind.content = ctx.textContent(props.children);
|
|
503
514
|
}
|
|
504
515
|
if (props.href)
|
|
505
516
|
kind.href = props.href;
|
|
@@ -507,15 +518,17 @@ function serializeHeading(element, level) {
|
|
|
507
518
|
const mergedStyle = { ...HEADING_DEFAULTS[level], ...(props.style || {}) };
|
|
508
519
|
const node = {
|
|
509
520
|
kind,
|
|
510
|
-
style:
|
|
521
|
+
style: ctx.style(mergedStyle),
|
|
511
522
|
children: [],
|
|
512
|
-
sourceLocation: extractSourceLocation(element),
|
|
513
523
|
};
|
|
524
|
+
const loc = ctx.sourceLocation(element);
|
|
525
|
+
if (loc !== undefined)
|
|
526
|
+
node.sourceLocation = loc;
|
|
514
527
|
if (props.bookmark)
|
|
515
528
|
node.bookmark = props.bookmark;
|
|
516
529
|
return node;
|
|
517
530
|
}
|
|
518
|
-
function
|
|
531
|
+
function buildList(element, ordered, ctx) {
|
|
519
532
|
const props = element.props;
|
|
520
533
|
const markerType = ordered
|
|
521
534
|
? mapListMarker(props.type, 'decimal')
|
|
@@ -524,48 +537,62 @@ function serializeList(element, ordered) {
|
|
|
524
537
|
// Children must be ListItems — anything else is silently dropped to
|
|
525
538
|
// keep the serializer tolerant. (We don't throw on stray content because
|
|
526
539
|
// a Fragment / null child in JSX is too common to be a real error.)
|
|
527
|
-
const childElements =
|
|
528
|
-
const children = childElements.map((c) =>
|
|
540
|
+
const childElements = ctx.flatten(props.children).filter((c) => isValidElement(c) && c.type === ListItem);
|
|
541
|
+
const children = childElements.map((c) => buildListItem(c, ctx));
|
|
529
542
|
const node = {
|
|
530
543
|
kind: { type: 'List', ordered, marker_type: markerType, start },
|
|
531
|
-
style:
|
|
544
|
+
style: ctx.style(props.style),
|
|
532
545
|
children,
|
|
533
|
-
sourceLocation: extractSourceLocation(element),
|
|
534
546
|
};
|
|
547
|
+
const loc = ctx.sourceLocation(element);
|
|
548
|
+
if (loc !== undefined)
|
|
549
|
+
node.sourceLocation = loc;
|
|
535
550
|
if (props.bookmark)
|
|
536
551
|
node.bookmark = props.bookmark;
|
|
537
552
|
return node;
|
|
538
553
|
}
|
|
539
|
-
function
|
|
554
|
+
function buildListItem(element, ctx) {
|
|
540
555
|
const props = element.props;
|
|
541
556
|
// ListItem content is layed out by the engine using layout_children, so
|
|
542
557
|
// we serialize whatever the user put inside as the node's children. This
|
|
543
558
|
// covers plain strings, JSX text, nested lists, formatted runs via
|
|
544
559
|
// <Text>, etc.
|
|
545
|
-
const rawChildren =
|
|
560
|
+
const rawChildren = ctx.flatten(props.children);
|
|
546
561
|
const children = [];
|
|
547
562
|
for (const c of rawChildren) {
|
|
548
563
|
if (typeof c === 'string' || typeof c === 'number') {
|
|
549
564
|
// Auto-wrap raw strings in a Text node so the engine has something
|
|
550
565
|
// concrete to render. Matches the convention everywhere else.
|
|
551
566
|
children.push({
|
|
552
|
-
kind: { type: 'Text', content:
|
|
567
|
+
kind: { type: 'Text', content: ctx.textContent(c) },
|
|
553
568
|
style: {},
|
|
554
569
|
children: [],
|
|
555
570
|
});
|
|
556
571
|
}
|
|
557
572
|
else if (isValidElement(c)) {
|
|
558
|
-
const node =
|
|
573
|
+
const node = ctx.child(c, null);
|
|
559
574
|
if (node)
|
|
560
575
|
children.push(node);
|
|
561
576
|
}
|
|
562
577
|
}
|
|
563
|
-
|
|
578
|
+
const node = {
|
|
564
579
|
kind: { type: 'ListItem' },
|
|
565
|
-
style:
|
|
580
|
+
style: ctx.style(props.style),
|
|
566
581
|
children,
|
|
567
|
-
sourceLocation: extractSourceLocation(element),
|
|
568
582
|
};
|
|
583
|
+
const loc = ctx.sourceLocation(element);
|
|
584
|
+
if (loc !== undefined)
|
|
585
|
+
node.sourceLocation = loc;
|
|
586
|
+
return node;
|
|
587
|
+
}
|
|
588
|
+
function serializeHeading(element, level) {
|
|
589
|
+
return buildHeading(element, level, MAIN_CTX);
|
|
590
|
+
}
|
|
591
|
+
function serializeList(element, ordered) {
|
|
592
|
+
return buildList(element, ordered, MAIN_CTX);
|
|
593
|
+
}
|
|
594
|
+
function serializeListItem(element) {
|
|
595
|
+
return buildListItem(element, MAIN_CTX);
|
|
569
596
|
}
|
|
570
597
|
function serializeImage(element) {
|
|
571
598
|
const props = element.props;
|
|
@@ -1115,6 +1142,28 @@ function serializeTemplateChild(child, parent = null) {
|
|
|
1115
1142
|
return serializeTemplateView(element, parent);
|
|
1116
1143
|
if (element.type === Text)
|
|
1117
1144
|
return serializeTemplateText(element);
|
|
1145
|
+
// Headings and lists reached this dispatch with no arm at all: they are
|
|
1146
|
+
// function components returning null, so they fell through to the
|
|
1147
|
+
// "unknown function component" fallback below and were deleted along with
|
|
1148
|
+
// their subtrees. Same builders as `serialize()`, different context.
|
|
1149
|
+
if (element.type === H1)
|
|
1150
|
+
return buildHeading(element, 1, TEMPLATE_CTX);
|
|
1151
|
+
if (element.type === H2)
|
|
1152
|
+
return buildHeading(element, 2, TEMPLATE_CTX);
|
|
1153
|
+
if (element.type === H3)
|
|
1154
|
+
return buildHeading(element, 3, TEMPLATE_CTX);
|
|
1155
|
+
if (element.type === H4)
|
|
1156
|
+
return buildHeading(element, 4, TEMPLATE_CTX);
|
|
1157
|
+
if (element.type === H5)
|
|
1158
|
+
return buildHeading(element, 5, TEMPLATE_CTX);
|
|
1159
|
+
if (element.type === H6)
|
|
1160
|
+
return buildHeading(element, 6, TEMPLATE_CTX);
|
|
1161
|
+
if (element.type === OrderedList)
|
|
1162
|
+
return buildList(element, true, TEMPLATE_CTX);
|
|
1163
|
+
if (element.type === UnorderedList)
|
|
1164
|
+
return buildList(element, false, TEMPLATE_CTX);
|
|
1165
|
+
if (element.type === ListItem)
|
|
1166
|
+
return buildListItem(element, TEMPLATE_CTX);
|
|
1118
1167
|
if (element.type === Image)
|
|
1119
1168
|
return serializeTemplateImage(element);
|
|
1120
1169
|
if (element.type === Table)
|
|
@@ -1188,36 +1237,39 @@ function serializeTemplateView(element, _parent = null) {
|
|
|
1188
1237
|
node.href = props.href;
|
|
1189
1238
|
return node;
|
|
1190
1239
|
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Formatted runs on the template path. Extracted from `serializeTemplateText`
|
|
1242
|
+
* so headings can build runs the same way text does — the two used to be one
|
|
1243
|
+
* inline block, which is why headings had no way to produce runs at all.
|
|
1244
|
+
*/
|
|
1245
|
+
function buildTemplateTextRuns(children) {
|
|
1246
|
+
const runs = [];
|
|
1247
|
+
for (const child of flattenTemplateChildren(children)) {
|
|
1248
|
+
if (typeof child === 'string' || typeof child === 'number') {
|
|
1249
|
+
const processed = typeof child === 'string' ? processTemplateString(child) : null;
|
|
1250
|
+
runs.push({ content: processed !== null ? processed : String(child) });
|
|
1251
|
+
}
|
|
1252
|
+
else if (isValidElement(child) && child.type === Text) {
|
|
1253
|
+
const childProps = child.props;
|
|
1254
|
+
const run = {
|
|
1255
|
+
content: flattenTemplateTextContent(childProps.children),
|
|
1256
|
+
};
|
|
1257
|
+
if (childProps.style)
|
|
1258
|
+
run.style = mapTemplateStyle(childProps.style);
|
|
1259
|
+
if (childProps.href)
|
|
1260
|
+
run.href = childProps.href;
|
|
1261
|
+
runs.push(run);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
return runs;
|
|
1265
|
+
}
|
|
1191
1266
|
function serializeTemplateText(element) {
|
|
1192
1267
|
const props = element.props;
|
|
1193
1268
|
const childElements = flattenTemplateChildren(props.children);
|
|
1194
1269
|
const hasTextChild = childElements.some(c => isValidElement(c) && c.type === Text);
|
|
1195
1270
|
const kind = { type: 'Text', content: '' };
|
|
1196
1271
|
if (hasTextChild) {
|
|
1197
|
-
|
|
1198
|
-
for (const child of childElements) {
|
|
1199
|
-
if (typeof child === 'string' || typeof child === 'number') {
|
|
1200
|
-
const processed = typeof child === 'string' ? processTemplateString(child) : null;
|
|
1201
|
-
if (processed !== null) {
|
|
1202
|
-
runs.push({ content: processed });
|
|
1203
|
-
}
|
|
1204
|
-
else {
|
|
1205
|
-
runs.push({ content: String(child) });
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
else if (isValidElement(child) && child.type === Text) {
|
|
1209
|
-
const childProps = child.props;
|
|
1210
|
-
const run = {
|
|
1211
|
-
content: flattenTemplateTextContent(childProps.children),
|
|
1212
|
-
};
|
|
1213
|
-
if (childProps.style)
|
|
1214
|
-
run.style = mapTemplateStyle(childProps.style);
|
|
1215
|
-
if (childProps.href)
|
|
1216
|
-
run.href = childProps.href;
|
|
1217
|
-
runs.push(run);
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1220
|
-
kind.runs = runs;
|
|
1272
|
+
kind.runs = buildTemplateTextRuns(props.children);
|
|
1221
1273
|
}
|
|
1222
1274
|
else {
|
|
1223
1275
|
kind.content = flattenTemplateTextContent(props.children);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formepdf/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "JSX-to-JSON serializer for Forme PDF engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"test:watch": "vitest"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@formepdf/shared": "0.
|
|
21
|
+
"@formepdf/shared": "0.24.0"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"react": "^18 || ^19"
|