-
-
Notifications
You must be signed in to change notification settings - Fork 531
feat: use slim shiki build and pre-compiled languages #1519
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
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5744c57
feat: use slim shiki build and pre-compiled languages
nperez0111 0e6feb0
Merge branch 'main' into shiki-bundling
nperez0111 18196a5
feat: separate code-block package
nperez0111 898197b
feat: configuration for codeBlock options exposed
nperez0111 2c8aea0
chore: package updates
nperez0111 8a92651
docs: example integration (revert me)
nperez0111 eda4f70
chore: bump to latest prosemirror-highlight version
nperez0111 9ef6b40
docs: add examples and docs
nperez0111 c790747
test: update snapshots
nperez0111 ad0936d
chore: update package-lock
nperez0111 1c3f2d1
Merge branch 'main' into shiki-bundling
nperez0111 5bda388
build: get build to work again
nperez0111 e0df65b
chore: add simple tests
nperez0111 16c7876
build: do not recompile shiki packages
nperez0111 3435284
docs: attribution
nperez0111 c4eac85
docs: add install step
nperez0111 952d851
docs: reorganize docs and add new advanced page for code blocks
nperez0111 864aeb5
chore: minor cleanup
nperez0111 3603ea1
fix: output `data-language` in HTML
nperez0111 6fd73a4
fix: just always output, even if the language is text
nperez0111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"playground": true, | ||
"docs": true, | ||
"author": "nperez0111", | ||
"tags": ["Basic"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import "@blocknote/core/fonts/inter.css"; | ||
import { BlockNoteView } from "@blocknote/mantine"; | ||
import "@blocknote/mantine/style.css"; | ||
import { useCreateBlockNote } from "@blocknote/react"; | ||
// This packages some of the most used languages in on-demand bundle | ||
import { codeBlock } from "@blocknote/code-block"; | ||
|
||
export default function App() { | ||
// Creates a new editor instance. | ||
const editor = useCreateBlockNote({ | ||
codeBlock, | ||
}); | ||
|
||
// Renders the editor instance using a React component. | ||
return <BlockNoteView editor={editor} />; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Code Block Highlighting | ||
|
||
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 | ||
|
||
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. | ||
|
||
To use it, you can do the following: | ||
|
||
```sh | ||
npm install @blocknote/code-block | ||
``` | ||
|
||
And then you can use it like this: | ||
|
||
```tsx | ||
import { codeBlock } from "@blocknote/code-block"; | ||
|
||
export default function App() { | ||
// Creates a new editor instance. | ||
const editor = useCreateBlockNote({ | ||
codeBlock, | ||
}); | ||
|
||
// Renders the editor instance using a React component. | ||
return <BlockNoteView editor={editor} />; | ||
} | ||
``` | ||
|
||
If you need to configure the themes or support more languages, you can provide your own codeBlock highlighting like described in [the custom code block docs](./custom-code-block/) | ||
|
||
**Relevant Docs:** | ||
|
||
- [Editor Setup](/docs/editor-basics/setup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<html lang="en"> | ||
<head> | ||
<script> | ||
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY --> | ||
</script> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Code Block Highlighting</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="./main.tsx"></script> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY | ||
import React from "react"; | ||
import { createRoot } from "react-dom/client"; | ||
import App from "./App"; | ||
|
||
const root = createRoot(document.getElementById("root")!); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@blocknote/example-code-block", | ||
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY", | ||
"private": true, | ||
"version": "0.12.4", | ||
"scripts": { | ||
"start": "vite", | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview", | ||
"lint": "eslint . --max-warnings 0" | ||
}, | ||
"dependencies": { | ||
"@blocknote/code-block": "latest", | ||
"@blocknote/core": "latest", | ||
"@blocknote/react": "latest", | ||
"@blocknote/ariakit": "latest", | ||
"@blocknote/mantine": "latest", | ||
"@blocknote/shadcn": "latest", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.0.25", | ||
"@types/react-dom": "^18.0.9", | ||
"@vitejs/plugin-react": "^4.3.1", | ||
"eslint": "^8.10.0", | ||
"vite": "^5.3.4" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"../../../.eslintrc.js" | ||
] | ||
}, | ||
"eslintIgnore": [ | ||
"dist" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"__comment": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY", | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": [ | ||
"DOM", | ||
"DOM.Iterable", | ||
"ESNext" | ||
], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": false, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "ESNext", | ||
"moduleResolution": "bundler", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx", | ||
"composite": true | ||
}, | ||
"include": [ | ||
"." | ||
], | ||
"__ADD_FOR_LOCAL_DEV_references": [ | ||
{ | ||
"path": "../../../packages/core/" | ||
}, | ||
{ | ||
"path": "../../../packages/react/" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY | ||
import react from "@vitejs/plugin-react"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import { defineConfig } from "vite"; | ||
// import eslintPlugin from "vite-plugin-eslint"; | ||
// https://vitejs.dev/config/ | ||
export default defineConfig((conf) => ({ | ||
plugins: [react()], | ||
optimizeDeps: {}, | ||
build: { | ||
sourcemap: true, | ||
}, | ||
resolve: { | ||
alias: | ||
conf.command === "build" || | ||
!fs.existsSync(path.resolve(__dirname, "../../packages/core/src")) | ||
? {} | ||
: ({ | ||
// Comment out the lines below to load a built version of blocknote | ||
// or, keep as is to load live from sources with live reload working | ||
"@blocknote/core": path.resolve( | ||
__dirname, | ||
"../../packages/core/src/" | ||
), | ||
"@blocknote/react": path.resolve( | ||
__dirname, | ||
"../../packages/react/src/" | ||
), | ||
} as any), | ||
}, | ||
})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"playground": true, | ||
"docs": true, | ||
"author": "nperez0111", | ||
"tags": ["Basic"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import "@blocknote/core/fonts/inter.css"; | ||
import { BlockNoteView } from "@blocknote/mantine"; | ||
import "@blocknote/mantine/style.css"; | ||
import { useCreateBlockNote } from "@blocknote/react"; | ||
// Bundle created from `npx shiki-codegen --langs typescript,javascript,react --themes light-plus,dark-plus --engine javascript --precompiled ./shiki.bundle.ts` | ||
import { createHighlighter } from "./shiki.bundle.js"; | ||
|
||
export default function App() { | ||
// Creates a new editor instance. | ||
const editor = useCreateBlockNote({ | ||
codeBlock: { | ||
indentLineWithTab: true, | ||
defaultLanguage: "typescript", | ||
supportedLanguages: { | ||
typescript: { | ||
name: "TypeScript", | ||
aliases: ["ts"], | ||
}, | ||
javascript: { | ||
name: "JavaScript", | ||
aliases: ["js"], | ||
}, | ||
vue: { | ||
name: "Vue", | ||
}, | ||
}, | ||
// This creates a highlighter, it can be asynchronous to load it afterwards | ||
createHighlighter: () => | ||
createHighlighter({ | ||
themes: ["dark-plus", "light-plus"], | ||
langs: [], | ||
}), | ||
}, | ||
initialContent: [ | ||
{ | ||
id: "fc832df4-bd15-49d2-8d64-140c27f29692", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing as previous comment r.e. using |
||
type: "codeBlock", | ||
props: { | ||
language: "typescript", | ||
}, | ||
content: [ | ||
{ | ||
type: "text", | ||
text: "const x = 3 * 4;", | ||
styles: {}, | ||
}, | ||
], | ||
children: [], | ||
}, | ||
{ | ||
id: "aecf786b-cbde-4915-b86c-b4c8264fc8f7", | ||
type: "paragraph", | ||
props: { | ||
textColor: "default", | ||
backgroundColor: "default", | ||
textAlignment: "left", | ||
}, | ||
content: [], | ||
children: [], | ||
}, | ||
{ | ||
id: "f85ab261-dfe8-47f0-929f-533808a4184d", | ||
type: "heading", | ||
props: { | ||
textColor: "default", | ||
backgroundColor: "default", | ||
textAlignment: "left", | ||
level: 3, | ||
}, | ||
content: [ | ||
{ | ||
type: "text", | ||
text: 'Click on "Typescript" above to see the different supported languages', | ||
styles: {}, | ||
}, | ||
], | ||
children: [], | ||
}, | ||
{ | ||
id: "dec03378-6b49-442a-89f0-b2551ce0f60c", | ||
type: "paragraph", | ||
props: { | ||
textColor: "default", | ||
backgroundColor: "default", | ||
textAlignment: "left", | ||
}, | ||
content: [], | ||
children: [], | ||
}, | ||
], | ||
}); | ||
|
||
// Renders the editor instance using a React component. | ||
return <BlockNoteView editor={editor} />; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Custom Code Block Theme & Language | ||
|
||
To configure a code block highlighting theme and language, you can use the `codeBlock` option in the `useCreateBlockNote` hook. | ||
|
||
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. | ||
|
||
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. | ||
|
||
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: | ||
|
||
```bash | ||
npx shiki-codegen --langs javascript,typescript,vue --themes light,dark --engine javascript --precompiled ./shiki.bundle.ts | ||
``` | ||
|
||
This will generate a `shiki.bundle.ts` file that you can use to create a syntax highlighter for your editor. | ||
|
||
Like this: | ||
|
||
```ts | ||
import { createHighlighter } from "./shiki.bundle.js"; | ||
|
||
export default function App() { | ||
// Creates a new editor instance. | ||
const editor = useCreateBlockNote({ | ||
codeBlock: { | ||
indentLineWithTab: true, | ||
defaultLanguage: "typescript", | ||
supportedLanguages: { | ||
typescript: { | ||
name: "TypeScript", | ||
aliases: ["ts"], | ||
}, | ||
}, | ||
createHighlighter: () => | ||
createHighlighter({ | ||
themes: ["light-plus", "dark-plus"], | ||
langs: [], | ||
}), | ||
}, | ||
}); | ||
|
||
return <BlockNoteView editor={editor} />; | ||
} | ||
``` | ||
|
||
**Relevant Docs:** | ||
|
||
- [Editor Setup](/docs/editor-basics/setup) | ||
- [shiki-codegen](https://shiki.style/packages/codegen) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<html lang="en"> | ||
<head> | ||
<script> | ||
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY --> | ||
</script> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Custom Code Block Theme & Language</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="./main.tsx"></script> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY | ||
import React from "react"; | ||
import { createRoot } from "react-dom/client"; | ||
import App from "./App"; | ||
|
||
const root = createRoot(document.getElementById("root")!); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern we currently use is that we have:
The idea is that the examples can really be a larger set of examples to click through and play with without too much reading - and that all documentation should be in /docs.
Could we change this? or lmk if you like a different setup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally makes sense. Updated