Socket
Socket
Sign inDemoInstall

lowlight

Package Overview
Dependencies
1
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

lowlight


Version published
Weekly downloads
1.9M
decreased by-20.89%
Maintainers
1
Created
Weekly downloads
 

Package description

What is lowlight?

The lowlight npm package is a library for syntax highlighting of code using languages supported by highlight.js. It is primarily used to parse and transform code into a syntax-highlighted format without a browser, making it suitable for server-side applications or pre-processing of static pages.

What are lowlight's main functionalities?

Syntax Highlighting

This feature allows you to highlight syntax of various programming languages. The code sample demonstrates highlighting a simple JavaScript code snippet.

const lowlight = require('lowlight');
const highlightedCode = lowlight.highlight('javascript', 'const x = 123;').value;
console.log(highlightedCode);

Registering Languages

This feature enables the addition of new languages to lowlight's highlighting capabilities by registering them through highlight.js. The code sample shows how to register and highlight Python code.

const lowlight = require('lowlight');
lowlight.registerLanguage('python', require('highlight.js/lib/languages/python'));
const highlightedPython = lowlight.highlight('python', 'def foo(): return 123').value;
console.log(highlightedPython);

Auto-Detection of Language

This feature automatically detects the programming language of a given code snippet and applies appropriate syntax highlighting. The code sample demonstrates auto-detection for a JavaScript snippet.

const lowlight = require('lowlight');
const result = lowlight.highlightAuto('const x = 123;');
console.log(result.language); // 'javascript'
console.log(result.value);

Other packages similar to lowlight

Readme

Source

lowlight Build Status Coverage Status

Virtual syntax highlighting for virtual DOMs and non-HTML things, with language auto-detection. Perfect for React, VDOM, and others.

Lowlight is built to work with all syntaxes supported by highlight.js, that’s 155 languages (and all 73 themes).

Installation

npm:

npm install lowlight

lowlight is also available for duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed. Read more about usage in the browser.

Usage

Dependencies:


var hast = require('hast');
var low = require('lowlight');

Compile:


var ast = low.highlight('js', '"use strict";').value;
var html = hast.stringify({'type': 'root', 'children': ast});

Yields:

[
  {
    "type": "element",
    "tagName": "span",
    "properties": {
      "className": [
        "hljs-meta"
      ]
    },
    "children": [
      {
        "type": "text",
        "value": "\"use strict\""
      }
    ]
  },
  {
    "type": "text",
    "value": ";"
  }
]

Or, stringified with hast:

<span class="hljs-meta">&quot;use strict&quot;</span>;

API

low.highlight(language, value[, options])

Parse value according to the language grammar.

Parameters

  • name (string) — See list of names and aliases;

  • value (string) — Source to highlight;

  • options (Object?, optional):

    • prefix (string?, default: 'hljs-') — Class prefix.

Returns: Object:

  • relevance (number) — Integer representing how sure low is the given code is in the given language;

  • language (string) — The given language;

  • value (Array.<Node>) — Hast nodes representing the highlighted given code.

low.highlightAuto(value[, options])

Parse value by guessing its grammar.

Parameters

  • value (string) — Source to highlight;

  • options (Object?, optional):

    • prefix (string?, default: 'hljs-') — Class prefix;

    • subset (Array.<string>?, optional, defaults to all registered languages.) — List of allowed languages.

Returns: Object:

  • relevance (number) — Integer representing how sure low is the given code is in the detected language;

  • language (string) — The given language;

  • value (Array.<Node>) — Hast nodes representing the highlighted given code.

  • secondBest (Object?) — Object with the same structure as the top returned object, but containing information for the second-best result. Can be null.

low.registerLanguage(name, syntax)

Register a syntax. Useful in the browser or with custom grammars.

Parameters

  • name (string) — Name of language;

  • syntax (Function) — Syntax highlighter, see highlight.jss docs for more information.

Lowlight in the browser

I do not suggest using the pre-built files or requiring lowlight in the browser as that would include a 530kb (170kb with GZIP) file.

Instead, require lowlight/lib/core, and include only the used highlighters. For example:

var low = require('lowlight/lib/core');
var js = require('highlight.js/lib/languages/javascript');

low.registerLanguage('javascript', js);

low.highlight('js', '"use strict";');
// See `Usage` for the results.

...When using browserify, minifying, and GZIP that results in just 17kb of code (7kb with GZIP).

License

MIT © Titus Wormer

Keywords

FAQs

Last updated on 19 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc