npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@syntect/node

v0.0.4

Published

Syntect (Syntax highlighter in Rust) for Node.js

Downloads

45

Readme

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.