Golang library for highlighting code syntax of programming languages inside HTML documents. Good documentation is included.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.7 KiB

Go report Go Reference Release License

highlight is a golang library for highlighting code syntax of different programming and markup languages inside HTML documents.


Supported Go versions:

  • 1.11
  • 1.17
  • 1.18

Add to go.mod file:

require github.com/lcomrade/highlight v1

How it works?

The <span> tag is used to highlight syntax when converting to HTML. This tag does not do anything on its own, but you can assign different properties to it in CSS. Including color and font.

All extraneous HTML tags will be protected.

Before highlighting:

User-agent: * # comment
Disallow: /faq

# comment 1
# comment 2

Allow: /
Crawl-delay: 10
Sitemap: https://example.org/sitemap.xml
Host: https://mirror.example.org

After highlighting:

<span class='code-keyword'>User-agent:</span> * <span class='code-c'># comment</span>
<span class='code-keyword'>Disallow:</span> /faq

<span class='code-comment'># comment 1</span>
<span class='code-comment'># comment 2</span>

<span class='code-keyword'>Allow:</span> /
<span class='code-keyword'>Crawl-delay:</span> 10
<span class='code-keyword'>Sitemap:</span> https://example.org/sitemap.xml
<span class='code-keyword'>Host:</span> https://mirror.example.org

Supported languages

  • C
  • Dockerfile
  • Golang
  • go.mod
  • INI
  • Java
  • JSON
  • Python
  • robots.txt
  • SQL

Code example

package main

import (

func main() {
	// Code written in the C language
	myCode := `
#include <stdio.h>

// Comment

int main() {
	printf("Hello, world!");

	return 0;

	// Highlight

Read more in the documentation

CSS classes

Example of a CSS file:

.code-operator, .code-keyword, .code-build-in-func, .code-key {
	color: Firebrick;

.code-var-type, .code-build-in-var {
	color: RoyalBlue;

.code-brackets {
	color: ForestGreen;

.code-comment {
	color: DimGray;