flashlightjs
TypeScript icon, indicating that this package has built-in type declarations

0.0.131 • Public • Published

🔦 flashlight


Easy, efficient, and lightweight syntax highlighting.

Contents

Install

npm:

npm i flashlight

Bun:

bun add flashlight

Use

Create a New Default Flashlight Instance

import { Flashlight } from "flashlightjs";

const flashlight = new Flashlight();

Note: The default language is JavaScript, and the defult style is AtomDark.

Create a New Flashlight Instance With a Different Language

import { Flashlight } from "flashlightjs";
import { Python } from "flashlightjs/languages";

const flashlight = new Flashlight([Python]);

Create a new Flashlight Instance With a Different Style

import { Flashlight } from "flashlightjs";
import { AtomDark } from "flashlightjs/styles";

const flashlight = new Flashlight(undefined, AtomDark);

Examples

Highlight JavaScript Code With the Flashlight Class

import { Flashlight } from "flashlightjs";
import { JavaScript } from "flashlightjs/languages";
import { AtomDark } from "flashlightjs/styles";
const code = `function greet(name) {
    return "Hello, " + name + "!";
}`;

const flashlight = new Flashlight([JavaScript], AtomDark);

const highlightedCode = flashlight.highlight(code, "JavaScript");

Highlight JavaScript Code With an Alternative Style

import { Flashlight } from "flashlightjs";
import { JavaScript } from "flashlightjs/languages";
import { AtomDark, VSCode } from "flashlightjs/styles";
const code = `function greet(name) {
    return "Hello, " + name + "!";
}`;

const flashlight = new Flashlight([JavaScript], AtomDark);

const highlightedCode = flashlight.highlight(code, "JavaScript", {
	style: VSCode
});

Highlight Python Code With the Flashlight Class

import { Flashlight } from "flashlightjs";
import { Python } from "flashlightjs/languages";
import { AtomDark } from "flashlightjs/styles";

const code = `def greet(name):
    return "Hello, " + name + "!"
`;

const flashlight = new Flashlight([Python], AtomDark);

const highlightedCode = flashlight.highlight(code, "Python");

Benchmarks

Performance Comparison

Library Time (ms)
flashlight 🔦 2.34
prismjs 5.42
highlight.js 14.05

These benchmarks show flashlight is approximately 6x faster than highlight.js and 2x faster than prismjs when highlighting equivalent code samples.

Why another syntax highlighter?

The main javascript syntax highlighting libraries (Prism, Highlight.js) are built using old standards, not readable, and generally take a less efficient approach to tokenizing code.

Flashlight aims to address these problems by doing the following:

  • Use modern javascript/typescript standards.
  • Create clean documentation for everything.
  • Prioritize expressive syntax.
  • Platform-independent code. Don't prioritize web over node.
  • Use state-machine tokenization over regex pattern matching more info.

Supported Languages

  • [x] JavaScript
  • [x] Python
  • [x] HTML
  • [ ] TypeScript
  • [ ] CSS
  • [ ] JSX
  • [ ] TSX
  • [ ] Rust

Package Sidebar

Install

npm i flashlightjs

Weekly Downloads

2

Version

0.0.131

License

MIT

Unpacked Size

107 kB

Total Files

33

Last publish

Collaborators

  • dmuraco