What is @tiptap/extension-code-block-lowlight?
@tiptap/extension-code-block-lowlight is an extension for the Tiptap editor that provides syntax highlighting for code blocks using the Lowlight library. It allows developers to easily integrate code block syntax highlighting into their Tiptap-based rich text editors.
What are @tiptap/extension-code-block-lowlight's main functionalities?
Basic Code Block with Syntax Highlighting
This code sample demonstrates how to set up a Tiptap editor with the CodeBlockLowlight extension to enable syntax highlighting for JavaScript code blocks.
const { Editor } = require('@tiptap/core');
const CodeBlockLowlight = require('@tiptap/extension-code-block-lowlight');
const lowlight = require('lowlight');
const editor = new Editor({
extensions: [
CodeBlockLowlight.configure({ lowlight })
],
content: '<pre><code class="language-js">console.log("Hello, world!")</code></pre>'
});
Custom Language Support
This code sample shows how to register a custom language (XML in this case) with Lowlight and use it in the Tiptap editor for syntax highlighting.
const { Editor } = require('@tiptap/core');
const CodeBlockLowlight = require('@tiptap/extension-code-block-lowlight');
const lowlight = require('lowlight');
const xml = require('highlight.js/lib/languages/xml');
lowlight.registerLanguage('xml', xml);
const editor = new Editor({
extensions: [
CodeBlockLowlight.configure({ lowlight })
],
content: '<pre><code class="language-xml"><note><to>Tove</to></note></code></pre>'
});
Dynamic Language Switching
This code sample demonstrates how to dynamically change the language of a code block in the Tiptap editor using the CodeBlockLowlight extension.
const { Editor } = require('@tiptap/core');
const CodeBlockLowlight = require('@tiptap/extension-code-block-lowlight');
const lowlight = require('lowlight');
const editor = new Editor({
extensions: [
CodeBlockLowlight.configure({ lowlight })
],
content: '<pre><code class="language-js">console.log("Hello, world!")</code></pre>'
});
// Dynamically change the language of a code block
editor.commands.setCodeBlockLanguage('language-python');
Other packages similar to @tiptap/extension-code-block-lowlight
prismjs
Prism is a lightweight, extensible syntax highlighter. It is a popular choice for syntax highlighting in web applications and can be used with various editors. Unlike @tiptap/extension-code-block-lowlight, Prism is not specifically designed for Tiptap but can be integrated with it.
highlight.js
Highlight.js is a syntax highlighter written in JavaScript. It works in the browser as well as on the server. Similar to Lowlight, it supports a wide range of languages and is highly configurable. It can be used with Tiptap through custom extensions.
codemirror
CodeMirror is a versatile text editor implemented in JavaScript for the browser. It is specialized for editing code and comes with built-in syntax highlighting. While it is a full-fledged code editor, it can be integrated with Tiptap for advanced code editing features.
@tiptap/extension-code-block-lowlight
Introduction
Tiptap is a headless wrapper around ProseMirror – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as New York Times, The Guardian or Atlassian.
Official Documentation
Documentation can be found on the Tiptap website.
License
Tiptap is open sourced software licensed under the MIT license.