Skip to content

Commit ff1d885

Browse files
committed
refactor: remove initializeESMDependencies kludge
1 parent 5354347 commit ff1d885

File tree

13 files changed

+43
-120
lines changed

13 files changed

+43
-120
lines changed

packages/core/src/api/exporters/html/externalHTMLExporter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
// 4. The HTML is wrapped in a single `div` element.
2929

3030
// Needs to be sync because it's used in drag handler event (SideMenuPlugin)
31-
// Ideally, call `await initializeESMDependencies()` before calling this function
3231
export const createExternalHTMLExporter = <
3332
BSchema extends BlockSchema,
3433
I extends InlineContentSchema,

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
import { Schema } from "prosemirror-model";
2+
import rehypeParse from "rehype-parse";
3+
import rehypeRemark from "rehype-remark";
4+
import remarkGfm from "remark-gfm";
5+
import remarkStringify from "remark-stringify";
6+
import { unified } from "unified";
7+
28
import { PartialBlock } from "../../../blocks/defaultBlocks.js";
39
import type { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js";
410
import {
511
BlockSchema,
612
InlineContentSchema,
713
StyleSchema,
814
} from "../../../schema/index.js";
9-
import {
10-
esmDependencies,
11-
initializeESMDependencies,
12-
} from "../../../util/esmDependencies.js";
1315
import { createExternalHTMLExporter } from "../html/externalHTMLExporter.js";
1416
import { removeUnderlines } from "./removeUnderlinesRehypePlugin.js";
1517
import { addSpacesToCheckboxes } from "./util/addSpacesToCheckboxesRehypePlugin.js";
1618

1719
// Needs to be sync because it's used in drag handler event (SideMenuPlugin)
18-
// Ideally, call `await initializeESMDependencies()` before calling this function
1920
export function cleanHTMLToMarkdown(cleanHTMLString: string) {
20-
const deps = esmDependencies;
21-
22-
if (!deps) {
23-
throw new Error(
24-
"cleanHTMLToMarkdown requires ESM dependencies to be initialized",
25-
);
26-
}
27-
28-
const markdownString = deps.unified
29-
.unified()
30-
.use(deps.rehypeParse.default, { fragment: true })
21+
const markdownString = unified()
22+
.use(rehypeParse, { fragment: true })
3123
.use(removeUnderlines)
3224
.use(addSpacesToCheckboxes)
33-
.use(deps.rehypeRemark.default)
34-
.use(deps.remarkGfm.default)
35-
.use(deps.remarkStringify.default, {
25+
.use(rehypeRemark)
26+
.use(remarkGfm)
27+
.use(remarkStringify, {
3628
handlers: { text: (node) => node.value },
3729
})
3830
.processSync(cleanHTMLString);
@@ -50,7 +42,6 @@ export async function blocksToMarkdown<
5042
editor: BlockNoteEditor<BSchema, I, S>,
5143
options: { document?: Document },
5244
): Promise<string> {
53-
await initializeESMDependencies();
5445
const exporter = createExternalHTMLExporter(schema, editor);
5546
const externalHTML = exporter.exportBlocks(blocks, options);
5647

packages/core/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
import { Element as HASTElement, Parent as HASTParent } from "hast";
2-
import { esmDependencies } from "../../../../util/esmDependencies.js";
2+
import { fromDom } from "hast-util-from-dom";
33

44
/**
55
* Rehype plugin which adds a space after each checkbox input element. This is
66
* because remark doesn't add any spaces between the checkbox input and the text
77
* itself, but these are needed for correct Markdown syntax.
88
*/
99
export function addSpacesToCheckboxes() {
10-
const deps = esmDependencies;
11-
12-
if (!deps) {
13-
throw new Error(
14-
"addSpacesToCheckboxes requires ESM dependencies to be initialized",
15-
);
16-
}
17-
1810
const helper = (tree: HASTParent) => {
1911
if (tree.children && "length" in tree.children && tree.children.length) {
2012
for (let i = tree.children.length - 1; i >= 0; i--) {
@@ -37,9 +29,7 @@ export function addSpacesToCheckboxes() {
3729
nextChild.children.splice(
3830
0,
3931
0,
40-
deps.hastUtilFromDom.fromDom(
41-
document.createTextNode(" "),
42-
) as HASTElement,
32+
fromDom(document.createTextNode(" ")) as HASTElement,
4333
);
4434
} else {
4535
helper(child as HASTParent);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
import { Block } from "../../../blocks/defaultBlocks.js";
99
import { nodeToBlock } from "../../nodeConversions/nodeToBlock.js";
1010
import { nestedListsToBlockNoteStructure } from "./util/nestedLists.js";
11-
export async function HTMLToBlocks<
11+
export function HTMLToBlocks<
1212
BSchema extends BlockSchema,
1313
I extends InlineContentSchema,
1414
S extends StyleSchema,
15-
>(html: string, pmSchema: Schema): Promise<Block<BSchema, I, S>[]> {
15+
>(html: string, pmSchema: Schema): Block<BSchema, I, S>[] {
1616
const htmlNode = nestedListsToBlockNoteStructure(html);
1717
const parser = DOMParser.fromSchema(pmSchema);
1818

packages/core/src/api/parsers/html/util/nestedLists.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { describe, expect, it } from "vitest";
2-
import { initializeESMDependencies } from "../../../../util/esmDependencies.js";
32
import { nestedListsToBlockNoteStructure } from "./nestedLists.js";
3+
import { unified } from "unified";
4+
import rehypeParse from "rehype-parse";
5+
import rehypeFormat from "rehype-format";
6+
import rehypeStringify from "rehype-stringify";
47

58
async function testHTML(html: string) {
6-
const deps = await initializeESMDependencies();
7-
89
const htmlNode = nestedListsToBlockNoteStructure(html);
910

10-
const pretty = await deps.unified
11-
.unified()
12-
.use(deps.rehypeParse.default, { fragment: true })
13-
.use(deps.rehypeFormat.default)
14-
.use(deps.rehypeStringify.default)
11+
const pretty = await unified()
12+
.use(rehypeParse, { fragment: true })
13+
.use(rehypeFormat)
14+
.use(rehypeStringify)
1515
.process(htmlNode.innerHTML);
1616

1717
expect(pretty.value).toMatchSnapshot();

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { Schema } from "prosemirror-model";
2+
import remarkGfm from "remark-gfm";
3+
import remarkParse from "remark-parse";
4+
import remarkRehype, {
5+
defaultHandlers as remarkRehypeDefaultHandlers,
6+
} from "remark-rehype";
7+
import rehypeStringify from "rehype-stringify";
8+
import { unified } from "unified";
29

310
import { Block } from "../../../blocks/defaultBlocks.js";
411
import {
512
BlockSchema,
613
InlineContentSchema,
714
StyleSchema,
815
} from "../../../schema/index.js";
9-
import { initializeESMDependencies } from "../../../util/esmDependencies.js";
1016
import { HTMLToBlocks } from "../html/parseHTML.js";
1117

1218
// modified version of https://github.com/syntax-tree/mdast-util-to-hast/blob/main/lib/handlers/code.js
@@ -49,19 +55,16 @@ function code(state: any, node: any) {
4955
}
5056

5157
export async function markdownToHTML(markdown: string): Promise<string> {
52-
const deps = await initializeESMDependencies();
53-
54-
const htmlString = deps.unified
55-
.unified()
56-
.use(deps.remarkParse.default)
57-
.use(deps.remarkGfm.default)
58-
.use(deps.remarkRehype.default, {
58+
const htmlString = unified()
59+
.use(remarkParse)
60+
.use(remarkGfm)
61+
.use(remarkRehype, {
5962
handlers: {
60-
...(deps.remarkRehype.defaultHandlers as any),
63+
...(remarkRehypeDefaultHandlers as any),
6164
code,
6265
},
6366
})
64-
.use(deps.rehypeStringify.default)
67+
.use(rehypeStringify)
6568
.processSync(markdown);
6669

6770
return htmlString.value as string;

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,9 +1574,9 @@ export class BlockNoteEditor<
15741574
* @param blocks An array of blocks that should be serialized into HTML.
15751575
* @returns The blocks, serialized as an HTML string.
15761576
*/
1577-
public async blocksToHTMLLossy(
1577+
public blocksToHTMLLossy(
15781578
blocks: PartialBlock<BSchema, ISchema, SSchema>[] = this.document,
1579-
): Promise<string> {
1579+
): string {
15801580
const exporter = createExternalHTMLExporter(this.pmSchema, this);
15811581
return exporter.exportBlocks(blocks, {});
15821582
}
@@ -1590,9 +1590,9 @@ export class BlockNoteEditor<
15901590
* @param blocks An array of blocks that should be serialized into HTML.
15911591
* @returns The blocks, serialized as an HTML string.
15921592
*/
1593-
public async blocksToFullHTML(
1593+
public blocksToFullHTML(
15941594
blocks: PartialBlock<BSchema, ISchema, SSchema>[],
1595-
): Promise<string> {
1595+
): string {
15961596
const exporter = createInternalHTMLSerializer(this.pmSchema, this);
15971597
return exporter.serializeBlocks(blocks, {});
15981598
}
@@ -1603,9 +1603,9 @@ export class BlockNoteEditor<
16031603
* @param html The HTML string to parse blocks from.
16041604
* @returns The blocks parsed from the HTML string.
16051605
*/
1606-
public async tryParseHTMLToBlocks(
1606+
public tryParseHTMLToBlocks(
16071607
html: string,
1608-
): Promise<Block<BSchema, ISchema, SSchema>[]> {
1608+
): Block<BSchema, ISchema, SSchema>[] {
16091609
return HTMLToBlocks(html, this.pmSchema);
16101610
}
16111611

packages/core/src/extensions/SideMenu/SideMenuPlugin.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
InlineContentSchema,
1818
StyleSchema,
1919
} from "../../schema/index.js";
20-
import { initializeESMDependencies } from "../../util/esmDependencies.js";
2120
import { getDraggableBlockFromElement } from "../getDraggableBlockFromElement.js";
2221
import { dragStart, unsetDragImage } from "./dragging.js";
2322

@@ -171,7 +170,6 @@ export class SideMenuView<
171170
this.onDragEnd as EventListener,
172171
true,
173172
);
174-
initializeESMDependencies();
175173

176174
// Shows or updates menu position whenever the cursor moves, if the menu isn't frozen.
177175
this.pmView.root.addEventListener(

packages/core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export * from "./i18n/dictionary.js";
5656
export * from "./schema/index.js";
5757
export * from "./util/browser.js";
5858
export * from "./util/combineByGroup.js";
59-
export * from "./util/esmDependencies.js";
6059
export * from "./util/string.js";
6160
export * from "./util/table.js";
6261
export * from "./util/typescript.js";

packages/core/src/util/esmDependencies.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)