Prism identifies tokens in your code, which are in turn styled by CSS to produce the syntax highlighting. This page provides an overview of the standard tokens and corresponding examples.
When defining a new language, you will need to provide token names for each 'type' of code, such as keywords and operators, so Prism's themes can assign colors (and other styles) accordingly. Prism's themes (both official and non-official) only guarantee coverage for these standard tokens, so it is recommended to make use of the following standard tokens to ensure that code will be highlighted.
General purpose | |
---|---|
keyword |
Pre-defined and reserved words.
|
builtin |
Functions/Methods/Classes/Types that are available out of the box.
|
class-name |
The name of a class, interface, trait, or type.
|
function |
The name of a function or method.
|
boolean |
True and false, and pairs with similar concepts (e.g. yes and no).
|
number |
A numerical value, regardless of base and order, and no matter real or imaginary.
|
string |
Literal text, including numbers and symbols and maybe even more special characters.
|
char |
A string that can comprise only a single character, enforced by the language.
|
symbol |
A primitive data type found in some languages, can be thought of as an identifier.
|
regex |
A regular expression.
|
url |
A link to another page or resource.
|
operator |
A symbol that represents an action or process, whether it's a mathematical operation, logical operation, and so on.
|
variable |
The name of a variable. This token is intended to be used sparingly. It's generally used on special variables (e.g. Less or Bash), not general variables from imperative and procedural programming languages (e.g. C, JavaScript, Python).
|
constant |
The name of a constant.
|
property |
An attribute/characteristic or object/map key.
|
punctuation |
Punctuation such as brackets, parentheses, commas, and more.
|
important |
Anything that is important and needs special highlighting.
|
comment |
Code comments.
|
Markup languages | |
tag |
A markup tag (e.g. HTML and XML tags).
|
attr-name , attr-value |
Kind of like a property of a markup tag and its value/argument respectively.
|
namespace |
Used to provide uniquely named elements and attributes in XML documents. Outside of markup languages, it is used to tokenize the package/namespace part of identifiers.
|
prolog |
The first part of an XML document.
|
doctype |
Document type declaration, specific to markup languages.
|
cdata |
Character data, specific to markup languages.
|
entity |
Code used to display reserved characters in markup languages.
|
Document-markup languages | |
bold |
Bolded text. Mostly found in document-markup languages.
|
italic |
Italicised text. Mostly found in document-markup languages.
|
Stylesheets | |
atrule |
Literally @ rules (statements) in stylesheets.
|
selector |
Code that identifies or picks something out of a group to operate on, such as the names of HTML elements in stylesheets.
|
Diff | |
inserted , deleted |
Added or modified line and deleted line respectively, mainly for diffs. In general, also the idea of something being increased and decreased/removed respectively.
|
In addition to the standard tokens above, Prism also has a token for languages that are embedded in another language, such as CSS in HTML, JS in HTML, Bash in Shell-session, and CSS in JS, allowing Prism's themes to highlight the tokens in the embedded languages more accurately. All embedded languages are wrapped in their own special token, which includes a CSS class language-xxxx
corresponding to the embedded language.
Open your browser's developer tools and check out the example below to see it in action!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>I can haz embedded CSS and JS</title>
<style>
@media print {
p { color: red !important; }
}
</style>
</head>
<body>
<h1>I can haz embedded CSS and JS</h1>
<script>
if (true) {
console.log('foo');
}
</script>
</body>
</html>
Sometimes, a language might use a particular name to refer to certain pieces of code, but which is not one of Prism's standard token names, such as function-defintion
. Since function-definition
is not a standard token, you might want to alias it with a standard token such as function
, which is semantically similar, and will ensure that Prism's themes will highlight it. Here's an example:
fn main() {
println!("Hello World");
another_function();
}