|
| 1 | +--- |
| 2 | +title: Code Block Syntax Highlighting |
| 3 | +description: This section explains how to add syntax highlighting to code blocks. |
| 4 | +imageTitle: Code Block Syntax Highlighting |
| 5 | +--- |
| 6 | + |
| 7 | +import { Example } from "@/components/example"; |
| 8 | + |
| 9 | +# Code Block Syntax Highlighting |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +We've created a default setup which automatically includes some of the most common languages in the most optimized way possible. The language syntaxes are loaded on-demand to ensure the smallest bundle size for your users. |
| 14 | + |
| 15 | +To use it, you can do the following: |
| 16 | + |
| 17 | +```sh |
| 18 | +npm install @blocknote/code-block |
| 19 | +``` |
| 20 | + |
| 21 | +And then you can use it like this: |
| 22 | + |
| 23 | +```tsx |
| 24 | +import { codeBlock } from "@blocknote/code-block"; |
| 25 | + |
| 26 | +export default function App() { |
| 27 | + // Creates a new editor instance. |
| 28 | + const editor = useCreateBlockNote({ |
| 29 | + codeBlock, |
| 30 | + }); |
| 31 | + |
| 32 | + // Renders the editor instance using a React component. |
| 33 | + return <BlockNoteView editor={editor} />; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +See the code block example for a more detailed example. |
| 38 | + |
| 39 | +<Example name="theming/code-block" /> |
| 40 | + |
| 41 | +## Custom Themes & Languages |
| 42 | + |
| 43 | +To configure a code block highlighting theme and language, you can use the `codeBlock` option in the `useCreateBlockNote` hook. |
| 44 | + |
| 45 | +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. |
| 46 | + |
| 47 | +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. |
| 48 | + |
| 49 | +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: |
| 50 | + |
| 51 | +```bash |
| 52 | +npx shiki-codegen --langs javascript,typescript,vue --themes light,dark --engine javascript --precompiled ./shiki.bundle.ts |
| 53 | +``` |
| 54 | + |
| 55 | +This will generate a `shiki.bundle.ts` file that you can use to create a syntax highlighter for your editor. |
| 56 | + |
| 57 | +Like this: |
| 58 | + |
| 59 | +```ts |
| 60 | +import { createHighlighter } from "./shiki.bundle.js"; |
| 61 | + |
| 62 | +export default function App() { |
| 63 | + // Creates a new editor instance. |
| 64 | + const editor = useCreateBlockNote({ |
| 65 | + codeBlock: { |
| 66 | + indentLineWithTab: true, |
| 67 | + defaultLanguage: "typescript", |
| 68 | + supportedLanguages: { |
| 69 | + typescript: { |
| 70 | + name: "TypeScript", |
| 71 | + aliases: ["ts"], |
| 72 | + }, |
| 73 | + }, |
| 74 | + createHighlighter: () => |
| 75 | + createHighlighter({ |
| 76 | + themes: ["light-plus", "dark-plus"], |
| 77 | + langs: [], |
| 78 | + }), |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + return <BlockNoteView editor={editor} />; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +See the custom code block example for a more detailed example. |
| 87 | + |
| 88 | +<Example name="theming/custom-code-block" /> |
0 commit comments