Skip to content

rudgal/best-code-comments-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Best Code Comments API

Sample comment cards

An API that shares legendary source code comments as embeddable image or JSON.

Hono Bun TypeScript Sharp

What is this?

This project grew out of revisiting the classic Stack Overflow thread:

Those snippets are too good to stay buried in an old discussion, so this service curates and repackages them. Use the API to

  • fetch JSON for bots, dashboards, or daily inspiration widgets,
  • embed themed SVG/PNG cards in READMEs, slides, and blog posts,
  • or simply hit /api/random for a laugh whenever you need one.

Endpoints

Method Path Description Query params
GET /health Service status and metadata
GET /api/random Random comment with optional filters tags, author, maxLines, minPopularity
GET /api/comment/:id Fetch a comment by numeric ID
GET /comment.svg
/comment.png
Render a comment as SVG or PNG (via Sharp) id, tags, author, maxLines, minPopularity, theme, width

Query Parameters

Name Default Description
tags Comma-separated tag filter; see src/types.ts for supported values.
author Case-insensitive substring match on the author field.
maxLines 25 Maximum number of lines the comment should have.
minPopularity 2 Minimum stackoverflow popularity score the comment should have.
id Request a specific comment by numeric ID.
theme light Theme for rendered SVG/PNG cards; supports light and dark.
width 820 Width of the rendered SVG in pixels (string or number).

SVG/PNG requests without an id redirect to a random comment that fits your filters, including maxLines and minPopularity.

Examples

JSON (random)

curl https://localhost:3000/api/random
{
  "id": 66,
  "author": "Yuval",
  "date": "2011-07-15 11:57:51Z",
  "source": "https://stackoverflow.com/a/400211",
  "popularity": 105,
  "tags": [
    "humor",
    "meta"
  ],
  "content": "// This comment is self explanatory."
}

SVG (random)

curl "https://localhost:3000/comment.svg" -o comment.svg

SVG example (random)

SVG (dark theme)

curl "https://localhost:3000/comment.svg?id=135&theme=dark" -o comment.svg

SVG example (specific id, dark-theme)

PNG (custom width)

curl "https://localhost:3000/comment.png?id=177&width=1300" -o comment.png

PNG example (specific id, specific width)

Quick Start

bun install
bun run build    # preprocess comment data
bun run dev      # runs bun --hot src/index.ts

Visit http://localhost:3000/health to confirm the service is running. The /all route is available in development for a quick gallery of every comment.

Project Structure

.
├── src/
│   ├── components/
│   │   ├── commentsAllPage.ts    # Dev-only gallery HTML renderer
│   │   └── commentsSvg.ts        # SVG string renderer
│   ├── data/                     # Preprocessed comment dataset (JSON)
│   ├── index.ts                  # Hono application entry point
│   ├── types.ts                  # Shared type definitions
│   └── utils.ts                  # Filtering helpers and constants
├── scripts/                      # Data scraping/build tools
├── fonts/                        # Local fallbacks for Sharp/Vercel
├── html/                         # Scraped HTML snapshots
└── README.md

Rendering Details

  • SVGs import JetBrains Mono and Roboto from Google Fonts (distributed under the SIL Open Font License and Apache 2.0 respectively).
  • Text wrapping keeps whitespace intact for ASCII art comments.
  • PNGs are rasterised from SVG using Sharp, so fonts must be accessible even in headless environments (handled by inline imports and optional fontconfig).

🙌 Thanks to the Stack Overflow community.

If this API made you smile, consider sharing it and starring the repo.

About

API that provides legendary source code comments as embeddable images or JSON.

Topics

Resources

License

Stars

Watchers

Forks