Class Methods
register( languages )
Registers a language or languages.
Arguments
languages
: A Language object, or an array with Language objects.
import { RyuseiLight, json, typescript } from '@ryusei/light'; RyuseiLight.register( json() ); // Or pass an array: RyuseiLight.register( [ json(), typescript() ] );
compose( extensions )
Registers extensions.
Arguments
extensions
: An object literal with extension functions.
import { RyuseiLight, Gutter, LineNumbers } from '@ryusei/light'; RyuseiLight.compose( { Gutter, LineNumbers } );
tokenize( code, language )
Tokenizes the provided code and returns lines with tokens. This is useful when you make your own renderer.
Arguments
extensions
: A string to tokenize.language
: A language ID.
Return
An array of arrays with tokens([ line1, line2, ... ]
).
Each line is an array with tokens as [ category, text, depth ]
.
const lines = RyuseiLight.tokenize( `console.log( 'hi!' )`, 'js' );
The result will be:
[ [ [ "identifier", "console", 0 ], [ "symbol", ".", 0 ], [ "function", "log", 0 ], [ "bracket", "(", 0 ], [ "space", " ", 0 ], [ "string", "'hi!'", 0 ], [ "space", " ", 0 ], [ "bracket", ")", 0 ], ] ]
Instance Methods
apply( target, options )
Applies the highlighter to elements that matches the selector or the provided element.
Arguments
target
: A selector or an element.options
: Optional. Options.
// With a selector: const ryuseilight = new RyuseiLight(); ryuseilight.apply( 'pre' ); // Or with an element: const pre = document.querySelector( 'pre' ); ryuseilight.apply( pre );
html( code, options )
Returns highlighted HTML by tokenizing the provided code.
Arguments
code
: Code to highlight.options
: Optional. Options.
Return
A Highlighted HTML string.
const ryuseilight = new RyuseiLight(); const html = ryuseilight.html( `console.log( 'hi!' )`, { language: 'js' } ); const div = document.querySelector( 'div' ); div.innerHTML = html;