JSPM

source-fragment

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q23738F
  • License MIT

Fetch source file fragment with highlighting

Package Exports

  • source-fragment

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (source-fragment) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.

License

MIT