Skip to content

feat: add option to set html id #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/01-basic/01-minimal/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@blocknote/core/fonts/inter.css";
import { useCreateBlockNote } from "@blocknote/react";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import { useCreateBlockNote } from "@blocknote/react";

export default function App() {
// Creates a new editor instance.
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/editor/BlockNoteEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ it("immediately replaces doc", async () => {
]
`);
});

it("adds id attribute when requested", async () => {
const editor = BlockNoteEditor.create({
setIdAttribute: true,
});
const blocks = await editor.tryParseMarkdownToBlocks(
"This is a normal text\n\n# And this is a large heading"
);
editor.replaceBlocks(editor.document, blocks);
expect(
await editor.blocksToFullHTML(editor.document)
).toMatchInlineSnapshot(`"<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1" id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1" id="1"><div class="bn-block-content" data-content-type="paragraph"><p class="bn-inline-content">This is a normal text</p></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="2" id="2"><div class="bn-block" data-node-type="blockContainer" data-id="2" id="2"><div class="bn-block-content" data-content-type="heading" data-level="1"><h1 class="bn-inline-content">And this is a large heading</h1></div></div></div></div>"`);
});
11 changes: 11 additions & 0 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ export type BlockNoteEditorOptions<
* You probably don't need to set this manually, but use the `server-util` package instead that uses this option internally
*/
_headless: boolean;

/**
* A flag indicating whether to set an HTML ID for every block
*
* When set to `true`, on each block an id attribute will be set with the block id
* Otherwise, the HTML ID attribute will not be set.
*
* (note that the id is always set on the `data-id` attribute)
*/
setIdAttribute?: boolean;
};

const blockNoteTipTapOptions = {
Expand Down Expand Up @@ -339,6 +349,7 @@ export class BlockNoteEditor<
collaboration: newOptions.collaboration,
trailingBlock: newOptions.trailingBlock,
disableExtensions: newOptions.disableExtensions,
setIdAttribute: newOptions.setIdAttribute,
});

const blockNoteUIExtension = Extension.create({
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/editor/BlockNoteExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { Link } from "@tiptap/extension-link";
import { Text } from "@tiptap/extension-text";
import * as Y from "yjs";
import { createCopyToClipboardExtension } from "../api/exporters/copyExtension";
import { createPasteFromClipboardExtension } from "../api/parsers/pasteExtension";
import { createDropFileExtension } from "../api/parsers/fileDropExtension";
import { createPasteFromClipboardExtension } from "../api/parsers/pasteExtension";
import { BackgroundColorExtension } from "../extensions/BackgroundColor/BackgroundColorExtension";
import { TextAlignmentExtension } from "../extensions/TextAlignment/TextAlignmentExtension";
import { TextColorExtension } from "../extensions/TextColor/TextColorExtension";
Expand Down Expand Up @@ -55,6 +55,7 @@ export const getBlockNoteExtensions = <
renderCursor?: (user: any) => HTMLElement;
};
disableExtensions: string[] | undefined;
setIdAttribute?: boolean;
}) => {
const ret: Extensions = [
extensions.ClipboardTextSerializer,
Expand All @@ -69,6 +70,7 @@ export const getBlockNoteExtensions = <
// DropCursor,
UniqueID.configure({
types: ["blockContainer"],
setIdAttribute: opts.setIdAttribute,
}),
HardBreak.extend({ priority: 10 }),
// Comments,
Expand Down Expand Up @@ -197,5 +199,5 @@ export const getBlockNoteExtensions = <
}

const disableExtensions: string[] = opts.disableExtensions || [];
return ret.filter(ex => !disableExtensions.includes(ex.name));
return ret.filter((ex) => !disableExtensions.includes(ex.name));
};
19 changes: 15 additions & 4 deletions packages/core/src/extensions/UniqueID/UniqueID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const UniqueID = Extension.create({
return {
attributeName: "id",
types: [],
setIdAttribute: false,
generateID: () => {
// Use mock ID if tests are running.
if (typeof window !== "undefined" && (window as any).__TEST_OPTIONS) {
Expand Down Expand Up @@ -77,10 +78,20 @@ const UniqueID = Extension.create({
default: null,
parseHTML: (element) =>
element.getAttribute(`data-${this.options.attributeName}`),
renderHTML: (attributes) => ({
[`data-${this.options.attributeName}`]:
attributes[this.options.attributeName],
}),
renderHTML: (attributes) => {
const defaultIdAttributes = {
[`data-${this.options.attributeName}`]:
attributes[this.options.attributeName],
};
if (this.options.setIdAttribute) {
return {
...defaultIdAttributes,
id: attributes[this.options.attributeName],
};
} else {
return defaultIdAttributes;
}
},
},
},
},
Expand Down
Loading