Skip to content

Commit cd90895

Browse files
committed
Fixed most parse tests
1 parent 703f86e commit cd90895

File tree

10 files changed

+60
-57
lines changed

10 files changed

+60
-57
lines changed

packages/core/src/blocks/Code/block.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { HighlighterGeneric } from "@shikijs/types";
22
import { createBlockNoteExtension } from "../../editor/BlockNoteExtension.js";
33
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
44
import { lazyShikiPlugin } from "./shiki.js";
5+
import { DOMParser } from "@tiptap/pm/model";
56

67
export type CodeBlockOptions = {
78
/**
@@ -71,12 +72,35 @@ export const createCodeBlockSpec = createBlockSpec(
7172
createCodeBlockConfig,
7273
(options) => ({
7374
parse: (e) => {
74-
const pre = e.querySelector("pre");
75-
if (!pre) {
75+
if (e.tagName !== "PRE") {
7676
return undefined;
7777
}
7878

79-
return {};
79+
if (
80+
e.childElementCount !== 1 ||
81+
e.firstElementChild?.tagName !== "CODE"
82+
) {
83+
return undefined;
84+
}
85+
86+
const code = e.firstElementChild!;
87+
const language =
88+
code.getAttribute("data-language") ||
89+
code.className
90+
.split(" ")
91+
.find((name) => name.includes("language-"))
92+
?.replace("language-", "");
93+
94+
return { language };
95+
},
96+
97+
parseContent: ({ el, schema }) => {
98+
const parser = DOMParser.fromSchema(schema);
99+
const code = el.firstElementChild!;
100+
101+
return parser.parse(code, {
102+
topNode: schema.nodes["codeBlock"].create(),
103+
}).content;
80104
},
81105

82106
render(block, editor) {

packages/core/src/blocks/ListItem/NumberedListItem/block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export const createNumberedListItemBlockSpec = createBlockSpec(
3434
}
3535

3636
if (
37-
parent.tagName === "UL" ||
38-
(parent.tagName === "DIV" && parent.parentElement?.tagName === "UL")
37+
parent.tagName === "OL" ||
38+
(parent.tagName === "DIV" && parent.parentElement?.tagName === "OL")
3939
) {
4040
return {};
4141
}

packages/core/src/blocks/Paragraph/block.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ export const createParagraphBlockSpec = createBlockSpec(
1717
createParagraphBlockConfig,
1818
{
1919
parse: (e) => {
20-
const paragraph = e.querySelector("p");
21-
if (!paragraph) {
20+
if (e.tagName !== "P") {
2221
return undefined;
2322
}
2423

25-
return {};
24+
// Edge case for things like images directly inside paragraph.
25+
if (!e.textContent?.trim()) {
26+
return undefined;
27+
}
28+
29+
return undefined;
2630
},
2731
render: () => {
2832
const dom = document.createElement("p");

packages/core/src/blocks/Quote/block.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export const createQuoteBlockConfig = createBlockConfig(
88
() =>
99
({
1010
type: "quote" as const,
11-
propSchema: { ...defaultProps },
11+
propSchema: {
12+
backgroundColor: defaultProps.backgroundColor,
13+
textColor: defaultProps.textColor,
14+
},
1215
content: "inline" as const,
1316
}) as const,
1417
);
@@ -17,7 +20,7 @@ export const createQuoteBlockSpec = createBlockSpec(
1720
createQuoteBlockConfig,
1821
{
1922
parse(element) {
20-
if (element.querySelector("blockquote")) {
23+
if (element.tagName === "BLOCKQUOTE") {
2124
return {};
2225
}
2326

tests/src/unit/core/formatConversion/parse/__snapshots__/html/deepNestedContent.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,25 +273,13 @@
273273
"text": "All",
274274
"type": "text",
275275
},
276-
],
277-
"id": "13",
278-
"props": {
279-
"backgroundColor": "default",
280-
"textAlignment": "left",
281-
"textColor": "default",
282-
},
283-
"type": "paragraph",
284-
},
285-
{
286-
"children": [],
287-
"content": [
288276
{
289277
"styles": {},
290278
"text": " Outer 4 Div After Outer 3 Div After Outer 2 Div After Outer 1 Div After",
291279
"type": "text",
292280
},
293281
],
294-
"id": "14",
282+
"id": "13",
295283
"props": {
296284
"backgroundColor": "default",
297285
"textAlignment": "left",

tests/src/unit/core/formatConversion/parse/__snapshots__/html/googleDocs.json

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -612,29 +612,12 @@ Hard Break",
612612
"content": [
613613
{
614614
"styles": {},
615-
"text": "Paragraph",
616-
"type": "text",
617-
},
618-
],
619-
"id": "23",
620-
"props": {
621-
"backgroundColor": "default",
622-
"textAlignment": "left",
623-
"textColor": "default",
624-
},
625-
"type": "paragraph",
626-
},
627-
{
628-
"children": [],
629-
"content": [
630-
{
631-
"styles": {},
632-
"text": "
615+
"text": "Paragraph
633616
",
634617
"type": "text",
635618
},
636619
],
637-
"id": "24",
620+
"id": "23",
638621
"props": {
639622
"backgroundColor": "default",
640623
"textAlignment": "left",

tests/src/unit/core/formatConversion/parse/__snapshots__/html/lists.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"content": [
5656
{
5757
"styles": {},
58-
"text": " Fourth",
58+
"text": "Fourth",
5959
"type": "text",
6060
},
6161
],
@@ -73,7 +73,7 @@
7373
"content": [
7474
{
7575
"styles": {},
76-
"text": " Fifth",
76+
"text": "Fifth",
7777
"type": "text",
7878
},
7979
],
@@ -127,7 +127,7 @@
127127
"content": [
128128
{
129129
"styles": {},
130-
"text": " Child 3",
130+
"text": "Child 3",
131131
"type": "text",
132132
},
133133
],
@@ -145,7 +145,7 @@
145145
"content": [
146146
{
147147
"styles": {},
148-
"text": " Child 4",
148+
"text": "Child 4",
149149
"type": "text",
150150
},
151151
],

tests/src/unit/core/formatConversion/parse/__snapshots__/html/mixedNestedLists.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"content": [
110110
{
111111
"styles": {},
112-
"text": " Nested Check List Item",
112+
"text": "Nested Check List Item",
113113
"type": "text",
114114
},
115115
],
@@ -127,7 +127,7 @@
127127
"content": [
128128
{
129129
"styles": {},
130-
"text": " Nested Check List Item",
130+
"text": "Nested Check List Item",
131131
"type": "text",
132132
},
133133
],
@@ -178,7 +178,7 @@
178178
"content": [
179179
{
180180
"styles": {},
181-
"text": " Check List Item",
181+
"text": "Check List Item",
182182
"type": "text",
183183
},
184184
],
@@ -231,7 +231,7 @@
231231
"content": [
232232
{
233233
"styles": {},
234-
"text": " Check List Item",
234+
"text": "Check List Item",
235235
"type": "text",
236236
},
237237
],
@@ -249,7 +249,7 @@
249249
"content": [
250250
{
251251
"styles": {},
252-
"text": " Nested Check List Item",
252+
"text": "Nested Check List Item",
253253
"type": "text",
254254
},
255255
],

tests/src/unit/core/formatConversion/parse/__snapshots__/html/mixedTextTableCell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"content": [
1515
{
1616
"styles": {},
17-
"text": "Table CellTable Cell Table Cell",
17+
"text": "Table Cell
18+
Table Cell Table Cell",
1819
"type": "text",
1920
},
2021
],

tests/src/unit/core/formatConversion/parse/__snapshots__/html/nestedLists.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
"content": [
177177
{
178178
"styles": {},
179-
"text": " Check List Item",
179+
"text": "Check List Item",
180180
"type": "text",
181181
},
182182
],
@@ -196,7 +196,7 @@
196196
"content": [
197197
{
198198
"styles": {},
199-
"text": " Nested Check List Item",
199+
"text": "Nested Check List Item",
200200
"type": "text",
201201
},
202202
],
@@ -214,7 +214,7 @@
214214
"content": [
215215
{
216216
"styles": {},
217-
"text": " Nested Check List Item",
217+
"text": "Nested Check List Item",
218218
"type": "text",
219219
},
220220
],
@@ -231,7 +231,7 @@
231231
"content": [
232232
{
233233
"styles": {},
234-
"text": " Check List Item",
234+
"text": "Check List Item",
235235
"type": "text",
236236
},
237237
],
@@ -249,7 +249,7 @@
249249
"content": [
250250
{
251251
"styles": {},
252-
"text": " Nested Check List Item",
252+
"text": "Nested Check List Item",
253253
"type": "text",
254254
},
255255
],

0 commit comments

Comments
 (0)