Skip to content

Conversation

Copy link

Copilot AI commented Jul 30, 2025

This PR implements syntax highlighting for code blocks using the syntect Rust library, addressing the feature request for enhanced code block rendering.

What's New

New Function: html_with_syntax_highlighting()

import pyromark

# Original functionality (unchanged)
html = pyromark.html('''
```python
def hello():
    print("Hello, world!")

''')

Output:
def hello():\n    print("Hello, world!")\n

New syntax highlighting functionality

html = pyromark.html_with_syntax_highlighting('''

def hello():
    print("Hello, world!")

''')

Output:

def hello...


### CLI Support

```bash
# Original behavior
pyromark input.md

# With syntax highlighting
pyromark --syntax-highlighting input.md

Key Features

  • Language Support: Supports all languages available in syntect (Python, JavaScript, Rust, C++, Java, Go, HTML, CSS, JSON, and many more)
  • Backward Compatible: Original html() function remains unchanged
  • Graceful Fallbacks: Unknown languages render as plain code blocks
  • Performance: Minimal overhead with Rust-powered processing
  • Theme: Uses the "base16-ocean.dark" theme for beautiful dark syntax highlighting

Implementation Details

  • Added syntect = "5.2" dependency for syntax highlighting
  • Implemented event stream processing to identify and highlight fenced code blocks
  • Preserves semantic HTML structure with both CSS classes and inline styles
  • Maintains all existing functionality and passes all original tests

Examples

The implementation handles multiple languages seamlessly:

# Python with proper keyword, function, and string highlighting
html = pyromark.html_with_syntax_highlighting('''
```python
class Calculator:
    def add(self, x, y):
        return x + y

''')

JavaScript with appropriate syntax coloring

html = pyromark.html_with_syntax_highlighting('''

function factorial(n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}

''')


All existing tests continue to pass, and comprehensive new tests verify the syntax highlighting functionality works correctly across various scenarios including edge cases, unknown languages, and integration with other Markdown features.

Fixes #343.

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey.

Copilot AI and others added 3 commits July 30, 2025 08:58
Co-authored-by: monosans <76561516+monosans@users.noreply.github.com>
Co-authored-by: monosans <76561516+monosans@users.noreply.github.com>
Co-authored-by: monosans <76561516+monosans@users.noreply.github.com>
Copilot AI changed the title [WIP] Syntax Highlighting Add syntax highlighting support for code blocks Jul 30, 2025
Copilot AI requested a review from monosans July 30, 2025 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants