What Is Prism.js
Prism.js is a lightweight syntax highlighting library. When provided an HTML code block with a language class, it tokenizes the source, wrapping each token in a span with a semantic class that your CSS theme colors. It supports well over 200 languages through optional language components, so ship what you need.
How to Import
Install Prism.js via npm, then import the library and only the language components you need.
npm install prismjs
import Prism from 'prismjs'
import 'prismjs/components/prism-javascript'
import 'prismjs/components/prism-python'
// add more as needed
Prism has several themes you can import under prismjs/themes/, or write your
own token styles in CSS targeting ".token.*" classes, which is what I've done here stay compatible with light and dark themes on my site.
In a traditional static setup, Prism.js scans the DOM on page load and highlights any matching
code blocks. That only runs once, so if you inject, fetch, or generate new code
blocks afterwards, you need to trigger Prism again with Prism.highlightAll() (or
Prism.highlightElement() for a specific block). Also, each block still needs a valid
"language-*" class so Prism knows which syntax to apply. I break that down in the section below.
How It's Integrated Here
Articles on this site are static HTML files fetched and injected into the DOM at runtime. Once article content loads, a useEffect in ArticlePage.jsx runs a function that scans each code block and applies the appropriate syntax.
The following Prism components have been imported and are available in any article; Markup (HTML / XML), CSS, JavaScript & JSX, TypeScript & TSX, JSON, Bash / Shell, PowerShell, Python, SQL, and YAML.
To highlight a block in an article with the appropriate language syntax, give the inner code element inside
the <pre><code> a "language-*" class matching one of the above. For example:
<pre><code class="language-python">...</code></pre>
Which renders like this:
def greet(name):
print(f"Hello, {name}")
Custom Language Aliases and Auto-Detection
Distinct from Prism.js, I've implemented a normalization function that maps various "language-*" aliases to the correct Prism language. I also created a small auto-detection function that infers the language code content, and a toolbar above the codeblock displaying the detected language, and a copy button.
Some of the aliases are defined below:
| Alias | Resolves To |
|---|---|
| language-html | markup |
| language-sh | bash |
| language-ps1 | powershell |
| language-yml | yaml |
If a code block has no language class attached, a small heuristic function inferLanguageFromCode
scans the source and infers a best-guess language, catching plain HTML, PowerShell,
shell, and JSON objects automatically.
########## ATTEMPT TO INFER LANGUAGE IF NOT EXPLICITLY PROVIDED ##########
function inferLanguageFromCode(source) {
const text = (source || '').trim()
if (!text) return ''
if (/<([a-z][\w-]*)[\s>]/i.test(text)) return 'markup'
if (/(^|\n)\s*\$env:/.test(text) || /\bGet-[A-Z]/.test(text)) return 'powershell'
if (/(^|\n)\s*(npm|pnpm|yarn|git|cd|ls|cat)\b/.test(text)) return 'bash'
if (/^\s*[\[{][\s\S]*[\]}]\s*$/.test(text) && /"\s*:/.test(text)) return 'json'
return ''
}
Additionally, I've given each highlighted code block a toolbar above, with a language label and a copy button.
This is unique to my site and is handled in the enhanceCodeBlocks function in
ArticlePage.jsx. I think it's worth noting that Prism.js has a built-in toolbar and copy plugin. However, I wanted
more control over the styling and behavior, to match my site's aesthetic.