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

sunlight-x

v0.0.4

Published

Sunlight highlighter refactored for Node.js

Downloads

118

Readme

sunlight-x: Intelligent Syntax Highlighting, Modernized

Build Status Build status

Codacy Badge Codacy Badge

Sunlight highlighter modernized for node.js. Here is the demo of sunlight-x.

Features

  • Relatively accurate highlighting for some languages. This is mainly done with a context-aware highlighting facility (at the expense of more complex language modules). In particular, "named identifiers" are supported in addition to normal identifiers.
  • Use multiple themes at the same time (TODO, but almost finished).
  • Line numbering.
  • Line highlighting.

Usage

Installation

Type yarn add sunlight-x or npm install --save sunlight-x in the console.

Using sunlight-x with default settings

var sunlight = require("sunlight-x");
var highlighter = new sunlight.Highlighter();

var code = "console.log('Hello world!');";
var language = "javascript";
highlighter.highlightCode(code, language);

Passing options to the highlighter

Options available to the highlighter is shown below with their defaults. You can pass any or all of these options to new sunlight.Highlighter().

var sunlight = require("sunlight-x");

var options = {
  // Main highlighter
  theme: "gitbook", // The theme.
  tabWidth: 4, // Width of a tab character.
  classPrefix: "sunlight-", // Style prefix. Custom stylesheet needed.
  showWhitespace: false, // Whether to show invisible tabs and whitespaces.
  maxHeight: false, // Max height of code container. False = no limit.

  // Line number plugin (loaded automatically)
  lineNumbers: false, // Whether to show line numbers.
  lineNumberStart: 1, // The starting number line if line numbers are shown.
  lineHighlight: [] // List of line numbers to be highlighted.
};

var highlighter = new sunlight.Highlighter(options);

var code = "console.log('Hello world!');";
var language = "javascript";
highlighter.highlightCode(code, language);

Caution: Do NOT modify the content of Highlighter.options. Highlighter.options be made immutable in the future. To change the options, make a different instance of Highlighter instead.

API

Highlighter class

var sunlight = require("sunlight-x");
var highlighter = new sunlight.Highlighter();

/**
 * Highlights the given code and returns the HTML as a string.
 * @param {string} code
 * @param {string} languageId
 * @returns {Element}
 */
var html = highlighter.highlightCode(code, languageId);

/**
 * Highlights the given code and returns the HTML Element.
 * @param {string} code
 * @param {string} languageId
 * @returns {Element}
 */
var element = highlighter.highlightCodeAsElement(code, languageId);

Extending sunlight-x

Now advanced usages of the highlighter is shown below, which is used to add an language to the highlighter. Read the source or the old sunlight doc for more details.

var sunlight = require("sunlight-x");

// Add a language to the highlighter. This is GLOBAL.
var language = ... // the language definition.
sunlight.registerLanguage(language);

// See if the language is registered. This is GLOBAL.
var isRegistered = sunlight.isRegistered(language)

Difference from original Sunlight

  • Flow annotation.
  • Continuous integration. Old Sunlight has tests, but they are invoked manually.
  • API is being refactored. The new API is inclined to usage in node.js.
  • Code are put in modules and classes. Some bugs are fixed.
  • Modified CSS (themes not supported in HTML output yet).

How to contribute

  • Start an issue, and write down your ideas there.
  • Give ideas on the API.
  • Add tests, contribute test snippets.
  • Add an language. Remember to add tests with it.
  • Points out where an existing language does not work well. (Start an issue for this.)

TODO

  • Add doucmentation.
  • Add the "doclinks" plugin.
  • Reorganize API:
    • Add API for getting the path and/or content of CSS files (and LESS files?).
  • Add more tests.
  • Revamp switchTo/switchBack mechanism. Currently it is broken when multiple languages are loaded.
  • Webpack for browsers.
  • Clarify owners of code snippets (some unknown code are used now).
    • Others are listed in the code snippets folder.
  • Refactors and bug fixes.
  • Add support of template interpolations in Javascript, PHP and other languages.
  • Modernize the languages being highlighted.
  • Rewrite number parsers.

Copyright

Copyright 2017-18 Leung Wing-chung. All rights reserved. Use of the software by a Apache License Version 2.0, that can be found in the LICENSE file.

Credits

This software is forked from Sunlight, authored by Tommy Montgomery. The original Sunlight is licensed by the WTFPL (warning: foul language inside).