Skip to content

Commit 9c6f563

Browse files
docs: Default block configuration example (#2098)
* Added example for configuring default blocks * Updated package lock * Regenerated example files * Updated docs * Docs fixes * Updated example * Implemented PR feedback
1 parent 8225736 commit 9c6f563

File tree

94 files changed

+2034
-1660
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2034
-1660
lines changed

docs/content/docs/features/blocks/code-blocks.mdx

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,48 @@ Code blocks by default are a simple way to display code. But, BlockNote also sup
1717

1818
<Callout type="info">
1919
These features are disabled by default to keep the default code block
20-
experience easy to use and reduce bundle size.
20+
experience easy to use and reduce bundle size. They can be individually added
21+
when [configuring the
22+
block](/docs/features/blocks#configuring-default-blocks).
2123
</Callout>
2224

23-
You can enable more advanced features by extending the editor's default schema with a new `codeBlock`, which you can pass options into when creating. For more on extending the editor schema, see [Custom Schemas](/docs/features/custom-schemas).
25+
**Configuration Options**
2426

2527
```ts
26-
const editor = new BlockNoteEditor({
27-
schema: BlockNoteSchema.create().extend({
28-
codeBlock: createCodeBlockSpec({
29-
indentLineWithTab: true,
30-
defaultLanguage: "typescript",
31-
supportedLanguages: {
32-
typescript: {
33-
name: "TypeScript",
34-
aliases: ["ts"],
35-
},
36-
},
37-
createHighlighter: () =>
38-
createHighlighter({
39-
themes: ["light-plus", "dark-plus"],
40-
langs: [],
41-
}),
42-
}),
43-
}),
44-
});
28+
type CodeBlockOptions = {
29+
indentLineWithTab?: boolean;
30+
defaultLanguage?: string;
31+
supportedLanguages?: Record<
32+
string,
33+
{
34+
name: string;
35+
aliases?: string[];
36+
}
37+
>;
38+
createHighlighter?: () => Promise<HighlighterGeneric<any, any>>;
39+
};
4540
```
4641

47-
You can choose to enable only certain features, or none at all. Giving you the flexibility to use code blocks how you need in your app.
42+
`indentLineWithTab:` Whether the Tab key should indent lines, or not be handled by the code block specially. Defaults to `true`.
43+
44+
`defaultLanguage:` The syntax highlighting default language for code blocks which are created/inserted without a set language, which is `text` by default (no syntax highlighting).
45+
46+
`supportedLanguages:` The syntax highlighting languages supported by the code block, which is an empty array by default.
4847

49-
## Block Shape
48+
`createHighlighter:` The [Shiki highliter](https://shiki.style/guide/load-theme) to use for syntax highlighting.
5049

51-
This describes the shape of a code block in BlockNote.
50+
BlockNote also provides a generic set of options for syntax highlighting in the `@blocknote/code-block` package, which support a wide range of languages:
51+
52+
```ts
53+
import { createCodeBlockSpec } from "@blocknote/core";
54+
import { codeBlockOptions } from "@blocknote/code-block";
55+
56+
const codeBlock = createCodeBlockSpec(codeBlockOptions);
57+
```
58+
59+
See [this example](/examples/theming/code-block) to see it in action.
60+
61+
**Type & Props**
5262

5363
```ts
5464
type CodeBlock = {
@@ -62,45 +72,13 @@ type CodeBlock = {
6272
};
6373
```
6474

65-
## Options
66-
67-
### Basic Setup
68-
69-
To enable code block syntax highlighting, you can extend the editor's default schema with a new `codeBlock`.
70-
71-
First, install the package:
72-
73-
```sh
74-
npm install @blocknote/code-block
75-
```
76-
77-
Then use it like this:
78-
79-
```tsx
80-
import { codeBlockOptions } from "@blocknote/code-block";
81-
82-
export default function App() {
83-
const editor = useCreateBlockNote({
84-
schema: BlockNoteSchema.create().extend({
85-
codeBlock: createCodeBlockSpec(codeBlockOptions),
86-
}),
87-
});
88-
89-
return <BlockNoteView editor={editor} />;
90-
}
91-
```
92-
93-
This will extend the default schema with a custom `codeBlock` that includes options from `@blocknote/code-block`, enabling syntax highlighting.
94-
95-
### Custom Themes & Languages
96-
97-
To configure a code block highlighting theme and language, you can again extend the editor's default schema with a new `codeBlock`.
75+
`language:` The syntax highlighting language to use. Defaults to `text`, which has no highlighting.
9876

99-
This allows you to configure a [shiki](https://shiki.style) highlighter for the code blocks of your editor, allowing you to tailor the themes and languages you would like to use.
77+
## Custom Syntax Highlighting
10078

101-
To create a syntax highlighter, you can use the [shiki-codegen](https://shiki.style/packages/codegen) CLI for generating the code to create a syntax highlighter for your languages and themes.
79+
To create your own syntax highlighter, you can use the [shiki-codegen](https://shiki.style/packages/codegen) CLI for generating the code to create one for your chosen languages and themes.
10280

103-
For example to create a syntax highlighter using the optimized javascript engine, javascript, typescript, vue, with light and dark themes, you can run the following command:
81+
For example, to create a syntax highlighter using the optimized javascript engine, javascript, typescript, vue, with light and dark themes, you can run the following command:
10482

10583
```bash
10684
npx shiki-codegen --langs javascript,typescript,vue --themes light,dark --engine javascript --precompiled ./shiki.bundle.ts

docs/content/docs/features/blocks/embeds.mdx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Embeds are a way to display content from external sources in your documents. Blo
1010

1111
## File
1212

13-
A file block is a block that displays a file.
14-
1513
**Type & Props**
1614

1715
```ts
@@ -36,7 +34,15 @@ type FileBlock = {
3634

3735
## Image
3836

39-
An image block is a block that displays an image.
37+
**Configuration Options**
38+
39+
```ts
40+
type ImageBlockOptions = {
41+
icon?: string;
42+
};
43+
```
44+
45+
`icon:` The HTML string for an icon SVG. If no URL is given, the image block displays a button to add one using a link or file, and the provided SVG replaces the generic file icon in that button.
4046

4147
**Type & Props**
4248

@@ -64,7 +70,15 @@ type ImageBlock = {
6470

6571
## Video
6672

67-
A video block is a block that displays a video.
73+
**Configuration Options**
74+
75+
```ts
76+
type VideoBlockOptions = {
77+
icon?: string;
78+
};
79+
```
80+
81+
`icon:` The HTML string for an icon SVG. If no URL is given, the video block displays a button to add one using a link or file, and the provided SVG replaces the generic file icon in that button.
6882

6983
**Type & Props**
7084

@@ -96,7 +110,15 @@ type VideoBlock = {
96110

97111
## Audio
98112

99-
An audio block is a block that displays an audio file.
113+
**Configuration Options**
114+
115+
```ts
116+
type AudioBlockOptions = {
117+
icon?: string;
118+
};
119+
```
120+
121+
`icon:` The HTML string for an icon SVG. If no URL is given, the audio block displays a button to add one using a link or file, and the provided SVG replaces the generic file icon in that button.
100122

101123
**Type & Props**
102124

docs/content/docs/features/blocks/index.mdx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The demo below showcases each of BlockNote's built-in block and inline content t
1212

1313
<Example name="basic/default-blocks" />
1414

15-
### Default Block Properties
15+
## Default Block Properties
1616

1717
There are some default block props that BlockNote uses for the built-in blocks:
1818

@@ -36,6 +36,38 @@ type DefaultProps = {
3636
};
3737
```
3838

39+
## Configuring Default Blocks
40+
41+
Some default blocks can be configured with options. For example, headings can be configured to have different available levels:
42+
43+
```ts
44+
// Creates a new instance of the default heading block.
45+
const heading = createHeadingBlockSpec({
46+
// Sets the block to support only heading levels 1-3.
47+
levels: [1, 2, 3],
48+
});
49+
```
50+
51+
Each default block type can be instantiated using their respective `create...BlockSpec` function. If the block can be configured, i.e. if it has options, you can pass them in an object to the function. To see which options each block type supports, read on to the next pages.
52+
53+
To add your configured block to the editor, you must pass in a [custom schema](/docs/features/custom-schemas) with it. The simplest way to do this is by [extending the default schema](/docs/features/custom-schemas#extending-an-existing-schema):
54+
55+
```ts
56+
const editor = useCreateBlockNote({
57+
// Creates a default schema and extends it with the configured heading block.
58+
schema: BlockNoteSchema.create().extend({
59+
blockSpecs: {
60+
heading: createHeadingBlockSpec({
61+
// Sets the allowed heading levels.
62+
levels: [1, 2, 3],
63+
}),
64+
},
65+
}),
66+
});
67+
```
68+
69+
You can see this in action in a working demo [here](/examples/custom-schema/configuring-blocks).
70+
3971
## Explore
4072

4173
<CardTable path="features/blocks" />

docs/content/docs/features/blocks/typography.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ type ParagraphBlock = {
2424

2525
## Heading
2626

27+
**Configuration Options**
28+
29+
```typescript
30+
type HeadingBlockOptions = Partial<{
31+
defaultLevel?: number;
32+
levels?: number[];
33+
allowToggleHeadings?: boolean;
34+
}>;
35+
```
36+
37+
`defaultLevel:` The default level for headings which are created/inserted without a set level, which is `1` by default.
38+
39+
`levels:` The heading levels that the block supports, or '1'-'6' by default.
40+
41+
`allowToggleHeadings:` Whether toggle headings should be supported, `true` by default. Toggle headings have a button which toggles between hiding and showing the block's children.
42+
2743
**Type & Props**
2844

2945
```typescript

docs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"@shikijs/engine-javascript": "^3.2.1",
5656
"@shikijs/langs-precompiled": "^3.2.1",
5757
"@shikijs/themes": "^3.2.1",
58-
"@shikijs/types": "^3.13.0",
58+
"@shikijs/types": "^3.2.1",
5959
"@tiptap/core": "^3.4.3",
6060
"@uppy/core": "^3.13.1",
6161
"@uppy/dashboard": "^3.9.1",
@@ -142,4 +142,4 @@
142142
"y-partykit": "^0.0.33",
143143
"yjs": "^13.6.27"
144144
}
145-
}
145+
}

examples/01-basic/01-minimal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
"@vitejs/plugin-react": "^4.3.1",
2929
"vite": "^5.3.4"
3030
}
31-
}
31+
}

examples/01-basic/02-block-objects/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
"@vitejs/plugin-react": "^4.3.1",
2929
"vite": "^5.3.4"
3030
}
31-
}
31+
}

examples/01-basic/03-multi-column/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
"@vitejs/plugin-react": "^4.3.1",
3030
"vite": "^5.3.4"
3131
}
32-
}
32+
}

examples/01-basic/04-default-blocks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
"@vitejs/plugin-react": "^4.3.1",
2929
"vite": "^5.3.4"
3030
}
31-
}
31+
}

examples/01-basic/05-removing-default-blocks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
"@vitejs/plugin-react": "^4.3.1",
2929
"vite": "^5.3.4"
3030
}
31-
}
31+
}

0 commit comments

Comments
 (0)