JSPM

  • Created
  • Published
  • Downloads 1050993
  • Score
    100M100P100Q184813F
  • License MIT

A simple and lightweight syntax highlighting library for SQL

Package Exports

  • sql-highlight

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sql-highlight) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

SQL syntax highlight

A simple and lightweight syntax highlighting library for SQL

NPM Version Downloads Stats

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

Installation

Install via Yarn (recommended):

yarn add sql-highlight -D

Install via NPM:

npm install sql-highlight --save-dev

Usage

Note that we're using ES6 import statements here. Usage with require works just as well.

In its most basic form:

import Highlight from 'sql-highlight'
const highlighter = new Highlight()

console.log(highlighter.highlight("SELECT `id`, `username` FROM `users` WHERE `email` = 'test@example.com'"))

Output:

Screenshot 1

HTML mode:

import Highlight from 'sql-highlight'
const highlighter = new Highlight({
  html: true
})

document.body.innerHTML += highlighter.highlight("SELECT `id`, `username` FROM `users` WHERE `email` = 'test@example.com'"))

Output:

<span class="sql-hl-keyword">SELECT</span>
<span class="sql-hl-string">`id`</span>
<span class="sql-hl-special">,</span>
<span class="sql-hl-string">`username`</span>
<span class="sql-hl-keyword">FROM</span>
<span class="sql-hl-string">`users`</span>
<span class="sql-hl-keyword">WHERE</span>
<span class="sql-hl-string">`email`</span>
<span class="sql-hl-special">=</span>
<span class="sql-hl-string">'test@example.com'</span>

Options

Options may be passed to the constructor while instantiating the Highlighter class.

Option Value Default Description
html boolean true Set to true to render HTML instead of Unicode.
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.

* 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)
}

Contributing

  1. Fork it (https://github.com/scriptcoded/sql-highlight/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Additional information

Malcolm Nihlén - malcolm.nihlen@gmail.com

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.