[Tex/LaTex] Add keyword to language in minted

highlightinglistingsmintedpygmentssyntax highlighting

I use minted to highlight some javascript code.

I use

\documentclass{article}

\usepackage{minted}

\begin{document}

\begin{minted}{js}
const mySchema = new Schema({
  name: String,
  is_true: Boolean,
  age: Number,
  object_ids: [ObjectId],
});
\end{minted}

\end{document}

to produce

enter image description here

The problem is that it highlights String, Boolean and Number, but not ObjectId.

How can I add ObjectId as a keyword to javascript in minted?

Best Answer

Regarding minted, the answer is: You have to write an extension to the JavaScript lexer of pygmentize or modify a copy of the lexer. See the answers to the question How to add custom C++ keywords to be recognized by Minted? for details.

If you are not bound to use minted, use the package listings. Here is an example for defining JavaScript highlighting (adapted from this answer to the question language option supported in listings. You can easily define your own classes of keywords and their formatting.

enter image description here

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{js}{
  keywords={const, typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{blue}\bfseries,
  keywords=[2]{boolean, string, number, objectid},
  keywordstyle=[2]\color{green}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

\lstset{
   language=js,
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   tabsize=2,
   breaklines=true,
   showtabs=false
}

\begin{document}

\begin{lstlisting}
const mySchema = new Schema({
  name: String,
  is_true: Boolean,
  age: Number,
  object_ids: [ObjectId],
});
\end{lstlisting}

\end{document}