In the world of programming, clear and visually appealing code representation is essential. That’s where Refractor steps in—a lightweight yet powerful tool for syntax highlighting. In this article, we’ll explore how to get started with Refractor, its installation, usage, and some troubleshooting tips to ensure a smooth experience.
What is Refractor?
Refractor serves as a wrapper for Prism, enabling you to output Abstract Syntax Trees (ASTs) instead of mere HTML strings. This package supports over 270 programming languages, providing a versatile tool for developers looking to enhance code readability in diverse environments.
When Should I Use This?
Consider using Refractor in scenarios where HTML serialization would be ineffective. For example:
- When displaying code in a command-line interface (CLI) with ANSI sequences.
- Within virtual DOM frameworks (like React or Preact) for efficient diffing.
- When working with ASTs and libraries such as rehype.
Playground
You can experiment with Refractor through the interactive demo on Replit.
Installation
Refractor is an ESM-only package. Below are the installation methods for different environments:
- For Node.js (version 14.14+, 16.0+):
npm install refractor - For Deno:
import refractor from "https://esm.sh/refractor@4" - For browsers:
<script type="module"> import refractor from "https://esm.sh/refractor@4"; </script>
How to Use Refractor
Using Refractor is straightforward. Here’s a simple example:
import refractor from 'refractor';
const tree = refractor.highlight('use strict;', 'js');
console.log(tree);
This will yield an AST representation of the highlighted code—a structured format that you could visualize as a tree with various nodes representing your code syntax.
API Overview
The following functions are available through Refractor:
- refractor.highlight(value, language): Highlights the provided code in the specified programming language.
- refractor.register(syntax): Register a custom language syntax.
- refractor.alias(name[, alias]): Create aliases for registered languages.
- refractor.registered(aliasOrlanguage): Check if a language or alias is registered.
- refractor.listLanguages(): List all registered languages and their aliases.
Example of Serializing HAST as HTML
Using HAST trees returned by Refractor, you can serialize to HTML with the following snippet:
import refractor from 'refractor';
import toHtml from 'hast-util-to-html';
const tree = refractor.highlight('use strict;', 'js');
console.log(toHtml(tree));
This will yield the HTML representation of the structure produced by the Refractor highlighting method.
Types and Data
This package is fully typed with TypeScript. You can include various syntaxes by importing them. Keep in mind, to maintain efficiency, you only need to import the ones you utilize.
CSS
Refractor does not inject CSS, as it may not render in a browser context. However, you can manually apply any Prism themes like this example:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/themes/prism-dark.min.css">
Troubleshooting
If you face any issues, consider the following:
- Ensure you are using a compatible Node.js version (14.14+ or 16.0+).
- Check if the language you are trying to highlight is registered.
- Verify that you are importing the correct syntax files as required.
- You may also refer to the Refractor GitHub repository for more examples and issues.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Contributions and Projects
Contributions to Refractor and its community are welcome! Check the repository for information on how to get involved. Additionally, keep an eye on projects powered by Refractor, like react-syntax-highlighter and @mapbox/rehype-prism.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

