Pygments rather than highlight.js for syntax highlighting

AI-generated summary

In a discussion about improving syntax highlighting in Discourse, dbitouze proposes replacing highlight.js with Pygments, citing its richer, more visually appealing output for languages like Python and LaTeX — demonstrated via side-by-side comparisons and screenshots. However, NateDhaliwal points out that Pygments is a Python library, not JavaScript, making integration into Discourse challenging. dbitouze acknowledges this limitation, noting Pygments is better suited for static sites. Moin suggests an alternative: switching to a different theme within highlight.js via the CodeBlock Theme Picker, which may improve aesthetics. Still, dbitouze argues that even with better themes, highlight.js’s lexers remain too minimalist compared to Pygments’ depth and language support. The thread highlights a tension between visual quality and technical feasibility in syntax highlighting for dynamic platforms like Discourse.

For syntax highlighting, Discourse relies on highlight.js but Pygments looks richer. For instance, here is how looks a Python code:

text = input('Type a number, and its factorial will be printed: ')
n = int(text)

if n < 0:
    raise ValueError('You must enter a non-negative integer')

factorial = 1
for i in range(2, n + 1):
    factorial *= i

print(factorial)

to be compared with how it could look with Pygments:

This is more prominent for a LaTeX code block (unfortunately, this language is not enabled on the present forum) but here is how it looks on our Discourse forum:

to be compared with how it could look with Pygments:

or even better with a customized theme/lexer:

Would it be possible to rely on Pygments rather than on highlight.js?

1 Like

The problem is, it’s a Python package and not a Javascript one :eyes: . I wonder if it can be converted over :thinking: .

3 Likes

Ah, indeed, Pygments looks to be suitable for static html sites, not for applications such as Discourse.

Maybe a different theme from highlight.js would improve the colors too?

You can try them at Demo - highlight.js and there is a component to change it
Codeblock Theme Picker

2 Likes

Indeed, another theme could provide nicer colors but the problem remains: the highlight.js lexer is minimalist.