Skip to content

Commit 52e425e

Browse files
committed
rich text logging improvements
1 parent d8e9c87 commit 52e425e

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

packages/basehub/src/react/rich-text/primitive.tsx

+54-20
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export type Node =
4343
| "taskList"
4444
| "blockquote"
4545
| "table"
46-
| "tableRow";
46+
| "tableRow"
47+
| "tableBody";
4748
attrs?: Attrs;
4849
marks?: Array<Mark>;
4950
content?: Array<Node>;
@@ -239,7 +240,6 @@ export const RichText = <
239240
key={index}
240241
components={props.components}
241242
blocks={props.blocks}
242-
level={0}
243243
slugger={slugger}
244244
/>
245245
);
@@ -331,14 +331,12 @@ const Node = ({
331331
components,
332332
blocks,
333333
parent,
334-
level,
335334
slugger,
336335
}: {
337336
node: Node;
338337
components?: Partial<Handlers>;
339338
blocks?: readonly CustomBlockBase[];
340339
parent?: Node;
341-
level: number;
342340
slugger: GithubSlugger;
343341
}) => {
344342
const children = node.content?.map((childNode, index) => {
@@ -349,7 +347,6 @@ const Node = ({
349347
key={index}
350348
components={components}
351349
blocks={blocks}
352-
level={level + 1}
353350
slugger={slugger}
354351
/>
355352
);
@@ -437,7 +434,23 @@ const Node = ({
437434
break;
438435
case "table":
439436
handler = components?.table ?? defaultHandlers.table;
440-
props = { children } satisfies ExtractPropsForHandler<Handlers["table"]>;
437+
438+
/**
439+
* In the case of table, we add a tableBody node that wraps its children, as it seems to be missing in the response.
440+
*/
441+
const overridenChildren = (
442+
<Node
443+
node={{ type: "tableBody", content: node.content }}
444+
parent={node}
445+
components={components}
446+
blocks={blocks}
447+
slugger={slugger}
448+
/>
449+
);
450+
451+
props = { children: overridenChildren } satisfies ExtractPropsForHandler<
452+
Handlers["table"]
453+
>;
441454
break;
442455
case "tableRow":
443456
handler = components?.tr ?? defaultHandlers.tr;
@@ -459,6 +472,14 @@ const Node = ({
459472
rowspan: node.attrs.rowspan,
460473
} satisfies ExtractPropsForHandler<Handlers["th"]>;
461474
break;
475+
case "tableFooter":
476+
handler = components?.tfoot ?? defaultHandlers.tfoot;
477+
props = { children } satisfies ExtractPropsForHandler<Handlers["tfoot"]>;
478+
break;
479+
case "tableBody":
480+
handler = components?.tbody ?? defaultHandlers.tbody;
481+
props = { children } satisfies ExtractPropsForHandler<Handlers["tbody"]>;
482+
break;
462483
case "image":
463484
handler = components?.img ?? defaultHandlers.img;
464485
props = {
@@ -481,23 +502,29 @@ const Node = ({
481502
break;
482503
case "basehub-block": {
483504
const block = blocks?.find((block: any) => {
505+
const typename = block?.__typename as string | undefined;
506+
const keysLength = Object.keys(block).length;
484507
const id = block?._id ?? block?._sys?.id;
485-
if (typeof id !== "string") {
508+
if (typeof id !== "string" && (!typename || keysLength > 1)) {
486509
if (isDev) {
487510
console.warn(
488-
`BaseHub RichText Error: make sure you send through the _id and the __typename for all custom blocks.`
511+
`BaseHub RichText Error: make sure you send through the _id and the __typename for all custom blocks.\nReceived ${JSON.stringify(
512+
block,
513+
null,
514+
2
515+
)}.`
489516
);
490517
}
491518
return false;
492519
}
493520
return id === node.attrs.id;
494521
});
495522
if (!block) {
496-
if (isDev) {
497-
console.warn(
498-
`BaseHub RichText Error: block "${node.attrs.id}" not found.`
499-
);
500-
}
523+
// if (isDev) {
524+
// console.warn(
525+
// `BaseHub RichText Error: block "${node.attrs.id}" not found.`
526+
// );
527+
// }
501528
break;
502529
}
503530
// @ts-ignore
@@ -577,22 +604,29 @@ const Marks = ({
577604
break;
578605
case "basehub-inline-block": {
579606
const block = blocks?.find((block: any) => {
607+
const typename = block?.__typename as string | undefined;
608+
const keysLength = Object.keys(block).length;
580609
const id = block?._id ?? block?._sys?.id;
581-
if (typeof id !== "string") {
610+
if (typeof id !== "string" && (!typename || keysLength > 1)) {
582611
if (isDev) {
583612
console.warn(
584-
`BaseHub RichText Error: make sure you send through the _id and the __typename for all custom blocks.`
613+
`BaseHub RichText Error: make sure you send through the _id and the __typename for all custom blocks.\nReceived ${JSON.stringify(
614+
block,
615+
null,
616+
2
617+
)}.`
585618
);
586619
}
620+
return false;
587621
}
588622
return id === mark.attrs.id;
589623
});
590624
if (!block) {
591-
if (isDev) {
592-
console.warn(
593-
`BaseHub RichText Error: block "${mark.attrs.id}" not found.`
594-
);
595-
}
625+
// if (isDev) {
626+
// console.warn(
627+
// `BaseHub RichText Error: block "${mark.attrs.id}" not found.`
628+
// );
629+
// }
596630
break;
597631
}
598632
handler =

0 commit comments

Comments
 (0)