A Val Town val for viewing annotated source code with interactive features including syntax highlighting, hover effects, and explanatory popups.
Visit the val URL without any path to see usage instructions and examples:
https://your-val-url.web.val.run/
This will display an interactive usage page with:
Visit your val URL with the path to the source code you want to annotate:
https://your-val-url.web.val.run/{username}/{valname}@{version}/{filename}
Example:
https://your-val-url.web.val.run/nbbaier/sqliteExplorerApp@100-main/main.tsx
This will fetch the source from https://esm.town/v/nbbaier/sqliteExplorerApp@100-main/main.tsx
and display it with annotations.
You can override the default functionality by providing custom val URLs as query parameters:
?syntaxHighlighter=https://your-custom-highlighter.web.val.run
Your custom syntax highlighter should accept POST requests with:
{ "code": "source code string", "language": "javascript|typescript|python|etc" }
And return HTML with syntax-highlighted code.
?blockDetector=https://your-custom-detector.web.val.run
Your custom block detector should accept POST requests with:
{ "code": "source code string", "line": 10, "column": 5 }
And return JSON with:
{ "start": 8, "end": 15, "type": "function|class|control|statement" }
?blockExplainer=https://your-custom-explainer.web.val.run
Your custom block explainer should accept POST requests with:
{ "code": "source code string", "blockInfo": { "start": 8, "end": 15, "type": "function" } }
And return plain text or HTML explanation.
https://your-val-url.web.val.run/nbbaier/sqliteExplorerApp@100-main/main.tsx?blockExplainer=https://my-ai-explainer.web.val.run&syntaxHighlighter=https://my-custom-highlighter.web.val.run
The val automatically detects language from file extensions:
.ts
, .tsx
→ TypeScript.js
, .jsx
→ JavaScript.py
→ Python.rs
→ Rust.go
→ Go.java
→ Java.cpp
, .cc
, .cxx
→ C++.c
→ C.css
→ CSS.html
→ HTML.json
→ JSON.md
→ MarkdownThe default block detector uses simple heuristics to identify:
function
or =>
class
if
, for
, or while
https://esm.town/v/{path}
The val is structured with three main pluggable functions:
defaultSyntaxHighlighter()
: Handles code syntax highlightingdefaultBlockDetector()
: Identifies logical code blocksdefaultBlockExplainer()
: Generates explanations for code blocksEach function can be replaced with custom implementations via query parameters, making the system highly extensible.
The project includes several example custom functions to demonstrate extensibility:
Uses OpenAI to generate detailed explanations of code blocks. Requires OPENAI_API_KEY
environment variable.
Usage:
?blockExplainer=https://your-val-url.web.val.run/examples/ai-explainer.ts
Provides more sophisticated block detection with support for:
Usage:
?blockDetector=https://your-val-url.web.val.run/examples/enhanced-detector.ts
Enhanced syntax highlighting with:
Usage:
?syntaxHighlighter=https://your-val-url.web.val.run/examples/custom-highlighter.ts
https://your-val-url.web.val.run/nbbaier/sqliteExplorerApp@100-main/main.tsx?blockDetector=https://your-val-url.web.val.run/examples/enhanced-detector.ts&blockExplainer=https://your-val-url.web.val.run/examples/ai-explainer.ts&syntaxHighlighter=https://your-val-url.web.val.run/examples/custom-highlighter.ts