Giter Site home page Giter Site logo

jackpilowsky / sql-highlight Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scriptcoded/sql-highlight

0.0 0.0 0.0 116 KB

A simple and lightweight SQL syntax highlighting library written in pure JavaScript

License: MIT License

JavaScript 97.44% CSS 2.56%

sql-highlight's Introduction

SQL syntax highlight

A simple and lightweight SQL syntax highlighting library written in pure JavaScript

Tests Status Coverage Status 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. Oh, and there are no external dependencies 😉

Installation

Install via Yarn:

yarn add sql-highlight

Install via 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:

Screenshot

HTML mode:

import { highlight } from '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-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">'[email protected]'</span>

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.
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)
  clear: '\x1b[0m'      // Clear (inserted after each match)
}

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 ESLint.

Code style

We use ESLint 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.

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.

sql-highlight's People

Contributors

ggerhard avatar jackpilowsky avatar paperbag42 avatar scriptcoded avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.