Skip to content

A code highlighter for the web using Rust + WASM.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE_APACHE
MIT
LICENSE_MIT
Notifications You must be signed in to change notification settings

pedrozaalex/pyranine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyranine

Built with 🦀 + 🕸

About

This is a simple library publishing a WASM module that can be used to highlight code in JavaScript, through the use of syntect.

The name pyranine comes from the dye commonly used in commercial yellow (paper) highlighters (and also in soup lol).

Usage

I've created this for my personal project RepoExplorer, so I've only tested it with Vite. I'm currently using vite-plugin-wasm-pack for that.

// vite.config.ts
import wasmPack from "vite-plugin-wasm-pack";

export default defineConfig({
  // ...
  plugins: [
    // ...
    wasmPack([], ["pyranine"]),
  ],
});
import initHighlighter, * as wasm from "pyranine";

const code = "const foo = 'bar';";

/* The following themes are available:
"base16-ocean.dark",
"base16-eighties.dark",
"base16-mocha.dark",
"base16-ocean.light",
"InspiredGitHub",
"Solarized (dark)",
"Solarized (light)",
*/

function tryHighlightStringAsHTML(
  code: string,
  language: string,
  theme: string
) {
  return new Promise()<string>((resolve) => {
    initHighlighter().then(() =>
      resolve(wasm.highlight(code, language, theme))
    );
  });
}

tryHighlightStringAsHTML(code, "javascript", "base16-ocean.dark").then((c) =>
  console.log(c)
);

Note

You may get an warning saying "pyranine doesn't appear to be written in CJS...". This is because the library is written in Rust and compiled to WASM, and the tooling around that is still being developed. I may also be doing something wrong, but I'm not sure what. If you know, please let me know, or feel free to open a PR.

The important thing is that the library seems to work fine, and the warning can be safely ignored.

About

A code highlighter for the web using Rust + WASM.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE_APACHE
MIT
LICENSE_MIT

Stars

Watchers

Forks

Languages