scriptcoded/sql-highlight
scriptcoded/sql-highlight has 42 GitHub stars and 14 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-10 00:04 UTC. Last push 2026-11-02.
A simple and lightweight SQL syntax highlighting library written in pure JavaScript
Measured star growth
- in 24h
- 0
- in 7 days
- Update delayed
- in 15 days
- Update delayed
- in 1 month
- Update delayed
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Originalsql-highlight
A simple and lightweight library for highlighting SQL queries written in pure JavaScript
What's it all about?
sql-highlight is a small package that highlights SQL queries. It can output to both the terminal with Unicode escape sequences, as well as to normal HTML. Oh, and there are no external dependencies 😉
Installation
sql-highlight is tested to work with Node.js 16, 18, 20, 22 and 24.
Install with Yarn:
yarn add sql-highlight
Install with NPM:
npm install sql-highlight
Usage
In its most basic form
const { highlight } = require('sql-highlight')
const sqlString = "SELECT `id`, `username` FROM `users` WHERE `email` = '[email protected]'"
const highlighted = highlight(sqlString)
console.log(highlighted)
Output:

HTML mode
const { highlight } = require('sql-highlight')
const sqlString = "SELECT `id`, `username` FROM `users` WHERE `email` = '[email protected]'"
const highlighted = highlight(sqlString, {
html: true
})
document.body.innerHTML += highlighted
Output:
<span class="sql-hl-keyword">SELECT</span>
<span class="sql-hl-identifier">`id`</span>
<span class="sql-hl-special">,</span>
<span class="sql-hl-identifier">`username`</span>
<span class="sql-hl-keyword">FROM</span>
<span class="sql-hl-identifier">`users`</span>
<span class="sql-hl-keyword">WHERE</span>
<span class="sql-hl-identifier">`email`</span>
<span class="sql-hl-special">=</span>
<span class="sql-hl-string">'[email protected]'</span>
Sample styles to get you going can be found in
sample-style.css. To use them, append the output to a
code tag with the class name sql, like this:
<code class="sql">
<span class="sql-hl-keyword">SELECT</span>
<span class="sql-hl-identifier">`id`</span>
<!-- etc... -->
</code>
Options
The following options may be passed to the highlight function.
| Option | Value | Default | Description |
|---|---|---|---|
| html | boolean |
false |
Set to true to render HTML instead of Unicode. |
| htmlEscaper | (str: string) => string |
Basic escaper | Function to escape HTML entities. Uses a basic escaper by default. If HTML mode is used in a browser environment this could be useful to escape strings using the DOM. |
| classPrefix | string |
'sql-hl-' |
Prefix to prepend to classes for HTML span-tags. Is appended with entity name. |
| colors | Object |
See below* | What color codes to use for Unicode rendering. A list of basic color codes can be found here. |
* colors option default value
{
keyword: '\x1b[35m', // SQL reserved keywords
function: '\x1b[31m', // Functions
number: '\x1b[32m', // Numbers
string: '\x1b[32m', // Strings
special: '\x1b[33m', // Special characters
bracket: '\x1b[33m', // Brackets (parentheses)
comment: '\x1b[2m\x1b[90m', // Comments
clear: '\x1b[0m' // Clear (inserted after each match)
}
Custom highlighting
In case you want to do the highlighting yourself you can use getSegments to only let sql-highlight parse the SQL string for you. You can then use the segments to highlight it yourself.
const { getSegments } = require('sql-highlight')
const sqlString = "SELECT `id`, `username` FROM `users` WHERE `email` = '[email protected]'"
const segments = getSegments(sqlString)
console.log(segments)
Output:
[
{ name: 'keyword', content: 'SELECT' },
{ name: 'whitespace', content: ' ' },
{ name: 'identifier', content: '`id`' },
{ name: 'special', content: ',' },
{ name: 'whitespace', content: ' ' },
{ name: 'identifier', content: '`username`' },
{ name: 'whitespace', content: ' ' },
{ name: 'keyword', content: 'FROM' },
{ name: 'whitespace', content: ' ' },
{ name: 'identifier', content: '`users`' },
{ name: 'whitespace', content: ' ' },
{ name: 'keyword', content: 'WHERE' },
{ name: 'whitespace', content: ' ' },
{ name: 'identifier', content: '`email`' },
{ name: 'whitespace', content: ' ' },
{ name: 'special', content: '=' },
{ name: 'whitespace', content: ' ' },
{ name: 'string', content: "'[email protected]'" }
]
Contributing
See the contribution guidelines.
Tests
We use Jest for running our tests. The test suite can be run by running npm run test. This will run both Jest and Biome.
Code style
We use Biome for making sure that our code remains pretty and consistent throughout the project. If your editor doesn't automatically pick up our config you can lint the code using npm run lint.
A note on Dependabot
Dependabot Auto Merge is installed in this repository to automatically merge dependabot PRs for minor version updates. Only PRs that pass the tests get merged. No new releases will be created for dependency updates as there are no production dependencies and a release would therefore be completely unnecessary.
Additional information
Malcolm Nihlén - [email protected]
Distributed under the MIT licence. See LICENCE for more information.
https://github.com/scriptcoded
Disclaimer
This was initially a fork from https://github.com/pomahtuk/sequilize-highlight. The repo wasn't being updated, NPM wasn't serving the latest version and there was a severe memory leak. Though the latest version now exists on NPM, issues still persist. This repo serves to address those problems, as well as providing a cleaner interface that's not bound to Sequelize.
With version 3.0.0 the library was almost completely rewritten, which leaves very little similarity with the original repo.
Explore related projects
Asabeneh/30-Days-Of-Python
The 30 Days of Python programming challenge is a step-by-step guide to learn the Python programming language in 30 days. This challenge may take more than 100 days. Follow your own pace. These videos may help too: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,486 starsgrafana/grafana
The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
77,790 starsredis/redis
For developers, who are building real-time data-driven applications, Redis is the preferred, fastest, and most feature-rich cache, data structure server, and document and vector query engine.
77,332 starsapache/superset
Apache Superset is a Data Visualization and Data Exploration Platform
75,455 stars