fix: Nested lists

This commit is contained in:
Tom Moor committed 2025-03-11 20:42:41 -04:00
1 parent 3504f0a0ff
commit dda384fb91
2 files changed
+215 -4

No files matched your search

+19 -4
View File
@@ -26,7 +26,7 @@ import isArray from "lodash/isArray";
import { MentionType, ProsemirrorData } from "@shared/types";
import Logger from "@server/logging/Logger";
export type NotionBlock = BlockObjectResponse & {
export type NotionBlock<T = BlockObjectResponse> = T & {
children: NotionBlock[];
};
@@ -88,6 +88,10 @@ export class NotionConverter {
let wrappingList;
const children = [] as ProsemirrorData[];
if (!item.children) {
return [];
}
for (const child of item.children) {
const mapped = mapChild(child);
if (!mapped) {
@@ -137,6 +141,10 @@ export class NotionConverter {
children.push(...(isArray(mapped) ? mapped : [mapped]));
}
if (wrappingList) {
children.push(wrappingList);
}
return children;
}
@@ -165,7 +173,9 @@ export class NotionConverter {
return undefined;
}
private static bulleted_list_item(item: BulletedListItemBlockObjectResponse) {
private static bulleted_list_item(
item: NotionBlock<BulletedListItemBlockObjectResponse>
) {
return {
type: "list_item",
content: [
@@ -173,6 +183,7 @@ export class NotionConverter {
type: "paragraph",
content: item.bulleted_list_item.rich_text.map(this.rich_text),
},
...this.mapChildren(item),
],
};
}
@@ -192,7 +203,9 @@ export class NotionConverter {
};
}
private static numbered_list_item(item: NumberedListItemBlockObjectResponse) {
private static numbered_list_item(
item: NotionBlock<NumberedListItemBlockObjectResponse>
) {
return {
type: "list_item",
content: [
@@ -200,6 +213,7 @@ export class NotionConverter {
type: "paragraph",
content: item.numbered_list_item.rich_text.map(this.rich_text),
},
...this.mapChildren(item),
],
};
}
@@ -414,7 +428,7 @@ export class NotionConverter {
};
}
private static to_do(item: ToDoBlockObjectResponse) {
private static to_do(item: NotionBlock<ToDoBlockObjectResponse>) {
return {
type: "checkbox_item",
attrs: {
@@ -425,6 +439,7 @@ export class NotionConverter {
type: "paragraph",
content: item.to_do.rich_text.map(this.rich_text),
},
...this.mapChildren(item),
],
};
}
@@ -102,6 +102,46 @@ exports[`NotionConverter converts a page 1`] = `
],
"type": "paragraph",
},
{
"content": [
{
"content": [
{
"content": [
{
"marks": [],
"text": "Bullet list item 2.1",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [
{
"content": [
{
"content": [
{
"marks": [],
"text": "Ordered list item 2.1.1",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "list_item",
},
],
"type": "ordered_list",
},
],
"type": "list_item",
},
],
"type": "bullet_list",
},
],
"type": "list_item",
},
@@ -970,6 +1010,76 @@ exports[`NotionConverter converts a page 1`] = `
],
"type": "paragraph",
},
{
"content": [
{
"content": [
{
"content": [
{
"marks": [],
"text": "Numbered list item 2.1",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "list_item",
},
{
"content": [
{
"content": [
{
"marks": [],
"text": "Numbered list item 2.2",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [
{
"content": [
{
"content": [
{
"marks": [],
"text": "Bullet list item 2.2.1",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "list_item",
},
{
"content": [
{
"content": [
{
"marks": [],
"text": "Bullet list item 2.2.2",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "list_item",
},
],
"type": "bullet_list",
},
],
"type": "list_item",
},
],
"type": "ordered_list",
},
],
"type": "list_item",
},
@@ -1326,6 +1436,92 @@ exports[`NotionConverter converts a page 1`] = `
],
"type": "paragraph",
},
{
"content": [
{
"attrs": {
"checked": false,
},
"content": [
{
"content": [
{
"marks": [],
"text": "Todo item 2.1",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [
{
"content": [
{
"content": [
{
"marks": [],
"text": "Numbered item 2.1.1",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "list_item",
},
{
"content": [
{
"content": [
{
"marks": [],
"text": "bulleted item 2.1.2",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "list_item",
},
],
"type": "ordered_list",
},
],
"type": "checkbox_item",
},
{
"attrs": {
"checked": false,
},
"content": [
{
"content": [
{
"marks": [],
"text": "Todo item 2.2",
"type": "text",
},
],
"type": "paragraph",
},
{
"content": [
{
"marks": [],
"text": "Nested content inside todo item 2.2.",
"type": "text",
},
],
"type": "paragraph",
},
],
"type": "checkbox_item",
},
],
"type": "checkbox_list",
},
],
"type": "checkbox_item",
},