Code Highlighting
script-free code syntax highlighting with theme supportA powerful syntax highlighting system that transforms code snippets into beautifully formatted, colorized HTML on the server. Built on the robust Chroma library, this component supports hundreds of programming languages and produces clean, accessible markup perfect for documentation, tutorials, and code examples.
Key Features
- No JavaScript Required: Full functionality using pure CSS and HTML
- Extensive Language Support: Hundreds of programming languages and file formats
- Multiple Integration Methods: Markdown, Go templates, and direct API calls
- Clean HTML Output: Semantic markup optimized for accessibility and styling
- Line Numbers: Optional line numbering for code references
- Theme Support: Integrates with Project Forge’s theming system
- Performance Optimized: Fast server-side rendering with minimal overhead
Go Template Integration
Import the components package and use the provided template functions:
1 | |
3. Go Code Integration
Use the utility functions directly in your Go code:
1 | |
API Reference
Go Functions
FormatLang
func FormatLang(content, language string) (string, error)
Formats code content with the specified language syntax highlighting.
Parameters:
- content (string): The source code to format
- language (string): Language identifier (e.g., “go”, “javascript”, “python”)
Returns:
- string: HTML-formatted code with syntax highlighting
- error: Error if formatting fails
Example:
html, err := cutil.FormatLang(`print("Hello World")`, "python")
FormatLangIgnoreErrors
func FormatLangIgnoreErrors(content, language string) string
Same as FormatLang but returns empty string on errors instead of failing.
Example:
html := cutil.FormatLangIgnoreErrors(userCode, "go")
FormatFilename
func FormatFilename(content, filename string) (string, error)
Automatically detects language from file extension and formats accordingly.
Parameters:
- content (string): The source code to format
- filename (string): Filename with extension for language detection
Example:
1 | |
FormatJSON
func FormatJSON(obj interface{}) (string, error)
Formats any Go object as syntax-highlighted JSON.
Example:
1 | |
Template Functions
JSON Component
{%= components.JSON(object) %}
Displays an object as formatted JSON in an HTML table with line numbers.
Example:
1 | |
JSONModal Component
{%= components.JSONModal(id, title, object, pageState) %}
Creates a modal dialog containing formatted JSON data.
Parameters:
- id (string): Unique identifier for the modal
- title (string): Modal window title
- object (interface{}): Object to display as JSON
- pageState (*cutil.PageState): Current page state
Example:
1 | |
Supported Languages
The component supports hundreds of languages through Chroma. Here are some commonly used ones: