Installation
First, install the package using npm:React must be installed for this package to work, as it produces React elements as output.
Your First Code Block
The simplest way to use Code Syntactic Sugar is with thehighlight function:
How It Works
Code Syntactic Sugar tokenizes your code string and returns React elements directly. This eliminates the need fordangerouslySetInnerHTML, making your code safer and more maintainable.
The highlight Function
Thehighlight function accepts two parameters:
- code (string, required) - The code string to highlight
- config (CodeSyntacticSugarConfig, optional) - Configuration object for customization
Using with Components
You can easily integrate Code Syntactic Sugar into your React components:Token Types
Code Syntactic Sugar recognizes the following token types:- identifier - Variable and function names
- keyword - JavaScript keywords (const, let, function, etc.)
- string - String literals
- class - Class names and numbers
- property - Object properties
- entity - Special entities
- jsxliterals - JSX tag names and attributes
- sign - Operators and punctuation
- comment - Code comments
- break - Line breaks
- space - Whitespace
All CSS class names use the
--css- prefix and token classes follow the pattern .css__token--<token-type>.Accessing Constants
You can access token types and modifiers programmatically:Next Steps
Styling
Learn how to customize colors and create themes
Line Modifiers
Highlight, add, or remove specific lines
Line Numbers
Add line numbers to your code blocks
Custom Themes
Create your own color themes