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

source-fragment

v1.1.0

Published

Fetch source file fragment with highlighting

Downloads

32

Readme

source-fragment

NPM version Build Status

Fetch source file fragment with highlighting

Install

npm install source-fragment

Example

Suppose we have the code (example.js):

function hello() {
    return (
      'world' +
      '!!!'
    );
}

To get colored fragment of this code:

var sf = require('source-fragment');

console.log(sf('example.js:2:12:5:6', { format: 'tty' }));

You'll see in console:

Example output (tty)

The same but in HTML format:

sf('example.js:2:12:5:6', { format: 'html' });
// <div class="j5as83pdmd85mv2c-source">
// <style>...</style>
// <div class="j5as83pdmd85mv2c-line"><span class="j5as83pdmd85mv2c-num">  2</span>...</div>
// ...
// </div>

When pasted on HTML page looks like:

Example output (html)

API

sourceFragment(loc[, options])

Options:

  • format

    Type: Object
    Default: null (no format is used)

    Defines formatter for a result. There are two predefined formats, that can be choosen by name (e.g. sourceFragment('...', { format: 'html' })):

    • html - formats result in HTML
    • tty – formats result using terminal escape codes, useful to output in console with color support
  • linesBefore

    Type: Number
    Default: 0

    Lines added before a range start line.

  • linesAfter

    Type: Number
    Default: 0

    Lines added after a range end line.

  • maxLines

    Type: Number
    Default: Infinity

    Limits lines number in result.

  • collapseOffset

    Type: Boolean
    Default: false

    Cut off common offset in resulting fragment, i.e. move source to the left as much as possible.

  • tabSize

    Type: Number
    Default: 8

    Number of spaces that used as tab replacement, i.e. tabs in source replaces for 8 spaces by default.

License

MIT