npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

emphasize

v7.0.0

Published

ANSI syntax highlighting for the terminal

Downloads

369,070

Readme

emphasize

Build Coverage Downloads Size

ANSI syntax highlighting for the terminal.

Contents

What is this?

This package wraps lowlight to output ANSI syntax highlighting instead of HTML. It can support 190+ programming languages.

When should I use this?

This package is useful when you want to display code on a terminal.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install emphasize

In Deno with esm.sh:

import {all, common, createEmphasize} from 'https://esm.sh/emphasize@7'

In browsers with esm.sh:

<script type="module">
  import {all, common, createEmphasize} from 'https://esm.sh/emphasize@7?bundle'
</script>

Use

Say example.css looks as follows:

@font-face {
  font-family: Alpha;
  src: url('Bravo.otf');
}

body, .charlie, #delta {
  color: #bada55;
  background-color: rgba(33, 33, 33, 0.33);
  font-family: "Alpha", sans-serif;
}

@import url(echo.css);

@media print {
  a[href^=http]::after {
    content: attr(href)
  }
}

…and example.js contains the following:

import fs from 'node:fs/promises'
import {emphasize} from 'emphasize'

const doc = String(await fs.readFile('example.css'))

const output = emphasize.highlightAuto(doc).value

console.log(output)

…now running node example.js yields:

\x1B[32m@font-face\x1B[39m {
  \x1B[33mfont-family\x1B[39m: Alpha;
  \x1B[33msrc\x1B[39m: \x1B[31murl\x1B[39m(\x1B[36m'Bravo.otf'\x1B[39m);
}

\x1B[32mbody\x1B[39m, \x1B[34m.charlie\x1B[39m, \x1B[34m#delta\x1B[39m {
  \x1B[33mcolor\x1B[39m: \x1B[36m#bada55\x1B[39m;
  \x1B[33mbackground-color\x1B[39m: \x1B[31mrgba\x1B[39m(\x1B[36m33\x1B[39m, \x1B[36m33\x1B[39m, \x1B[36m33\x1B[39m, \x1B[36m0.33\x1B[39m);
  \x1B[33mfont-family\x1B[39m: \x1B[36m"Alpha"\x1B[39m, sans-serif;
}

\x1B[32m@import\x1B[39m url(echo.css);

\x1B[32m@media\x1B[39m print {
  \x1B[32ma\x1B[39m\x1B[35m[href^=http]\x1B[39m\x1B[35m::after\x1B[39m {
    \x1B[33mcontent\x1B[39m: \x1B[31mattr\x1B[39m(href)
  }
}

…which looks as follows:

Screenshot showing the code in terminal

API

This package exports the identifiers all, common, and createEmphasize. There is no default export.

It exports the TypeScript types AutoOptions, LanguageFn, Result, Sheet, and Style.

all

Map of all (±190) grammars (Record<string, LanguageFn>).

See all from lowlight.

common

Map of common (37) grammars (Record<string, LanguageFn>).

See common from lowlight.

createEmphasize(grammars?)

Create a emphasize instance.

Parameters
Returns

Emphasize (emphasize).

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

Highlight value (code) as language (name).

Parameters
  • language (string) — programming language name
  • value (string) — code to highlight
  • sheet (Sheet, optional) — style sheet
Returns

Result.

emphasize.highlightAuto(value[, options])

Highlight value (code) and guess its programming language.

Parameters
  • value (string) — code to highlight
  • options (AutoOptions or Sheet, optional) — configuration or style sheet
Returns

Result.

emphasize.listLanguages()

List registered languages.

See lowlight.listLanguages.

emphasize.register(grammars)

Register languages.

See lowlight.register.

emphasize.registerAlias(aliases)

Register aliases.

See lowlight.registerAlias.

emphasize.registered(aliasOrLanguage)

Check whether an alias or name is registered.

See lowlight.registered.

AutoOptions

Configuration for highlightAuto (TypeScript type).

Fields
  • sheet (Sheet, optional) — sheet
  • subset (Array<string>, default: all registered languages) — list of allowed languages

LanguageFn

Highlight.js grammar (TypeScript type).

See LanguageFn from lowlight.

Result

Result (TypeScript type).

Fields
  • language (string or undefined) — detected programming language.
  • relevance (number or undefined) — how sure lowlight is that the given code is in the language
  • value (string) — highlighted code

Sheet

Map highlight.js classes to styles functions (TypeScript type).

The hljs- prefix must not be used in those classes. The “descendant selector” (a space) is supported.

For convenience chalk’s chaining of styles is suggested. An abbreviated example is as follows:

{
  'comment': chalk.gray,
  'meta meta-string': chalk.cyan,
  'meta keyword': chalk.magenta,
  'emphasis': chalk.italic,
  'strong': chalk.bold,
  'formula': chalk.inverse
}
Type
type Sheet = Record<string, Style>

Style

Color something (TypeScript type).

Parameters
  • value (string) — input
Returns

Output (string).

Compatibility

This projects is compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, emphasize@7, compatible with Node.js 16.

Security

This package is safe.

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer