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

macrolight

v1.5.0

Published

Simple, minimal syntax highlighter.

Downloads

64

Readme

macrolight

syntax highlighting, some assembly required

A modernized, more maintainable, and configurable version of Dmitry Prokashev's microlight syntax highlighter.

Usage

Installation

$ npm i macrolight

or in a browser / Deno:

import { highlight, highlightAll } from "https://esm.sh/macrolight";

API

There are two methods:

highlight

highlight (src: Element | string, config: Record<string, string> = {}): string

highlight takes in either an element or a plain string, and returns its contents syntax highlighted.

The config object can be used to change its behaviour.

config is an object that can have the following properties:

  • keywords: a list of words to highlight as keywords.
  • styles: an object with any of the following properties:
    • unformatted - a string that will be used as the style attribute for unformatted text.
    • keyword - a string that will be used as the style attribute for keywords.
    • punctuation - a string that will be used as the style attribute for punctuation characters.
    • string - a string that will be used as the style attribute for strings and regexes.
    • comment - a string that will be used as the style attribute for comments.
  • dontEscape - a boolean variable that, when set, disables the escaping of quotes, ampersands, apostrophes, and greater-than/less-than signs in generated HTML.

For example:

highlight(document.querySelector("#highlighted"), {
  keywords: ["const", "let", "import", "from", "in"],
  styles: {
    "comment": "font-style: italic; color: gray",
    "punctuation": "color: orange",
  },
});
highlight('const a = "<test>contents</test>";', {
  keywords: ["const", "test"],
  styles: {
    "comment": "font-style: italic; color: gray",
    "punctuation": "color: orange",
  },
});

Note that macrolight does NOT highlight any keywords by default.

highlightAll

highlightAll (config: Record<string, string> = {}, selector: string = '.macrolight'): void

highlightAll highlights all elements that match a certain selector (.macrolight by default). The config object is the same as the one described above for highlight.

HL_KEYWORDS

A small set of keywords is also exported by macrolight. You can use these by importing them the usual way:

import { HL_KEYWORDS } from "https://esm.sh/macrolight";

and then use them like so:

let lang = "py";
highlightAll({
  keywords: HL_KEYWORDS[lang],
});

To combine all the keywords into one long list, you can write a simple expression like:

const COMBINED_KEYWORDS = Object.values(HL_KEYWORDS).reduce(
  (acc, curr) => [...acc, curr],
  [],
);
highlightAll({
  keywords: COMBINED_KEYWORDS,
});

If you would like to use this list with TypeScript, you may encounter an issue where the list is treated implictly as any. To work around this, you can do something like:

// in a file by itself
import { HL_KEYWORDS as _HL_KEYWORDS } from "https://esm.sh/macrolight";
export const HL_KEYWORDS: Record<string, string[]> = _HL_KEYWORDS;

Finally, if there's a language you'd like to add keywords / aliases for, feel free to do so by editing hl-keywords.js and sending a pull request.

Misc notes

macrolight also adds the classes

  • macrolight-unformatted
  • macrolight-keyword
  • macrolight-punctuation
  • macrolight-string
  • macrolight-comment

on generated spans so you can style them with CSS.

LICENSE

macrolight is free, open-source software under the MIT License. Copyright © 2021 Siddharth Singh.

macrolight is a fork of Dmitry Prokashev's microlight. microlight is free, open-source software under the MIT License. Copyright © 2016 asvd.