You can access search results via JSON API by adding format=json
to your query:
https://codesearch.val.run/$%7Bsuccess?q=function&page=2&format=json
For typeahead suggestions, use the /typeahead
endpoint:
https://codesearch.val.run/typeahead?q=function
Returns an array of strings in format "username" or "username/projectName"
Found 31553 results for "function"(1366ms)
2122// Default syntax highlighter
23async function defaultSyntaxHighlighter(code: string, language: string): Promise<string> {
24// Using Prism.js for syntax highlighting
25return `
3233// Default block detector - finds the smallest logical block containing the cursor
34async function defaultBlockDetector(code: string, line: number, column: number): Promise<{start: number, end: number, type: string}> {
35// Simple implementation - finds function, class, or statement blocks
36const lines = code.split('\n');
37
40
41// Simple heuristics for block detection
42if (currentLine.includes('function') || currentLine.includes('=>')) {
43return findFunctionBlock(lines, line);
44} else if (currentLine.includes('class')) {
45return findClassBlock(lines, line);
5253// Default block explainer
54async function defaultBlockExplainer(code: string, blockInfo: {start: number, end: number, type: string}): Promise<string> {
55const lines = code.split('\n');
56const blockCode = lines.slice(blockInfo.start - 1, blockInfo.end).join('\n');
57
58switch (blockInfo.type) {
59case 'function':
60return `This is a function block. Functions are reusable pieces of code that perform specific tasks. This function contains ${blockInfo.end - blockInfo.start + 1} lines of code.`;
61case 'class':
62return `This is a class definition. Classes are blueprints for creating objects with shared properties and methods.`;
70}
7172// Helper functions for block detection
73function findFunctionBlock(lines: string[], startLine: number): {start: number, end: number, type: string} {
74let braceCount = 0;
75let start = startLine;
76let end = startLine;
77
78// Find function start
79while (start > 0 && !lines[start - 1].includes('function') && !lines[start - 1].includes('=>')) {
80start--;
81}
82
83// Find function end by counting braces
84for (let i = start - 1; i < lines.length; i++) {
85const line = lines[i];
93}
94
95return { start, end, type: 'function' };
96}
9798function findClassBlock(lines: string[], startLine: number): {start: number, end: number, type: string} {
99let braceCount = 0;
100let start = startLine;
121}
122123function findControlBlock(lines: string[], startLine: number): {start: number, end: number, type: string} {
124let braceCount = 0;
125let start = startLine;
141}
142143function findStatementBlock(lines: string[], startLine: number): {start: number, end: number, type: string} {
144return { start: startLine, end: startLine, type: 'statement' };
145}
146147function escapeHtml(text: string): string {
148return text
149.replace(/&/g, '&')
154}
155156function generateUsagePage(): string {
157return `
158<!DOCTYPE html>
194background: #2563eb;
195}
196.custom-function-link {
197display: inline-block;
198background: #10b981;
204font-size: 12px;
205}
206.custom-function-link:hover {
207background: #059669;
208}
233<div>
234<h3 class="font-semibold text-gray-700 mb-2">🔧 Extensible</h3>
235<p class="text-gray-600 text-sm">Override default functions with custom implementations</p>
236</div>
237</div>
249<div>
250<a href="/std/blob" class="example-link">📦 Val Town Blob Storage</a>
251<span class="text-sm text-gray-500">- Simple utility functions</span>
252</div>
253<div>
263264<div class="feature-card">
265<h2 class="text-2xl font-semibold text-gray-800 mb-4">🎛️ Custom Functions</h2>
266<p class="text-gray-600 mb-4">Enhance the annotator with custom implementations:</p>
267
270<div class="space-y-2">
271<div>
272<a href="/examples/ai-explainer.ts" class="custom-function-link">🤖 AI Explainer</a>
273<span class="text-sm text-gray-600">- Uses OpenAI for detailed code explanations</span>
274</div>
275<div>
276<a href="/examples/enhanced-detector.ts" class="custom-function-link">🔍 Enhanced Detector</a>
277<span class="text-sm text-gray-600">- Sophisticated block detection with more code constructs</span>
278</div>
279<div>
280<a href="/examples/custom-highlighter.ts" class="custom-function-link">🎨 Custom Highlighter</a>
281<span class="text-sm text-gray-600">- Enhanced highlighting with complexity indicators</span>
282</div>
284</div>
285286<h3 class="text-lg font-semibold text-gray-700 mb-2">Usage with Custom Functions:</h3>
287<div class="code-example text-sm">
288# With AI explanations
308</div>
309<div>
310<a href="/examples/README.md" class="text-blue-600 hover:text-blue-800 font-medium">🔧 Custom Functions Guide</a>
311<p class="text-sm text-gray-600">Learn how to create and use custom highlighters, detectors, and explainers</p>
312</div>
373const language = getLanguageFromPath(valPath);
374
375// Get custom function URLs from query parameters
376const syntaxHighlighterUrl = c.req.query('syntaxHighlighter');
377const blockDetectorUrl = c.req.query('blockDetector');
395});
396397function getLanguageFromPath(path: string): string {
398const ext = path.split('.').pop()?.toLowerCase();
399switch (ext) {
414}
415416async function generateAnnotatedPage(
417sourceCode: string,
418language: string,
461}
462463function generateStyles(): string {
464return `<style>
465.code-container {
534}
535536function generateJavaScript(
537sourceCode: string,
538syntaxHighlighterUrl?: string,
549let currentBlock = null;
550
551// Custom function URLs
552const syntaxHighlighterUrl = ${JSON.stringify(syntaxHighlighterUrl || null)};
553const blockDetectorUrl = ${JSON.stringify(blockDetectorUrl || null)};
555
556// Wait for Prism to load and process the code
557document.addEventListener('DOMContentLoaded', function() {
558setTimeout(initializeInteractivity, 100);
559});
560
561function initializeInteractivity() {
562const codeElement = codeContainer.querySelector('code');
563if (!codeElement) {
609}
610
611async function detectBlock(code, line, column) {
612if (blockDetectorUrl) {
613const response = await fetch(blockDetectorUrl, {
622}
623
624async function explainBlock(code, blockInfo) {
625if (blockExplainerUrl) {
626const response = await fetch(blockExplainerUrl, {
635}
636
637function defaultBlockDetector(code, line, column) {
638const lines = code.split('\\n');
639const currentLine = lines[line - 1] || '';
640
641if (currentLine.includes('function') || currentLine.includes('=>')) {
642return findFunctionBlock(lines, line);
643} else if (currentLine.includes('class')) {
644return findClassBlock(lines, line);
650}
651
652function defaultBlockExplainer(code, blockInfo) {
653switch (blockInfo.type) {
654case 'function':
655return \`This is a function block. Functions are reusable pieces of code that perform specific tasks. This function contains \${blockInfo.end - blockInfo.start + 1} lines of code.\`;
656case 'class':
657return 'This is a class definition. Classes are blueprints for creating objects with shared properties and methods.';
665}
666
667function findFunctionBlock(lines, startLine) {
668let braceCount = 0;
669let start = startLine;
670let end = startLine;
671
672// Find function start
673while (start > 0 && !lines[start - 1].includes('function') && !lines[start - 1].includes('=>')) {
674start--;
675}
676
677// Find function end by counting braces
678for (let i = start - 1; i < lines.length; i++) {
679const line = lines[i];
687}
688
689return { start, end, type: 'function' };
690}
691
692function findClassBlock(lines, startLine) {
693let braceCount = 0;
694let start = startLine;
715}
716
717function findControlBlock(lines, startLine) {
718let braceCount = 0;
719let start = startLine;
735}
736
737function findStatementBlock(lines, startLine) {
738return { start: startLine, end: startLine, type: 'statement' };
739}
740
741function highlightBlock(blockInfo, lineHeight) {
742const top = (blockInfo.start - 1) * lineHeight;
743const height = (blockInfo.end - blockInfo.start + 1) * lineHeight;
750}
751
752function showPopup(content, x, y) {
753explanationContent.innerHTML = content;
754popup.style.display = 'block';
757}
758
759function closePopup() {
760popup.style.display = 'none';
761}
8- **Interactive Hover**: Hover over code blocks to highlight the smallest logical containing block
9- **Click Explanations**: Click on highlighted blocks to see detailed explanations in a popup
10- **Extensible**: Override default functions with custom implementations via query parameters
1112## Usage
41This will fetch the source from `https://esm.town/v/nbbaier/sqliteExplorerApp@100-main/main.tsx` and display it with annotations.
4243### Custom Functions
4445You can override the default functionality by providing custom val URLs as query parameters:
4647#### Syntax Highlighter
79"start": 8,
80"end": 15,
81"type": "function|class|control|statement"
82}
83```
95"start": 8,
96"end": 15,
97"type": "function"
98}
99}
128The default block detector uses simple heuristics to identify:
1291301. **Function blocks**: Lines containing `function` or `=>`
1312. **Class blocks**: Lines containing `class`
1323. **Control blocks**: Lines containing `if`, `for`, or `while`
145## Architecture
146147The val is structured with three main pluggable functions:
148149- `defaultSyntaxHighlighter()`: Handles code syntax highlighting
151- `defaultBlockExplainer()`: Generates explanations for code blocks
152153Each function can be replaced with custom implementations via query parameters, making the system highly extensible.
154155## Error Handling
160- Client-side errors are logged to console
161162## Example Custom Functions
163164The project includes several example custom functions to demonstrate extensibility:
165166### AI-Powered Explainer (`/examples/ai-explainer.ts`)
174### Enhanced Block Detector (`/examples/enhanced-detector.ts`)
175Provides more sophisticated block detection with support for:
176- Functions, classes, interfaces
177- Objects and arrays
178- Control structures