Skip to content

Commit 7cd14e2

Browse files
fix: better markdown & html paste, make methods synchronous (#1957)
Co-authored-by: Nick the Sick <nick@blocknotejs.org>
1 parent a90ae4d commit 7cd14e2

File tree

9 files changed

+92
-98
lines changed

9 files changed

+92
-98
lines changed

docs/content/docs/reference/editor/overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ editor.pasteText("Hello, world!");
106106
Paste Markdown content into the editor.
107107

108108
```ts
109-
await editor.pasteMarkdown("# Hello\n\nThis is **bold** text.");
109+
editor.pasteMarkdown("# Hello\n\nThis is **bold** text.");
110110
```
111111

112112
## Options

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@
9696
"hast-util-from-dom": "^5.0.1",
9797
"prosemirror-dropcursor": "^1.8.2",
9898
"prosemirror-highlight": "^0.13.0",
99-
"prosemirror-model": "^1.25.1",
99+
"prosemirror-model": "^1.25.3",
100100
"prosemirror-state": "^1.4.3",
101101
"prosemirror-tables": "^1.6.4",
102102
"prosemirror-transform": "^1.10.4",
103-
"prosemirror-view": "^1.38.1",
103+
"prosemirror-view": "^1.40.1",
104104
"rehype-format": "^5.0.1",
105105
"rehype-parse": "^9.0.1",
106106
"rehype-remark": "^10.0.1",

packages/core/src/api/exporters/markdown/markdownExporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function cleanHTMLToMarkdown(cleanHTMLString: string) {
3232
return markdownString.value as string;
3333
}
3434

35-
export async function blocksToMarkdown<
35+
export function blocksToMarkdown<
3636
BSchema extends BlockSchema,
3737
I extends InlineContentSchema,
3838
S extends StyleSchema,
@@ -41,7 +41,7 @@ export async function blocksToMarkdown<
4141
schema: Schema,
4242
editor: BlockNoteEditor<BSchema, I, S>,
4343
options: { document?: Document },
44-
): Promise<string> {
44+
): string {
4545
const exporter = createExternalHTMLExporter(schema, editor);
4646
const externalHTML = exporter.exportBlocks(blocks, options);
4747

packages/core/src/api/parsers/html/parseHTML.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { Block } from "../../../blocks/defaultBlocks.js";
99
import { nodeToBlock } from "../../nodeConversions/nodeToBlock.js";
1010
import { nestedListsToBlockNoteStructure } from "./util/nestedLists.js";
11+
1112
export function HTMLToBlocks<
1213
BSchema extends BlockSchema,
1314
I extends InlineContentSchema,

packages/core/src/api/parsers/markdown/parseMarkdown.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function code(state: any, node: any) {
5454
return result;
5555
}
5656

57-
export async function markdownToHTML(markdown: string): Promise<string> {
57+
export function markdownToHTML(markdown: string): string {
5858
const htmlString = unified()
5959
.use(remarkParse)
6060
.use(remarkGfm)
@@ -70,12 +70,12 @@ export async function markdownToHTML(markdown: string): Promise<string> {
7070
return htmlString.value as string;
7171
}
7272

73-
export async function markdownToBlocks<
73+
export function markdownToBlocks<
7474
BSchema extends BlockSchema,
7575
I extends InlineContentSchema,
7676
S extends StyleSchema,
77-
>(markdown: string, pmSchema: Schema): Promise<Block<BSchema, I, S>[]> {
78-
const htmlString = await markdownToHTML(markdown);
77+
>(markdown: string, pmSchema: Schema): Block<BSchema, I, S>[] {
78+
const htmlString = markdownToHTML(markdown);
7979

8080
return HTMLToBlocks(htmlString, pmSchema);
8181
}

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,21 @@ import { dropCursor } from "prosemirror-dropcursor";
105105
import { EditorView } from "prosemirror-view";
106106
import { redoCommand, undoCommand, ySyncPluginKey } from "y-prosemirror";
107107
import { createInternalHTMLSerializer } from "../api/exporters/html/internalHTMLSerializer.js";
108-
import { inlineContentToNodes } from "../api/nodeConversions/blockToNode.js";
109-
import { docToBlocks } from "../api/nodeConversions/nodeToBlock.js";
110108
import {
111109
BlocksChanged,
112110
getBlocksChangedByTransaction,
113111
} from "../api/getBlocksChangedByTransaction.js";
114-
import { nestedListsToBlockNoteStructure } from "../api/parsers/html/util/nestedLists.js";
112+
import { inlineContentToNodes } from "../api/nodeConversions/blockToNode.js";
113+
import { docToBlocks } from "../api/nodeConversions/nodeToBlock.js";
115114
import { CodeBlockOptions } from "../blocks/CodeBlockContent/CodeBlockContent.js";
116115
import type { ThreadStore, User } from "../comments/index.js";
116+
import { BlockChangePlugin } from "../extensions/BlockChange/BlockChangePlugin.js";
117117
import type { CursorPlugin } from "../extensions/Collaboration/CursorPlugin.js";
118118
import type { ForkYDocPlugin } from "../extensions/Collaboration/ForkYDocPlugin.js";
119119
import { EventEmitter } from "../util/EventEmitter.js";
120120
import { BlockNoteExtension } from "./BlockNoteExtension.js";
121121

122122
import "../style.css";
123-
import { BlockChangePlugin } from "../extensions/BlockChange/BlockChangePlugin.js";
124123

125124
/**
126125
* A factory function that returns a BlockNoteExtension
@@ -1616,9 +1615,9 @@ export class BlockNoteEditor<
16161615
* @param blocks An array of blocks that should be serialized into Markdown.
16171616
* @returns The blocks, serialized as a Markdown string.
16181617
*/
1619-
public async blocksToMarkdownLossy(
1618+
public blocksToMarkdownLossy(
16201619
blocks: PartialBlock<BSchema, ISchema, SSchema>[] = this.document,
1621-
): Promise<string> {
1620+
): string {
16221621
return blocksToMarkdown(blocks, this.pmSchema, this, {});
16231622
}
16241623

@@ -1833,14 +1832,6 @@ export class BlockNoteEditor<
18331832
this.showSelectionPlugin.setEnabled(forceSelectionVisible);
18341833
}
18351834

1836-
/**
1837-
* This will convert HTML into a format that is compatible with BlockNote.
1838-
*/
1839-
private convertHtmlToBlockNoteHtml(html: string) {
1840-
const htmlNode = nestedListsToBlockNoteStructure(html.trim());
1841-
return htmlNode.innerHTML;
1842-
}
1843-
18441835
/**
18451836
* Paste HTML into the editor. Defaults to converting HTML to BlockNote HTML.
18461837
* @param html The HTML to paste.
@@ -1849,7 +1840,8 @@ export class BlockNoteEditor<
18491840
public pasteHTML(html: string, raw = false) {
18501841
let htmlToPaste = html;
18511842
if (!raw) {
1852-
htmlToPaste = this.convertHtmlToBlockNoteHtml(html);
1843+
const blocks = this.tryParseHTMLToBlocks(html);
1844+
htmlToPaste = this.blocksToFullHTML(blocks);
18531845
}
18541846
if (!htmlToPaste) {
18551847
return;
@@ -1869,7 +1861,8 @@ export class BlockNoteEditor<
18691861
* Paste markdown into the editor.
18701862
* @param markdown The markdown to paste.
18711863
*/
1872-
public async pasteMarkdown(markdown: string) {
1873-
return this.pasteHTML(await markdownToHTML(markdown));
1864+
public pasteMarkdown(markdown: string) {
1865+
const html = markdownToHTML(markdown);
1866+
return this.pasteHTML(html);
18741867
}
18751868
}

packages/xl-ai/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@
7373
"ai": "^4.3.15",
7474
"lodash.isequal": "^4.5.0",
7575
"prosemirror-changeset": "^2.3.0",
76-
"prosemirror-model": "^1.24.1",
76+
"prosemirror-model": "^1.25.3",
7777
"prosemirror-state": "^1.4.3",
7878
"prosemirror-tables": "^1.6.4",
7979
"prosemirror-transform": "^1.10.4",
80-
"prosemirror-view": "^1.33.7",
80+
"prosemirror-view": "^1.40.1",
8181
"react": "^19.1.0",
8282
"react-dom": "^19.1.0",
8383
"react-icons": "^5.2.1",

packages/xl-multi-column/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
"@blocknote/core": "0.37.0",
5555
"@blocknote/react": "0.37.0",
5656
"@tiptap/core": "^2.26.1",
57-
"prosemirror-model": "^1.25.1",
57+
"prosemirror-model": "^1.25.3",
5858
"prosemirror-state": "^1.4.3",
5959
"prosemirror-tables": "^1.3.7",
6060
"prosemirror-transform": "^1.10.4",
61-
"prosemirror-view": "^1.38.1",
61+
"prosemirror-view": "^1.40.1",
6262
"react-icons": "^5.2.1"
6363
},
6464
"devDependencies": {

0 commit comments

Comments
 (0)