An open API service indexing awesome lists of open source software.

https://github.com/thuongtruong109/flashot

πŸ“Ÿ Rapidly snapshot code snippets as images. A best alternative beyond carbon.sh and ray.so
https://github.com/thuongtruong109/flashot

bun bunjs code code-snippets code-to-image code2image jsr node nodejs npm package screenshot snapshot snippets

Last synced: 6 months ago
JSON representation

πŸ“Ÿ Rapidly snapshot code snippets as images. A best alternative beyond carbon.sh and ray.so

Awesome Lists containing this project

README

          


Line banner

![CI status](https://img.shields.io/github/actions/workflow/status/thuongtruong109/flashot/ci.yml?branch=main&label=ci&logo=github&style=flat&colorA=080f12&colorB=1fa669)
[![Npm version](https://img.shields.io/npm/v/flashot?style=flat&label=version&colorA=080f12&colorB=1fa669&logo=npm)](https://npmjs.com/package/flashot)
[![Npm downloads](https://img.shields.io/npm/dm/flashot?style=flat&logo=npm&colorA=080f12&colorB=1fa669)](https://npmjs.com/package/flashot)
[![Jsr version](https://img.shields.io/jsr/v/%40thuongtruong/flashot?style=flat&label=version&colorA=080f12&colorB=1fa669&logo=jsr)](https://jsr.io/@thuongtruong/flashot)
[![Endpoint Badge](https://img.shields.io/endpoint?url=https%3A%2F%2Fjsr.io%2Fbadges%2F%40thuongtruong%2Fflashot%2Fscore&style=flat&logo=jsr&label=score&labelColor=080f12&color=1fa669)](https://jsr.io/@thuongtruong/flashot/score)
[![JSDocs](https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&logo=javascript&colorA=080f12&colorB=1fa669)](https://www.jsdocs.io/package/flashot)
[![Sponsor](https://img.shields.io/badge/sponsor-1fa669?logo=githubsponsors&labelColor=080f12)](https://github.com/sponsors/thuongtruong109)

**Flashot** is the **blazing-fast image generation tool** for code snippets with elegant design and flawless performance

**Super fast:** (generated in **~135ms**)

Example output

## ✨ Features

- πŸ–₯️ **Used from CLI:** Use Flashot as a command-line tool for quick image generation
- πŸ“₯ **Used from import:** Use Flashot as an importable module in your code:
- πŸ‘©β€πŸ’» **Inline code support:** Easily convert inline code snippets to images
- 🌐 **URL support:** Fetch code snippets directly from URLs
- πŸ—‚οΈ **File path support:** Convert code snippets from local files
- πŸ—ƒοΈ **Buffer support:** Convert code snippets from buffers
- πŸ–ΌοΈ **Desktop App:** Native desktop application built with Tauri for a seamless user experience
- 🎨 **Customizable styles:** Choose from various options to match your style
- πŸ“¦ **Lightweight:** Minimal dependencies to keep your project lean
- πŸ“ **Line numbers:** Support for displaying & customizing line numbers
- πŸ–οΈ **Highlighting:** Support for custom syntax highlighting
- πŸ—„οΈ **Render caching:** Efficiently handles caching for improved performance (render, tokens, sizes, fonts)
- πŸͺ“ **Multi-format support:** Generate images in various formats (png, jpeg, webp, avif)
- πŸ–ΌοΈ **High-quality output:** Generates crisp and clear images which keep the original code's formatting intact
- ⚑ **Blazing fast:** Optimized for speed, ensuring quick image generation
- πŸ› οΈ **Easy to use:** Easy to integrate into your projects with a simple API
- πŸ”· **TypeScript support:** Fully typed for better developer experience
- πŸ” **Extensive testing:** Thoroughly tested with a comprehensive suite of unit tests
- πŸ”‹ **Easy integration:** Simple API for seamless integration into your projects
- πŸ”§ **Flexible environment support:** Works seamlessly in various environments (Node.js, Bun, Deno, Workers, …)

## πŸ“¦ Installation

### βš™οΈ For API usage, install via your favorite package manager:

```bash
npm install flashot
```

```bash
yarn add flashot
```

```bash
pnpm add flashot
```

```bash
bun install flashot
```

```bash
deno add jsr:@thuongtruong109/flashot
```

```bash
npx jsr add @thuongtruong/flashot
```

### πŸ›  For CLI usage, install globally via npm:

```bash
npm install -g flashot
```

Then quick check from terminal with CLI runner

```bash
flashot -h
```

### πŸ–₯️ For Desktop app usage, build from source:

#### Prerequisites

- [Node.js](https://nodejs.org/) (version 18 or higher)
- [Rust](https://rustup.rs/) (latest stable)
- [Tauri CLI](https://tauri.app/v1/guides/getting-started/prerequisites)

#### Build Instructions

```bash
git clone https://github.com/thuongtruong109/flashot.git
cd flashot

bun install

bun run app:build
```

The built application will be available in `src-tauri/target/release/`.

## πŸͺ” Usage Example

### βš™οΈ For API usage, you can import the functions from the package:

##### Inline code

```js
import { writeFile } from "node:fs/promises";
import { codeToImg } from "flashot";

const buffer = await codeToImg('console.log("hello, world!");', {
/* add more options*/
});
await writeFile("inline.webp", buffer);
```

##### Raw content url

```js
import { writeFile } from "node:fs/promises";
import { urlToImg } from "flashot";

const buffer = await urlToImg("https://randomfox.ca/floof", {
/* add more options*/
format: OutputFormat.Png,
});
await writeFile("url.png", buffer);
```

##### Buffer

```js
import { writeFile } from "node:fs/promises";
import { bufferToImg } from "flashot";

const buffer =
"";
const img = await bufferToImg(buffer, {
/* add more options*/
});
await writeFile("buffer.png", img);
```

##### Path dir

```js
import { writeFile } from "node:fs/promises";
import { pathToImg } from "flashot";

const img = await pathToImg("../package.json", {
/* add more options*/
});
await writeFile("path.png", img);
```

### πŸ›  For CLI usage, you can run the commands from your terminal:

##### Inline code

```bash
flashot code "console.log('Hello world')" <...options>
```

##### Raw content url

```bash
flashot url "https://randomfox.ca/floof" <...options>
```

##### Buffer

```bash
flashot buffer "" <...options>
```

##### Path dir

```bash
flashot path "../package.json" <...options>
```

### πŸ–₯️ For Desktop app usage, launch the built application:

After building the desktop app as described in the installation section, run the executable from `src-tauri/target/release/`. The desktop app provides a graphical interface for all Flashot features, including code input, option customization, and image generation.

## Options

### πŸ›  CLI️ options

```bash
Usage: flashot [command] [options]

Commands:
code Convert code string to image
file Convert code file to image
url Convert code from URL to image
buffer Convert hex buffer string to image
help [command] display help for command

Options:
-V, --version output the version number
-o, --output output image file path (default: "tmp.webp")
-l, --lang programming language (default: "js")
-t, --theme syntax highlighting theme (default: "dracula")
-f, --font font family to use (default: "https://fonts.bunny.net/jetbrains-mono/files/jetbrains-mono-latin-400-normal.woff2")
--format output image format (png, jpeg, webp, avif) (default: "webp")
-q, --quality image quality (1-100) (only for jpeg) (default: "100")
-g, --gap gap between lines (default: "1")
-p, --style-padding padding around code (default: "25")
--style-border-radius border radius (default: "8")
-b, --bg background color (default: "not needed!")
-w, --width image width (default: "not needed!")
-H, --height image height (default: "not needed!")
-L, --line-numbers-enabled enable line numbers (default: false)
--line-numbers-start-from line number start (default: "1")
--line-numbers-color line number color (default: "#7b7f8b")
--line-numbers-margin-right line number margin right (default: "0")
--highlight-enabled enable line highlighting (default: false)
--highlight-background-color highlight background color (default: "#347faa23")
--highlight-border-radius highlight border radius (default: "0")
--highlight-at highlight line number (default: "1")
--highlight-depth highlight depth (shadow effect) (default: "1")
-v, --verbose enable verbose output
-h, --help display help for command
```

### βš™οΈ API options (default is not needed)

```js
const defaultOptions = {
lang: "ts", // default is javascript
theme: "ayu-dark", // default is github-dark
font: "https://fonts.bunny.net/ubuntu-sans-mono/files/ubuntu-sans-mono-latin-400-normal.woff2", // default is bunny.net/jetbrains-mono.
format: "png", // default is "webp" (options: png, jpeg, webp, avif)
quality: 100, // default is 100 (1-100), only applies to jpeg formats
width: 800, // default is system's width
height: 400, // default is system's height
bg: "transparent", // default is system's background
gap: 1, // gap between lines (default is 1)
style: {
borderRadius: 10, // default is 8
padding: 30, // default is 25
// ... more custom styles
},
lineNumbers: {
enabled: true, // default is false
startFrom: 5, // default is 1
color: "#d64141ff", // default is #7b7f8b
marginRight: 2, // default is 0
},
highlight: {
enabled: true, // default is false
backgroundColor: "#25ce5da1", // default is #347faa23,
borderRadius: 2, // default is 0
at: 2, // start from at line - default is 1
depth: 3, // total lines - default is 1
},
};
```

| Option | Description | Default |
| ------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `lang` | Code language ([supported](https://shiki.style/languages)) | `"js"` |
| `theme` | Rendering theme ([supported](https://shiki.style/themes)) | `"github-dark"` |
| `font` | Font for rendering (URL/Buffer/ArrayBuffer) | [`Jetbrains Mono`](https://fonts.bunny.net/jetbrains-mono/files/jetbrains-mono-latin-400-normal.woff2) |
| `format` | Output image format (`png`, `jpeg`, `webp`, `avif`) | `webp` |
| `quality` | Image quality (1-100) for JPEG formats | `100` |
| `width` | Image width | System default |
| `height` | Image height | System default |
| `bg` | Background color | Theme's background |
| `gap` | Gap between lines | `1` |
| `style` | Additional container styles ([docs](https://takumi.kane.tw/docs/deep-dives/stylesheets)) | `{ borderRadius: 8, padding: 25 }` |
| `lineNumbers` | Line number styles | `{ enabled: false, color: '#7b7f8b', marginRight: 0 }` |
| `highlight` | Syntax highlighting styles | `{ enabled: false, backgroundColor: '#347faa23', borderRadius: 0, at: 1, depth: 1 }` |

## πŸ§ͺ Code Coverage

| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
| --------- | ------- | -------- | ------- | ------- | ----------------- |
| All files | 99.17 | 81.25 | 100 | 99.17 |
| core.ts | 100 | 100 | 100 | 100 |
| index.ts | 100 | 100 | 100 | 100 |
| shared.ts | 100 | 100 | 100 | 100 |
| utils.ts | 98.36 | 78.57 | 100 | 98.36 | 67 |

![Coverage](./public/coverage.png)

## 🏁 Benchmarks

| No | Task name | Latency avg (ns) | Latency med (ns) | Throughput avg (ops/s) | Throughput med (ops/s) | Samples |
| --- | ---------- | ------------------ | --------------------- | ---------------------- | ---------------------- | ------- |
| 1 | 1 lines | 10175455 Β± 1.43% | 10075900 Β± 106800 | 99 Β± 1.05% | 99 Β± 1 | 99 |
| 2 | 10 lines | 39056303 Β± 0.45% | 38845500 Β± 405750 | 26 Β± 0.44% | 26 Β± 0 | 64 |
| 3 | 100 lines | 343126433 Β± 0.29% | 343616650 Β± 2711850 | 3 Β± 0.29% | 3 Β± 0 | 64 |
| 4 | 1000 lines | 1726190939 Β± 0.46% | 1715535300 Β± 13138650 | 1 Β± 0.45% | 1 Β± 0 | 64 |

## βš“ Docker Deployment

Flashot API is available as Docker images on both GitHub Container Registry and Docker Hub.

### Quick Start with Docker

```bash
# Pull from GitHub Container Registry
docker pull ghcr.io/thuongtruong109/flashot-api:latest

# Or pull from Docker Hub
docker pull thuongtruong109/flashot-api:latest

# Run the container
docker run -p 8080:8080 ghcr.io/thuongtruong109/flashot-api:latest
```

### Docker Compose

```bash
# Clone the repository
git clone https://github.com/thuongtruong109/flashot.git
cd flashot

# Start with Docker Compose
docker-compose up -d
```

### Container Registries

- **GitHub Container Registry**: `ghcr.io/thuongtruong109/flashot-api`
- **Docker Hub**: `thuongtruong109/flashot-api`

### Available Tags

- `latest` - Latest stable release
- `main` - Latest development build
- `main-` - Specific commit builds

### API Endpoints

Once running, the API provides several endpoints:

- `POST /` - Convert code string to image
- `POST /url` - Convert code from URL to image
- `POST /file` - Convert code from file path to image
- `POST /buffer` - Convert code from hex buffer to image
- `GET /options` - Get available configuration options
- `GET /health` - Health check endpoint

Example usage:

```bash
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{
"code": "console.log(\"Hello, World!\");",
"options": {
"lang": "javascript",
"theme": "github-dark",
"format": "png"
}
}' \
--output hello-world.png
```

For detailed deployment instructions, see [api/DEPLOYMENT.md](./api/DEPLOYMENT.md).

## πŸ“š Technologies

- ⚑ **[Bun](https://bun.sh)** - Fast all-in-one JavaScript runtime and toolkit
- πŸ—οΈ **[TypeScript](https://www.typescriptlang.org/)** - Type-safe development with strict mode enabled
- πŸ“¦ **[Vite](https://vitejs.dev/)** - Lightning-fast build tool with optimized bundling
- πŸͺ“ **[Tsdown](https://rolldown/tsdown)** - A powerful tool for TypeScript package
- πŸ§ͺ **[Vitest](https://vitest.dev/)** - Blazing fast unit testing & interactive test UI framework
- 🎨 **[Shiki](https://github.com/shikijs/shiki)** and **[Takumi](https://github.com/kane50613/takumi)** - Render container highlight
- πŸ“ **[Biome](https://biomejs.dev/)** - Fast formatter and linter for consistent code style
- πŸš€ **Dual Module Support** - ESM and CommonJS output with proper type definitions
- πŸ”₯ **[ESLint](https://eslint.org/)** - Advanced linting with TypeScript and SonarJS rules
- 🧩 **[Lefthook](https://github.com/evilmartians/lefthook)** and **[Commitlint](https://commitlint.js.org/)** - Automated Git hooks for linting and formatting
- πŸ› οΈ **[Tinybench](https://github.com/tinybench/tinybench)** - A tiny benchmarking library for measuring performance
- πŸ–₯️ **[Commander](https://github.com/tj/commander.js)** - A popular library for building command-line interfaces
- πŸ–ΌοΈ **[Tauri](https://tauri.app/)** - Build smaller, faster, and more secure desktop applications with a web frontend

## 🀝 Contributing

Contributions are welcome! This starter kit uses:

- Automated code formatting and linting
- Comprehensive test coverage requirements

Please ensure all tests pass and code quality checks succeed before submitting a PR.

> Check [CONTRIBUTING.md](./.github/CONTRIBUTING.md) for more information.

## πŸ’¬ Discussions

Head over to the [discussions](https://github.com/thuongtruong109/flashot/discussions/new/choose) to share your ideas.

## πŸ“„ License

[MIT License](./LICENSE) - [Tran Nguyen Thuong Truong](mailto:thuongtruongofficial@gmail.com)