Code Blocks
Expressive Code is a beautiful solution for code blocks that, under the hood, uses Shiki for syntax highlighting. Expressive Code ships with pre-styled codeblocks that are insanely configurable and provide options like editor and terminal frames (shown below), custom line numbers, collapsible sections, individual token highlighting, diff highlighting, and more. To use these for any provided codeblock, simply add any of the following props after the codeblock’s backticks:
Syntax Highlighting
console.log('This code is syntax highlighted!')ANSI colors:- Regular: Red Green Yellow Blue Magenta Cyan- Bold: Red Green Yellow Blue Magenta Cyan- Dimmed: Red Green Yellow Blue Magenta Cyan
256 colors (showing colors 160-177):160 161 162 163 164 165166 167 168 169 170 171172 173 174 175 176 177
Full RGB colors:ForestGreen - RGB(34, 139, 34)
Text formatting: Bold Dimmed Italic UnderlineCode editor frames
// Using `title="my-test-file.js"`console.log('Title attribute example')// Using `// src/content/index.ts`console.log('File name comment example')Terminal frames
echo "This terminal frame has no title"Write-Output "This one has a title!"Marking full lines & line ranges
// Line 1 - targeted by line number// Line 2// Line 3// Line 4 - targeted by line number// Line 5// Line 6// Line 7 - targeted by range "7-8"// Line 8 - targeted by range "7-8"Selecting line marker types
mark: default marker type, shown as a gray highlightins: inserted lines, shown with a green highlight and a green bar on the leftdel: deleted lines, shown with a red highlight and a red bar on the left
function demo() { console.log('this line is marked as deleted') // This line and the next one are marked as inserted console.log('this is the second inserted line')
return 'this line uses the neutral default marker type'}Adding labels to line markers
// <- This codeblock starts at line 100!
// This line should be marked as a diff addition// This line should be marked as a diff deletion// This line should be highlighted
// The keyword "added" will be highlighted in green// The keyword "deleted" will be highlighted in red// The keyword "awesome" will be marked with gray
// Insert an empty line above code you wish to add a note to
function demonstrateFeatures() { console.log('Hello world!') return true}
function obfuscateString(input) { return Buffer.from(input) .toString('base64') .replace(/[A-Za-z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + (Math.random() > 0.5 ? 1 : -1)), )}
function deleteAllFiles() { fs.rmdirSync('/etc', { recursive: true }) fs.rmdirSync('/usr', { recursive: true }) fs.rmdirSync('/home', { recursive: true }) return 'System wiped!'}
// These lines can be collapsedinterface HidingStuffHere { name: string age: number email: string phone: string}Adding long labels on their own lines
// <- This codeblock starts at line 100!
// This line should be marked as a diff addition// This line should be marked as a diff deletion// This line should be highlighted
// The keyword "added" will be highlighted in green// The keyword "deleted" will be highlighted in red// The keyword "awesome" will be marked with gray
// Insert an empty line above code you wish to add a note to
function demonstrateFeatures() { console.log('Hello world!') return true}
function obfuscateString(input) { return Buffer.from(input) .toString('base64') .replace(/[A-Za-z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + (Math.random() > 0.5 ? 1 : -1)), )}
function deleteAllFiles() { fs.rmdirSync('/etc', { recursive: true }) fs.rmdirSync('/usr', { recursive: true }) fs.rmdirSync('/home', { recursive: true }) return 'System wiped!'}
// These lines can be collapsedinterface HidingStuffHere { name: string age: number3 collapsed lines
email: string phone: string}Using diff-like syntax
this line will be marked as insertedthis line will be marked as deletedthis is a regular linefunction thisIsJavaScript() { // This entire block gets highlighted as JavaScript, // and we can still add diff markers to it! console.log('Old code to be removed') console.log('New and shiny code!')}Marking individual text inside lines
// Plaintext search stringsfunction demo() { // Mark any given text inside lines return 'Multiple matches of the given text are supported'}Marking individual text inside lines
// Regular expressionsconsole.log('The words yes and yep will be marked.')# Regular expressionsecho "Test" > /home/test.txt// Regular expressionsIf you only want to mark certain parts matched by your regular expression, you can use capture groups.
For example, the expression `/ye(s|p)/` will match yes and yep, but only mark the character s or p.// Regular expressionsTo prevent this special treatment of capture groups, you can convert them to non-capturing groups by adding ?: after the opening parenthesis.
This block uses `/ye(?:s|p)/`, which causes the fullmatching words "yes" and "yep" to be marked.// Selecting inline marker types (mark, ins, del)function demo() { console.log('These are inserted and deleted marker types') // The return statement uses the default marker type return true}Configuring word wrap per block
// Example with wrapfunction getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}// Example with wrap=falsefunction getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}Configuring indentation of wrapped lines
// Example with preserveIndent (enabled by default)function getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}// Example with preserveIndent=falsefunction getLongString() { return 'This is a very long string that will most probably not fit into the available space unless the container is extremely wide'}Collapsible sections
5 collapsed lines
// All this boilerplate setup code will be collapsedimport { someBoilerplateEngine } from '@example/some-boilerplate'import { evenMoreBoilerplate } from '@example/even-more-boilerplate'
const engine = someBoilerplateEngine(evenMoreBoilerplate())
// This part of the code will be visible by defaultengine.doSomething(1, 2, 3, calcFn)
function calcFn() { // You can have multiple collapsed sections3 collapsed lines
const a = 1 const b = 2 const c = a + b
// This will remain visible console.log(`Calculation result: ${a} + ${b} = ${c}`) return c}
4 collapsed lines
// All this code until the end of the block will be collapsed againengine.closeConnection()engine.freeMemory()engine.shutdown({ reason: 'End of example boilerplate code' })Displaying line numbers per block
// This code block will show line numbersconsole.log('Greetings from line 2!')console.log('I am on line 3')// Line numbers are disabled for this blockconsole.log('Hello?')console.log('Sorry, do you know what line I am on?')// Changing the starting line numberconsole.log('Greetings from line 5!')console.log('I am on line 6')