From 446528156907ff26f2429abfbcf2ece277ba5964 Mon Sep 17 00:00:00 2001 From: xiemiao <1345888598@qq.com> Date: Tue, 26 May 2026 18:20:19 +0800 Subject: [PATCH] Render unsupported blocks with a warning --- README.md | 1 + src/block.test.tsx | 30 ++++++++++++++++++++++++++++++ src/block.tsx | 16 +++++++++++----- src/styles.css | 8 ++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/block.test.tsx diff --git a/README.md b/README.md index 2d26f27..23863a4 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ List of pages that implement this library. ## Supported Blocks Most common block types are supported. We happily accept pull requests to add support for the missing blocks. +Unsupported block types render an explicit warning so pages do not fail silently. | Block Type | Supported | Notes | | ----------------- | ---------- | ------------------------------------------------------------------------------------- | diff --git a/src/block.test.tsx b/src/block.test.tsx new file mode 100644 index 0000000..782d106 --- /dev/null +++ b/src/block.test.tsx @@ -0,0 +1,30 @@ +import * as React from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { NotionRenderer } from "./renderer"; + +test("renders a clear message for unsupported block types", () => { + const blockMap = { + unsupported: { + role: "reader", + value: { + id: "unsupported", + version: 1, + type: "database", + created_time: 0, + last_edited_time: 0, + parent_id: "", + parent_table: "block", + alive: true, + created_by_table: "notion_user", + created_by_id: "", + last_edited_by_table: "notion_user", + last_edited_by_id: "" + } + } + } as any; + + const html = renderToStaticMarkup(); + + expect(html).toContain("Unsupported Notion block type: database"); + expect(html).toContain("notion-unsupported-block"); +}); diff --git a/src/block.tsx b/src/block.tsx index 919ddf4..8219bc2 100644 --- a/src/block.tsx +++ b/src/block.tsx @@ -7,7 +7,6 @@ import { MapPageUrl, MapImageUrl, CustomBlockComponents, - BlockValueProp, CustomDecoratorComponents, CustomDecoratorComponentProps } from "./types"; @@ -516,10 +515,15 @@ export const Block: React.FC = props => { ); default: + const unsupportedType = block?.value?.type || "unknown"; if (process.env.NODE_ENV !== "production") { - console.log("Unsupported type " + block?.value?.type); + console.warn("Unsupported Notion block type: " + unsupportedType); } - return
; + return ( +
+ Unsupported Notion block type: {unsupportedType} +
+ ); } return null; }; @@ -531,12 +535,14 @@ export const Block: React.FC = props => { // Do not use custom component for base page block level !== 0 ) { - const CustomComponent = customBlockComponents[blockValue?.type]!; + const CustomComponent = customBlockComponents[blockValue?.type]! as React.FC< + any + >; return ( } + blockValue={blockValue} level={level} > {children} diff --git a/src/styles.css b/src/styles.css index da0e210..387a733 100644 --- a/src/styles.css +++ b/src/styles.css @@ -197,6 +197,14 @@ img.notion-page-icon-offset { .notion-small-text { font-size: 14px; } +.notion-unsupported-block { + padding: 0.75em 1em; + border: 1px solid rgba(224, 62, 62, 0.35); + border-radius: 3px; + color: rgb(224, 62, 62); + background: rgb(251, 228, 228); + font-size: 0.875em; +} .notion-quote { white-space: pre-wrap; word-break: break-word;