Skip to content

Commit 8ef6188

Browse files
committed
Updated code block docs & package export name
1 parent 80963f4 commit 8ef6188

File tree

6 files changed

+54
-83
lines changed

6 files changed

+54
-83
lines changed

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

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@ Code blocks by default are a simple way to display code. But, BlockNote also sup
2020
experience easy to use and reduce bundle size.
2121
</Callout>
2222

23-
You can enable more advanced features by passing the `codeBlock` option when creating the editor.
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).
2424

2525
```ts
2626
const editor = new BlockNoteEditor({
27-
codeBlock: {
28-
indentLineWithTab: true,
29-
defaultLanguage: "typescript",
30-
supportedLanguages: {
31-
typescript: {
32-
name: "TypeScript",
33-
aliases: ["ts"],
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+
},
3436
},
35-
},
36-
createHighlighter: () =>
37-
createHighlighter({
38-
themes: ["light-plus", "dark-plus"],
39-
langs: [],
40-
}),
41-
},
37+
createHighlighter: () =>
38+
createHighlighter({
39+
themes: ["light-plus", "dark-plus"],
40+
langs: [],
41+
}),
42+
}),
43+
}),
4244
});
4345
```
4446

@@ -64,7 +66,7 @@ type CodeBlock = {
6466

6567
### Basic Setup
6668

67-
To enable code block syntax highlighting, you can use the `codeBlock` option in the `useCreateBlockNote` hook.
69+
To enable code block syntax highlighting, you can extend the editor's default schema with a new `codeBlock`.
6870

6971
First, install the package:
7072

@@ -75,20 +77,24 @@ npm install @blocknote/code-block
7577
Then use it like this:
7678

7779
```tsx
78-
import { codeBlock } from "@blocknote/code-block";
80+
import { codeBlockOptions } from "@blocknote/code-block";
7981

8082
export default function App() {
8183
const editor = useCreateBlockNote({
82-
codeBlock,
84+
schema: BlockNoteSchema.create().extend({
85+
codeBlock: createCodeBlockSpec(codeBlockOptions),
86+
}),
8387
});
8488

8589
return <BlockNoteView editor={editor} />;
8690
}
8791
```
8892

93+
This will extend the default schema with a custom `codeBlock` that includes options from `@blocknote/code-block`, enabling syntax highlighting.
94+
8995
### Custom Themes & Languages
9096

91-
To configure a code block highlighting theme and language, you can use the `codeBlock` option in the `useCreateBlockNote` hook.
97+
To configure a code block highlighting theme and language, you can again extend the editor's default schema with a new `codeBlock`.
9298

9399
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.
94100

@@ -109,21 +115,23 @@ import { createHighlighter } from "./shiki.bundle.js";
109115

110116
export default function App() {
111117
const editor = useCreateBlockNote({
112-
codeBlock: {
113-
indentLineWithTab: true,
114-
defaultLanguage: "typescript",
115-
supportedLanguages: {
116-
typescript: {
117-
name: "TypeScript",
118-
aliases: ["ts"],
118+
schema: BlockNoteSchema.create().extend({
119+
codeBlock: createCodeBlockSpec({
120+
indentLineWithTab: true,
121+
defaultLanguage: "typescript",
122+
supportedLanguages: {
123+
typescript: {
124+
name: "TypeScript",
125+
aliases: ["ts"],
126+
},
119127
},
120-
},
121-
createHighlighter: () =>
122-
createHighlighter({
123-
themes: ["light-plus", "dark-plus"],
124-
langs: [],
125-
}),
126-
},
128+
createHighlighter: () =>
129+
createHighlighter({
130+
themes: ["light-plus", "dark-plus"],
131+
langs: [],
132+
}),
133+
}),
134+
}),
127135
});
128136

129137
return <BlockNoteView editor={editor} />;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Code Block Syntax Highlighting
22

3-
To enable code block syntax highlighting, you can use the `codeBlock` option in the `useCreateBlockNote` hook. This is excluded by default to reduce bundle size.
3+
To enable code block syntax highlighting, you can extend the editor's default schema with a new `codeBlock`, which you can pass options into when creating. By passing the default options from `@blocknote/code-block`, you can enable syntax highlighting. This is excluded by default to reduce bundle size.
44

55
**Relevant Docs:**
66

77
- [Code Block Syntax Highlighting](/docs/features/blocks/code-blocks)
88
- [Editor Setup](/docs/getting-started/editor-setup)
9+
- [Custom Schema](/docs/features/custom-schemas)

examples/04-theming/06-code-block/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { BlockNoteView } from "@blocknote/mantine";
44
import "@blocknote/mantine/style.css";
55
import { useCreateBlockNote } from "@blocknote/react";
66
// This packages some of the most used languages in on-demand bundle
7-
import { codeBlock } from "@blocknote/code-block";
7+
import { codeBlockOptions } from "@blocknote/code-block";
88

99
export default function App() {
1010
// Creates a new editor instance.
1111
const editor = useCreateBlockNote({
1212
schema: BlockNoteSchema.create().extend({
1313
blockSpecs: {
14-
codeBlock: createCodeBlockSpec(codeBlock),
14+
codeBlock: createCodeBlockSpec(codeBlockOptions),
1515
},
1616
}),
1717
initialContent: [
Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
11
# Custom Code Block Theme & Language
22

3-
To configure a code block highlighting theme and language, you can use the `codeBlock` option in the `useCreateBlockNote` hook.
3+
To configure a code block highlighting theme and language, you can extend the editor's default schema with a new `codeBlock`, which you can pass options into when creating. You can then use a shiki highlighter to add custom syntax highlighting.
44

5-
This allows you to configure a shiki highlighter for the code blocks of your editor, allowing you to tailor the themes and languages you would like to use.
6-
7-
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.
8-
9-
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:
10-
11-
```bash
12-
npx shiki-codegen --langs javascript,typescript,vue --themes light,dark --engine javascript --precompiled ./shiki.bundle.ts
13-
```
14-
15-
This will generate a `shiki.bundle.ts` file that you can use to create a syntax highlighter for your editor.
16-
17-
Like this:
18-
19-
```ts
20-
import { createHighlighter } from "./shiki.bundle";
21-
22-
export default function App() {
23-
// Creates a new editor instance.
24-
const editor = useCreateBlockNote({
25-
codeBlock: {
26-
indentLineWithTab: true,
27-
defaultLanguage: "typescript",
28-
supportedLanguages: {
29-
typescript: {
30-
name: "TypeScript",
31-
aliases: ["ts"],
32-
},
33-
},
34-
createHighlighter: () =>
35-
createHighlighter({
36-
themes: ["light-plus", "dark-plus"],
37-
langs: [],
38-
}),
39-
},
40-
});
41-
42-
return <BlockNoteView editor={editor} />;
43-
}
44-
```
5+
First use the [shiki-codegen](https://shiki.style/packages/codegen) CLI to create a `shiki.bundle.ts` file. You can then pass this file into the `codeBlock` options when creating it.
456

467
**Relevant Docs:**
478

489
- [Code Blocks](/docs/features/blocks/code-blocks)
4910
- [shiki-codegen](https://shiki.style/packages/codegen)
11+
- [Custom Schema](/docs/features/custom-schemas)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { describe, expect, it } from "vitest";
2-
import { codeBlock } from "./index.js";
2+
import { codeBlockOptions } from "./index.js";
33

44
describe("codeBlock", () => {
55
it("should exist", () => {
6-
expect(codeBlock).toBeDefined();
6+
expect(codeBlockOptions).toBeDefined();
77
});
88
it("should have defaultLanguage", () => {
9-
expect(codeBlock.defaultLanguage).toBeDefined();
9+
expect(codeBlockOptions.defaultLanguage).toBeDefined();
1010
});
1111
it("should have supportedLanguages", () => {
12-
expect(codeBlock.supportedLanguages).toBeDefined();
12+
expect(codeBlockOptions.supportedLanguages).toBeDefined();
1313
});
1414
it("should have createHighlighter", () => {
15-
expect(codeBlock.createHighlighter).toBeDefined();
15+
expect(codeBlockOptions.createHighlighter).toBeDefined();
1616
});
1717
});

packages/code-block/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CodeBlockOptions } from "@blocknote/core";
22
import { createHighlighter } from "./shiki.bundle.js";
33

4-
export const codeBlock = {
4+
export const codeBlockOptions = {
55
defaultLanguage: "javascript",
66
supportedLanguages: {
77
text: {

0 commit comments

Comments
 (0)