Expand description
§syntastica-js
Modern and easy syntax highlighting using tree-sitter; use
syntastica
from JavaScript/TypeScript.
The full JavaScript/TypeScript API docs can be found here.
§Basic Usage
import syntastica from '@syntastica/core'
// initialize the module
await syntastica.init()
// load some languages
await syntastica.loadLanguage('node_modules/@syntastica/lang-rust/rust.wasm')
await syntastica.loadLanguage(
'node_modules/@syntastica/lang-javascript/javascript.wasm',
)
// highlight a piece of code once
const rustInput = `fn main() {\n println!("Hello, World!");\n}`
const rustOutput = syntastica.highlight(rustInput, 'rust', 'one::dark')
document.getElementById('rust-code').innerHTML = rustOutput
// highlight a piece of code multiple times
const jsInput = `console.log('Hello, World!')`
const highlights = syntastica.process(jsInput, 'javascript')
// - first to HTML again
const jsOutput1 = syntastica.render(highlights, 'gruvbox::dark')
document.getElementById('js-code').innerHTML = jsOutput1
// - then for terminals (e.g. for usage in nodejs CLIs)
const jsOutput2 = syntastica.render(highlights, 'one::deep', 'terminal')
console.log(jsOutput2)
Structs§
- Lang
Info - Information about a loaded language.
Functions§
- add_
language ⚠ - Add a language to the set.
- get_
builtin_ ⚠theme - Serialize a builtin theme to the raw style map.
- highlight⚠
- Process and render a piece of code in the given language with the given theme.
- process⚠
- Process a piece of code in the given language, and return the [
Highlights
] for a following call torender
. - render⚠
- Render code that was previously processed by calling
process
given the name of aRenderer
.