Is there a way to highlight code when using TipTap?
Using the default simple editor I can create block of code when using three backticks but, those are not highlighted.
import { Editor } from '@tiptap/core';
import StarterKit from '@tiptap/starter-kit';
import Highlight from '@tiptap/extension-highlight';
import Typography from '@tiptap/extension-typography';
new Editor({
element: element,
extensions: [StarterKit, Highlight, Typography],
content: html,
onTransaction: () => {
// force re-render so `editor.isActive` works as expected
editor = editor;
},
editorProps: {
attributes: {
class: 'focus:outline-none',
},
}
});
I followed @lakshya-thakur answer and used the code-block-lowlight extension.
However, the documentation is outdated or at least did not worked out at ouf the box with SvelteKit / Vite. Therefore here the detailed solution.
lowlight
and the languages you which to load. You can either import all languages, specific languages or as in this example only a subset of the most common languages.Duplicate extension names found: ['blockCode']
at runtime, therefore the block code extension included in the starter kit should be disabled.All together the coudl look like:
The code should now be highlighted.