fix: Handle empty text blocks from Notion response (#8785)

This commit is contained in:
Hemachandar
2025-03-25 17:01:06 +05:30
committed by GitHub
parent ab55e0bed9
commit f6da244c33
4 changed files with 682 additions and 15 deletions

View File

@@ -1,12 +1,22 @@
import { Node } from "prosemirror-model";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import data from "@server/test/fixtures/notion-page.json";
import nodesWithEmptyTextNode from "@server/test/fixtures/notion-page-with-empty-text-nodes.json";
import allNodes from "@server/test/fixtures/notion-page.json";
import { NotionConverter, NotionPage } from "./NotionConverter";
describe("NotionConverter", () => {
it("converts a page", () => {
const response = NotionConverter.page({
children: data,
children: allNodes,
} as NotionPage);
expect(response).toMatchSnapshot();
expect(ProsemirrorHelper.toProsemirror(response)).toBeInstanceOf(Node);
});
it("converts a page with empty text nodes", () => {
const response = NotionConverter.page({
children: nodesWithEmptyTextNode,
} as NotionPage);
expect(response).toMatchSnapshot();

View File

@@ -179,11 +179,15 @@ export class NotionConverter {
}
private static bookmark(item: BookmarkBlockObjectResponse) {
const caption = item.bookmark.caption
.map(this.rich_text_to_plaintext)
.join("");
return {
type: "paragraph",
content: [
{
text: item.bookmark.caption.map(this.rich_text_to_plaintext).join(""),
text: caption || item.bookmark.url,
type: "text",
marks: [
{
@@ -221,17 +225,14 @@ export class NotionConverter {
}
private static code(item: CodeBlockObjectResponse) {
const text = item.code.rich_text.map(this.rich_text_to_plaintext).join("");
return {
type: "code_fence",
attrs: {
language: item.code.language,
},
content: [
{
type: "text",
text: item.code.rich_text.map(this.rich_text_to_plaintext).join(""),
},
],
content: text ? [{ type: "text", text }] : undefined,
};
}
@@ -364,12 +365,14 @@ export class NotionConverter {
private static equation(item: EquationBlockObjectResponse) {
return {
type: "math_block",
content: [
{
type: "text",
text: item.equation.expression,
},
],
content: item.equation.expression
? [
{
type: "text",
text: item.equation.expression,
},
]
: undefined,
};
}

View File

@@ -1983,3 +1983,148 @@ exports[`NotionConverter converts a page 1`] = `
"type": "doc",
}
`;
exports[`NotionConverter converts a page with empty text nodes 1`] = `
{
"content": [
{
"content": [],
"type": "paragraph",
},
{
"attrs": {
"language": "javascript",
},
"content": undefined,
"type": "code_fence",
},
{
"content": [],
"type": "paragraph",
},
{
"content": [
{
"content": [
{
"text": "E",
"type": "text",
},
],
"type": "math_inline",
},
],
"type": "paragraph",
},
{
"content": [],
"type": "paragraph",
},
{
"content": [
{
"marks": [
{
"attrs": {
"href": "http://github.com/outline/",
},
"type": "link",
},
],
"text": "http://github.com/outline/",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [],
"type": "paragraph",
},
{
"content": [
{
"marks": [
{
"attrs": {
"href": "https://github.com/outline/outline",
},
"type": "link",
},
],
"text": "https://github.com/outline/outline",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [],
"type": "paragraph",
},
{
"content": undefined,
"type": "math_block",
},
{
"content": [],
"type": "paragraph",
},
{
"content": [
{
"marks": [
{
"attrs": {
"href": "https://google.com",
"title": null,
},
"type": "link",
},
],
"text": "https://google.com",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [],
"type": "paragraph",
},
{
"content": [
{
"marks": [
{
"attrs": {
"href": "https://github.com/outline/outline",
},
"type": "link",
},
],
"text": "https://github.com/outline/outline",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [],
"type": "paragraph",
},
{
"attrs": {
"href": "https://prod-files-secure.s3.us-west-2.amazonaws.com/2f3fcad6-fc32-434b-b6b2-a03ca7893c4d/49bfa851-95c1-458b-abb0-88ed591f7712/Empty_pdf.pdf",
"title": "",
},
"type": "attachment",
},
{
"content": [],
"type": "paragraph",
},
],
"type": "doc",
}
`;