Skip to content

Commit 7945895

Browse files
committed
Merge branch 'main' into new-releases
2 parents 75d9591 + 05feb1e commit 7945895

File tree

25 files changed

+284
-205
lines changed

25 files changed

+284
-205
lines changed

.eslintrc.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
module.exports = {
22
root: true,
3-
extends: ["react-app", "react-app/jest"],
4-
plugins: ["import"],
3+
extends: [
4+
"eslint:recommended",
5+
"plugin:@typescript-eslint/recommended",
6+
"react-app",
7+
"react-app/jest",
8+
],
9+
parser: "@typescript-eslint/parser",
10+
plugins: ["import", "@typescript-eslint"],
511
rules: {
612
curly: 1,
713
"import/no-extraneous-dependencies": [
@@ -13,5 +19,8 @@ module.exports = {
1319
bundledDependencies: false,
1420
},
1521
],
22+
// would be nice to enable these rules later, but they are too noisy right now
23+
"@typescript-eslint/no-non-null-assertion": "off",
24+
"@typescript-eslint/no-explicit-any": "off",
1625
},
1726
};

examples/vanilla/src/ui/addHyperlinkToolbar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export const addHyperlinkToolbar = (editor: BlockNoteEditor) => {
1212
element.style.padding = "10px";
1313
element.style.opacity = "0.8";
1414

15-
let url = hyperlinkToolbarState.url;
16-
let text = hyperlinkToolbarState.text;
15+
const url = hyperlinkToolbarState.url;
16+
const text = hyperlinkToolbarState.text;
1717

1818
const editBtn = createButton("edit", () => {
1919
const newUrl = prompt("new url") || url;

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"eslint-config-react-app": "^7.0.0",
1414
"lerna": "^5.4.0",
1515
"patch-package": "^6.4.7",
16-
"typescript": "^5.0.4"
16+
"typescript": "^5.0.4",
17+
"@typescript-eslint/parser": "^5.5.0",
18+
"@typescript-eslint/eslint-plugin": "^5.5.0"
1719
},
1820
"scripts": {
1921
"start": "lerna run --stream --scope @blocknote/example-editor dev",

packages/core/src/BlockNoteEditor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
353353
*/
354354
public forEachBlock(
355355
callback: (block: Block<BSchema>) => boolean,
356-
reverse: boolean = false
356+
reverse = false
357357
): void {
358358
const blocks = this.topLevelBlocks.slice();
359359

@@ -692,7 +692,7 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
692692
return;
693693
}
694694

695-
let { from, to } = this._tiptapEditor.state.selection;
695+
const { from, to } = this._tiptapEditor.state.selection;
696696

697697
if (!text) {
698698
text = this._tiptapEditor.state.doc.textBetween(from, to);

packages/core/src/BlockNoteExtensions.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,34 @@ export const getBlockNoteExtensions = <BSchema extends BlockSchema>(opts: {
118118
fragment: opts.collaboration.fragment,
119119
})
120120
);
121-
const defaultRender = (user: { color: string; name: string }) => {
122-
const cursor = document.createElement("span");
121+
if (opts.collaboration.provider?.awareness) {
122+
const defaultRender = (user: { color: string; name: string }) => {
123+
const cursor = document.createElement("span");
123124

124-
cursor.classList.add(styles["collaboration-cursor__caret"]);
125-
cursor.setAttribute("style", `border-color: ${user.color}`);
125+
cursor.classList.add(styles["collaboration-cursor__caret"]);
126+
cursor.setAttribute("style", `border-color: ${user.color}`);
126127

127-
const label = document.createElement("span");
128+
const label = document.createElement("span");
128129

129-
label.classList.add(styles["collaboration-cursor__label"]);
130-
label.setAttribute("style", `background-color: ${user.color}`);
131-
label.insertBefore(document.createTextNode(user.name), null);
130+
label.classList.add(styles["collaboration-cursor__label"]);
131+
label.setAttribute("style", `background-color: ${user.color}`);
132+
label.insertBefore(document.createTextNode(user.name), null);
132133

133-
const nonbreakingSpace1 = document.createTextNode("\u2060");
134-
const nonbreakingSpace2 = document.createTextNode("\u2060");
135-
cursor.insertBefore(nonbreakingSpace1, null);
136-
cursor.insertBefore(label, null);
137-
cursor.insertBefore(nonbreakingSpace2, null);
138-
return cursor;
139-
};
140-
ret.push(
141-
CollaborationCursor.configure({
142-
user: opts.collaboration.user,
143-
render: opts.collaboration.renderCursor || defaultRender,
144-
provider: opts.collaboration.provider,
145-
})
146-
);
134+
const nonbreakingSpace1 = document.createTextNode("\u2060");
135+
const nonbreakingSpace2 = document.createTextNode("\u2060");
136+
cursor.insertBefore(nonbreakingSpace1, null);
137+
cursor.insertBefore(label, null);
138+
cursor.insertBefore(nonbreakingSpace2, null);
139+
return cursor;
140+
};
141+
ret.push(
142+
CollaborationCursor.configure({
143+
user: opts.collaboration.user,
144+
render: opts.collaboration.renderCursor || defaultRender,
145+
provider: opts.collaboration.provider,
146+
})
147+
);
148+
}
147149
} else {
148150
// disable history extension when collaboration is enabled as Yjs takes care of undo / redo
149151
ret.push(History);

packages/core/src/api/blockManipulation/blockManipulation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let insert: (
2424
) => Block<DefaultBlockSchema>[];
2525

2626
beforeEach(() => {
27-
(window as Window & { __TEST_OPTIONS?: {} }).__TEST_OPTIONS = {};
27+
(window as Window & { __TEST_OPTIONS?: any }).__TEST_OPTIONS = {};
2828

2929
editor = new BlockNoteEditor();
3030

@@ -80,7 +80,7 @@ afterEach(() => {
8080
editor._tiptapEditor.destroy();
8181
editor = undefined as any;
8282

83-
delete (window as Window & { __TEST_OPTIONS?: {} }).__TEST_OPTIONS;
83+
delete (window as Window & { __TEST_OPTIONS?: any }).__TEST_OPTIONS;
8484
});
8585

8686
describe("Inserting Blocks with Different Placements", () => {

packages/core/src/api/blockManipulation/blockManipulation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function removeBlocks(
107107
});
108108

109109
if (idsOfBlocksToRemove.size > 0) {
110-
let notFoundIds = [...idsOfBlocksToRemove].join("\n");
110+
const notFoundIds = [...idsOfBlocksToRemove].join("\n");
111111

112112
throw Error(
113113
"Blocks with the following IDs could not be found in the editor: " +

packages/core/src/api/formatConversions/formatConversions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ function removeInlineContentClass(html: string) {
579579
}
580580

581581
beforeEach(() => {
582-
(window as Window & { __TEST_OPTIONS?: {} }).__TEST_OPTIONS = {};
582+
(window as Window & { __TEST_OPTIONS?: any }).__TEST_OPTIONS = {};
583583

584584
editor = new BlockNoteEditor();
585585
});
@@ -588,7 +588,7 @@ afterEach(() => {
588588
editor._tiptapEditor.destroy();
589589
editor = undefined as any;
590590

591-
delete (window as Window & { __TEST_OPTIONS?: {} }).__TEST_OPTIONS;
591+
delete (window as Window & { __TEST_OPTIONS?: any }).__TEST_OPTIONS;
592592
});
593593

594594
describe("Non-Nested Block/HTML/Markdown Conversions", () => {

packages/core/src/api/formatConversions/formatConversions.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import rehypeRemark from "rehype-remark";
44
import rehypeStringify from "rehype-stringify";
55
import remarkGfm from "remark-gfm";
66
import remarkParse from "remark-parse";
7-
import remarkRehype from "remark-rehype";
7+
import remarkRehype, { defaultHandlers } from "remark-rehype";
88
import remarkStringify from "remark-stringify";
99
import { unified } from "unified";
1010
import { Block, BlockSchema } from "../../extensions/Blocks/api/blockTypes";
@@ -47,7 +47,7 @@ export async function HTMLToBlocks<BSchema extends BlockSchema>(
4747
htmlNode.innerHTML = html.trim();
4848

4949
const parser = DOMParser.fromSchema(schema);
50-
const parentNode = parser.parse(htmlNode);
50+
const parentNode = parser.parse(htmlNode); //, { preserveWhitespace: "full" });
5151

5252
const blocks: Block<BSchema>[] = [];
5353

@@ -73,6 +73,45 @@ export async function blocksToMarkdown<BSchema extends BlockSchema>(
7373
return markdownString.value as string;
7474
}
7575

76+
// modefied version of https://github.com/syntax-tree/mdast-util-to-hast/blob/main/lib/handlers/code.js
77+
// that outputs a data-language attribute instead of a CSS class (e.g.: language-typescript)
78+
function code(state: any, node: any) {
79+
const value = node.value ? node.value + "\n" : "";
80+
/** @type {Properties} */
81+
const properties: any = {};
82+
83+
if (node.lang) {
84+
// changed line
85+
properties["data-language"] = node.lang;
86+
}
87+
88+
// Create `<code>`.
89+
/** @type {Element} */
90+
let result: any = {
91+
type: "element",
92+
tagName: "code",
93+
properties,
94+
children: [{ type: "text", value }],
95+
};
96+
97+
if (node.meta) {
98+
result.data = { meta: node.meta };
99+
}
100+
101+
state.patch(node, result);
102+
result = state.applyData(node, result);
103+
104+
// Create `<pre>`.
105+
result = {
106+
type: "element",
107+
tagName: "pre",
108+
properties: {},
109+
children: [result],
110+
};
111+
state.patch(node, result);
112+
return result;
113+
}
114+
76115
export async function markdownToBlocks<BSchema extends BlockSchema>(
77116
markdown: string,
78117
blockSchema: BSchema,
@@ -81,7 +120,12 @@ export async function markdownToBlocks<BSchema extends BlockSchema>(
81120
const htmlString = await unified()
82121
.use(remarkParse)
83122
.use(remarkGfm)
84-
.use(remarkRehype)
123+
.use(remarkRehype, {
124+
handlers: {
125+
...(defaultHandlers as any),
126+
code,
127+
},
128+
})
85129
.use(rehypeStringify)
86130
.process(markdown);
87131

0 commit comments

Comments
 (0)