just/highlight

Types

A highlighting token, containing information about the kind of syntax being used. Many similar tokens (e.g. all keywords) are grouped together to simplify them.

For syntax tokens, see just/token.{type Token}.

pub type Token {
  Whitespace(String)
  Keyword(String)
  Variable(String)
  Class(String)
  String(String)
  Regexp(String)
  Number(String)
  Function(String)
  Operator(String)
  Comment(String)
  Punctuation(String)
  Other(String)
}

Constructors

  • Whitespace(String)
  • Keyword(String)
  • Variable(String)
  • Class(String)
  • String(String)
  • Regexp(String)
  • Number(String)
  • Function(String)
  • Operator(String)
  • Comment(String)
  • Punctuation(String)
  • Other(String)

Functions

pub fn ansi(code: String) -> String

Convert a string of JavaScript source code into ansi highlighting.

Colours taken from contour:

TokenColour
KeywordYellow
ClassCyan
FunctionBlue
OperatorMagenta
CommentItalic grey
String, Number, RegexpGreen
Whitespace, VariableNo colour

If you wish to use other colours or another format, use to_tokens.

pub fn html(code: String) -> String

Convert a string of JavaScript source code into an HTML string. Each token is wrapped in a <span> with a class indicating the type of token.

Class names taken from contour:

TokenCSS class
Keywordhl-keyword
Variablehl-variable
Classhl-class
Functionhl-function
Operatorhl-operator
Punctuationhl-punctuation
Commenthl-comment
Stringhl-string
Regexphl-regexp
Numberhl-number
Whitespaceno class

Place the output within a <pre><code>...</code></pre> and add styling for these CSS classes to get highlighting on your website. Here’s some CSS you could use:

pre code .hl-comment  { color: #d4d4d4; font-style: italic }
pre code .hl-function { color: #9ce7ff }
pre code .hl-keyword  { color: #ffd596 }
pre code .hl-operator { color: #ffaff3 }
pre code .hl-string   { color: #c8ffa7 }
pre code .hl-number   { color: #c8ffa7 }
pre code .hl-regexp   { color: #c8ffa7 }
pre code .hl-class    { color: #ffddfa }

If you wish to use another format see to_ansi or to_tokens.

pub fn tokens(code: String) -> List(Token)

Convert a string of JavaScript source code into highlighting tokens. Highlighting tokens only contain information about the kind of syntax being used, grouping similar tokens (e.g. all keywords) into one category.

To convert code into syntax tokens, see just.tokenise.

Search Document