Giter Site home page Giter Site logo

syntect-js's Introduction

Syntect for Node.js and WASM

Syntect (Syntax highlighter in Rust) for Node.js and WebAssembly.

Syntect highlights code with TextMate's .tmTheme theme. And the highlighting result is independent of the theme (using CSS classes, unlike Shiki, which uses inline styles) so you can switch themes in your website by only switching CSS.

# Install the meta-package
yarn add syntect       # Auto choose --
                       # Native binding for Node.js
                       # WebAssembly for browser

# Or Install manually
yarn add @syntect/node # Node.js native binding
yarn add @syntect/wasm # WebAssembly

Usage

Note: See WASM Notes if you encountered any error using the WASM version.

import { getCSS, highlight } from "syntect" /* or "@syntect/node" / "@syntect/wasm" */;

// Generate CSS code from .tmTheme file
const result = getCSS(
  // The text content of .tmTheme file
  tmThemeText,
  // The prefix of CSS class name,
  "hl-"
);
console.log(result.css);
// Only needed once.
// You could save the CSS file for your website.
// See also [CLI]

// Highlight code
const highlightResult = highlight(
  // The code to highlight
  "#include <cstdio>",
  // The language name or file extension. Use "plain" or "plaintext" for plain text
  "cpp",
  // The prefix of CSS class name,
  "hl-"
);
console.log(result.html);

CLI

There's a cli utility for getCSS function. You should install the Node.js version (syntect or @syntect/node) to use it.

# Install with `yarn add syntect` or `yarn add @syntect/node`

# yarn syntect-css <prefix>
cat my-theme.tmTheme | yarn syntect-css hl-

# e.g. the tomorrow theme
curl https://raw.githubusercontent.com/chriskempson/textmate-tomorrow-theme/master/Tomorrow.tmTheme | yarn syntect-css hl-

WASM Notes

The WASM version has all code in ES Modules.

To use the WASM version with Node.js, please add --experimental-wasm-modules to your Node.js command line options to enable Node.js to load .wasm modules. For older version of Node.js you'll also need --experimental-modules to enable basic ES Modules support.

To use the WASM version in browser, you may need to configure your bundler:

  • Webpack 5: enable experiments.asyncWebAssembly.
  • Vite: not currently supported but related works are in progress.

syntect-js's People

Contributors

menci avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

syntect-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.