One essential element of code editors is syntax highlighting, which employs different colors and fonts to indicate different sections of a codebase. It is essential for improving code readability:
Visual Clarity: Syntax highlighting visually distinguishes between various components in code by applying different colors to keywords, strings, comments, etc. This division makes it easier for developers to see and rapidly understand logic and code structure.
Focus and Scannability: Highlighted code makes it simpler to concentrate on pertinent portions when skimming through code files, which boosts productivity and eases mental strain.
Language comprehending: Syntax highlighting is particularly helpful for developers unfamiliar with a programming language as it aids in comprehending language structures.
When incorporating Tiptap Editor to render content, it provides a convenient method to display content exactly as it appears within the editor itself. By setting the editable boolean expression to false, we disable editing capabilities for the post, ensuring the content is view-only.
However, despite its convenience, using the editor to render content might present certain challenges:
Rendering Performance: The Tiptap Editor can exhibit slower rendering times, especially with complex or extensive content. This delay might impact the overall user experience, necessitating optimization measures for better performance.
SEO Considerations: In applications like a blog, SEO (Search Engine Optimization) is paramount. Rendering content in HTML is recommended for SEO purposes. While Tiptap allows rendering content in HTML, certain styling elements, such as code highlighting, might face challenges.
Styling Limitations: Not all styling or formatting applied within the Tiptap Editor seamlessly translates to the rendered HTML output. For instance, code highlighting does not function as expected when rendering HTML, posing a challenge for maintaining consistent styles across the content.
To address the limitations in styling elements like code highlighting when rendering content from the Tiptap Editor into HTML, leveraging external libraries such as react-syntax-highlight can offer a viable solution.
Introducing react-syntax-highlight:
The react-syntax-highlight library provides a robust solution for syntax highlighting in React applications. It enables the presentation of code snippets with enhanced readability and visual appeal by applying syntax highlighting to various programming languages.
Library Installation: Begin by installing react-syntax-highlight into your Next.js project. Utilize npm or yarn for smooth integration:
npm install react-syntax-highlight or yarn add react-syntax-highlight
Parse Tiptap's HTML output containing code snippets.
Identify code blocks within the HTML content.
Implement the react-syntax-highlight library to apply syntax highlighting to these identified code blocks.
import React from 'react';
import ReactHtmlParser from 'react-html-parser';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism'; // Adjust style as needed
const renderContentWithCodeHighlighting = (content) => {
const parsedContent = ReactHtmlParser(content, {
transform: (node, index) => {
if (node.type === 'tag' && node.name === 'code') {
return (
<SyntaxHighlighter
key={index}
language="javascript" // Define the language for code highlighting
style={oneDark} // Choose the desired style
>
{node.children[0].data}
</SyntaxHighlighter>
);
}
return undefined;
},
});
return parsedContent;
};
// Usage within a React component
const RenderedContent = ({ tiptapHTMLContent }) => {
return (
<div className="rendered-content">
{renderContentWithCodeHighlighting(tiptapHTMLContent)}
</div>
);
};
export default RenderedContent;
Purpose: This function parses Tiptap's HTML output and identifies <code> tags to apply syntax highlighting.
Implementation:
It utilizes ReactHtmlParser to parse the HTML content.
The transform function in ReactHtmlParser checks each node in the HTML content.
When encountering a <code> tag (representing code snippets), it replaces it with a SyntaxHighlighter component.
Parameters such as language (programming language) and style (code highlighting style) are passed to the SyntaxHighlighter.
Implementation:
The RenderedContent functional component takes tiptapHTMLContent as a prop.
It renders a div with the class "rendered-content".
Inside this div, the renderContentWithCodeHighlighting function is invoked, passing the Tiptap Editor's HTML content (tiptapHTMLContent).
Let's add some styles to the SyntaxHighlighter:
Determines if code lines should be enclosed in a parent element.
To prevent readers from having to scroll horizontally to view all of the code, you may force-wrap lines. Alternatively, you may turn off the wrapping to see the raw code unaltered.
<SyntaxHighlighter key={index} language="javascript" style={oneDark} wrapLongLines={true} > {node.children[0].data} </SyntaxHighlighter>
2 ShowLineNumbers:
<SyntaxHighlighter key={index} language="javascript" style={oneDark} wrapLongLines={true} showLineNumbers={true} > {node.children[0].data} </SyntaxHighlighter>
1 OneDark:
import React from 'react';
import ReactHtmlParser from 'react-html-parser';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism';
const renderContentWithCodeHighlighting = (content) => {
const parsedContent = ReactHtmlParser(content, {
transform: (node, index) => {
if (node.type === 'tag' && node.name === 'code') {
return (
<SyntaxHighlighter
key={index}
language="javascript"
style={oneDark}
>
{node.children[0].data}
</SyntaxHighlighter>
);
}
return undefined;
},
});
return parsedContent;
};
2 Dracula:
import React from 'react';
import ReactHtmlParser from 'react-html-parser';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { darcula } from 'react-syntax-highlighter/dist/esm/styles/prism';
const renderContentWithCodeHighlighting = (content) => {
const parsedContent = ReactHtmlParser(content, {
transform: (node, index) => {
if (node.type === 'tag' && node.name === 'code') {
return (
<SyntaxHighlighter
key={index}
language="javascript"
style={darcula}
>
{node.children[0].data}
</SyntaxHighlighter>
);
}
return undefined;
},
});
return parsedContent;
};
You can find many other Themes here.
Incorporating syntax highlighting into Tiptap Editor's HTML output in React applications enhances the visual presentation of code snippets. This blog post demonstrated the process of integrating react-syntax-highlighter to achieve syntax highlighting, ensuring improved readability and comprehension of code within the Tiptap-rendered content.