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
:
Token | Colour |
---|---|
Keyword | Yellow |
Class | Cyan |
Function | Blue |
Operator | Magenta |
Comment | Italic grey |
String, Number, Regexp | Green |
Whitespace, Variable | No 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
:
Token | CSS class |
---|---|
Keyword | hl-keyword |
Variable | hl-variable |
Class | hl-class |
Function | hl-function |
Operator | hl-operator |
Punctuation | hl-punctuation |
Comment | hl-comment |
String | hl-string |
Regexp | hl-regexp |
Number | hl-number |
Whitespace | no 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
.