Skip to content

harrowfung/code-renderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Renderer

A fast, customizable code-to-image rendering API built in Rust. Generate beautiful PNG or SVG images of syntax-highlighted code snippets with extensive theming options.

Features

  • Syntax Highlighting: 50+ languages supported via syntect
  • Multiple Output Formats: PNG (with configurable scale) and SVG
  • Extensive Customization:
    • Multiple built-in themes
    • Override individual syntax token colors (keywords, strings, comments, etc.)
    • Customizable padding, border radius, and shadows
    • macOS or Windows-style window controls
    • Line numbers with configurable start
    • Line highlighting
  • High Quality: Retina-ready PNG output with configurable scale factor
  • Fast: Built with Axum for high-performance async HTTP

Quick Start

Using Docker

docker build -t code-renderer .
docker run -p 3000:3000 code-renderer

From Source

cargo build --release
./target/release/code-renderer

The server starts on http://localhost:3000.

You can configure the listen address and port via CLI flags:

  • --addr <ADDR> (default: 0.0.0.0)
  • --port <PORT> (default: 3000)

Examples:

./target/release/code-renderer --addr 127.0.0.1 --port 8080
./target/release/code-renderer --port 4000

API Reference

POST /render

Render code to an image.

Request Body:

{
  "code": "fn main() {\n    println!(\"Hello, world!\");\n}",
  "language": "rust",
  "output_format": "png",
  "scale": 2,
  "theme": "base16-ocean.dark",
  "title": "main.rs",
  "line_numbers": true,
  "line_number_start": 1,
  "highlight_lines": [2],
  "font_family": "Menlo",
  "font_size": 14,
  "line_height": 1.5,
  "padding": {
    "top": 20,
    "right": 24,
    "bottom": 24,
    "left": 24
  },
  "border_radius": 12,
  "shadow": {
    "enabled": true,
    "color": "rgba(0, 0, 0, 0.55)",
    "blur": 60,
    "offset_x": 0,
    "offset_y": 20
  },
  "window_controls": {
    "enabled": true,
    "style": "macos"
  },
  "theme_overrides": {
    "background": "#1e1e2e",
    "keyword": "#cba6f7",
    "string": "#a6e3a1"
  }
}

Response: PNG or SVG image data

GET /themes

List available syntax themes.

Response:

{
  "themes": ["base16-ocean.dark", "base16-ocean.light", "Solarized (dark)", ...]
}

GET /languages

List supported programming languages.

Response:

{
  "languages": ["Rust", "Python", "JavaScript", "Go", ...]
}

GET /health

Health check endpoint. Returns 200 OK.

Configuration Options

Basic Options

Option Type Default Description
code string required Source code to render
language string required Language for syntax highlighting
output_format "png" | "svg" "png" Output format
scale number 2.0 Scale factor for PNG (2.0 = retina)
theme string "base16-ocean.dark" Base syntax theme
title string null Window title text

Typography

Option Type Default Description
font_family string "Menlo" Font family (system font)
font_size number 14 Font size in pixels
line_height number 1.5 Line height multiplier
line_numbers boolean true Show line numbers
line_number_start number 1 Starting line number
highlight_lines number[] [] Lines to highlight (1-indexed)

Layout

Option Type Default Description
padding.top number 20 Top padding in pixels
padding.right number 24 Right padding
padding.bottom number 24 Bottom padding
padding.left number 24 Left padding
border_radius number 12 Corner radius
width number null Fixed width (auto if null)
max_width number 800 Maximum width

Shadow

Option Type Default Description
shadow.enabled boolean true Enable drop shadow
shadow.color string "rgba(0,0,0,0.55)" Shadow color
shadow.blur number 60 Blur radius
shadow.offset_x number 0 Horizontal offset
shadow.offset_y number 20 Vertical offset

Window Controls

Option Type Default Description
window_controls.enabled boolean true Show window controls
window_controls.style "macos" | "windows" "macos" Control style

Theme Overrides

Override any color in the theme:

Option Description
theme_overrides.background Background color
theme_overrides.foreground Default text color
theme_overrides.line_numbers Line number color
theme_overrides.title Title text color
theme_overrides.line_highlight Highlighted line background
theme_overrides.keyword Keywords (fn, let, if, etc.)
theme_overrides.string String literals
theme_overrides.comment Comments
theme_overrides.function Function names
theme_overrides.type Type names
theme_overrides.number Number literals
theme_overrides.operator Operators
theme_overrides.variable Variable names
theme_overrides.punctuation Punctuation
theme_overrides.window_controls Custom window control colors

Examples

Basic Usage

main.rs

curl -X POST http://localhost:3000/render \
  -H "Content-Type: application/json" \
  -d '{
    "code": "fn main() {\n    let name = \"World\";\n    let count: i32 = 42;\n    // Print greeting\n    println!(\"Hello, {}! Count: {}\", name, count);\n}",
    "language": "rust",
    "output_format": "svg",
    "theme_overrides": {
      "background": "#1e1e2e",
      "foreground": "#cdd6f4",
      "line_numbers": "#6c7086",
      "title": "#bac2de",
      "keyword": "#cba6f7",
      "string": "#a6e3a1",
      "comment": "#6c7086",
      "function": "#89b4fa",
      "type": "#f9e2af",
      "number": "#fab387",
      "operator": "#89dceb",
      "variable": "#cdd6f4",
      "punctuation": "#9399b2"
    },
    "title": "main.rs",
    "scale": 1,
    "border_radius": 0
  }' -o code.svg

Available Themes

  • base16-ocean.dark
  • base16-ocean.light
  • base16-eighties.dark
  • base16-mocha.dark
  • Solarized (dark)
  • Solarized (light)
  • InspiredGitHub

Use GET /themes for the complete list.

Supported Languages

50+ languages including:

  • Rust, Go, Python, JavaScript, TypeScript
  • C, C++, C#, Java, Kotlin
  • Ruby, PHP, Swift, Objective-C
  • HTML, CSS, JSON, YAML, XML
  • SQL, Shell/Bash, Dockerfile
  • And many more...

Use GET /languages for the complete list.

Environment Variables

Variable Default Description
PORT 3000 Server port
RUST_LOG code_renderer=info Log level

Development

# Run in development mode with hot reload
cargo watch -x run

# Run tests
cargo test

# Build release binary
cargo build --release

License

MIT

About

A fast, customizable code-to-image rendering API built in Rust.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors