import gradio as gr import json import os from pathlib import Path # Category color mapping CATEGORY_COLORS = { 'ai-dev': '#8b5cf6', 'ai-engineering': '#7c3aed', 'claude-code': '#2563eb', 'common-tasks': '#059669', 'conv-mgmt': '#0891b2', 'cybersec': '#dc2626', 'development': '#3b82f6', 'documentation': '#6366f1', 'educational': '#f59e0b', 'experiments': '#ec4899', 'filesystem-ops': '#84cc16', 'for-fun': '#f97316', 'general-purpose': '#64748b', 'ideation': '#a855f7', 'local-ai': '#6366f1', 'media': '#06b6d4', 'misc': '#9ca3af', 'operations': '#10b981', 'seo-web': '#14b8a6', 'sysadmin': '#22c55e', 'tech-docs': '#3b82f6', 'writing-and-editing': '#f472b6' } # Load slash commands data from categorized folders def load_commands(): """Load slash commands from categorized folder structure""" commands = [] commands_dir = Path("commands") if not commands_dir.exists(): return [] # Recursively find all .md files in commands directory for cmd_file in commands_dir.rglob("*.md"): # Get the top-level category (first subdirectory under commands/) parts = cmd_file.relative_to(commands_dir).parts category = parts[0] if parts else 'misc' commands.append({ 'name': cmd_file.stem, 'path': str(cmd_file), 'category': category }) # Sort commands alphabetically by name commands.sort(key=lambda x: x['name'].lower()) return commands def load_command_content(path): """Load the content of a specific command file""" file_path = Path(path) if file_path.exists(): with open(file_path, 'r') as f: content = f.read() # Remove YAML frontmatter if present if content.startswith('---'): parts = content.split('---', 2) if len(parts) >= 3: return parts[2].strip() return content return "Content not found" def search_commands(search_term, commands_data): """Filter commands based on search term""" if not search_term: return commands_data search_term = search_term.lower() filtered = [ cmd for cmd in commands_data if search_term in cmd['name'].lower() ] return filtered def create_command_card(command): """Create an HTML card for a command""" import html name = command['name'] path = command['path'] category = command.get('category', 'misc') content = load_command_content(path) # Extract description from content if available description = "" lines = content.split('\n') if lines: # Skip the first line and find the first non-empty line as description for line in lines[1:6]: # Start from second line, check up to 6 lines if line.strip() and not line.startswith('#'): description = line.strip() break # Get category color category_color = CATEGORY_COLORS.get(category, '#9ca3af') category_display = category.replace('-', ' ').title() # JSON encode the content for safe JavaScript usage content_json = json.dumps(content) # Generate unique ID unique_id = f"cmd-{name.replace(' ', '-').replace('/', '-')}" card_html = f""" """ return card_html def display_commands(search_term): """Main function to display filtered commands""" commands_data = load_commands() filtered_commands = search_commands(search_term, commands_data) if not filtered_commands: return "
No commands found matching your search.
" html_output = f"Showing {len(filtered_commands)} command(s)
" for command in filtered_commands: html_output += create_command_card(command) html_output += "