source-fragment v1.1.0
source-fragment
Fetch source file fragment with highlighting
Install
npm install source-fragmentExample
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:

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:

API
sourceFragment(loc[, options])Options:
formatType:
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 HTMLtty– formats result using terminal escape codes, useful to output in console with color support
linesBeforeType:
Number
Default:0Lines added before a range start line.
linesAfterType:
Number
Default:0Lines added after a range end line.
maxLinesType:
Number
Default:InfinityLimits lines number in result.
collapseOffsetType:
Boolean
Default:falseCut off common offset in resulting fragment, i.e. move source to the left as much as possible.
tabSizeType:
Number
Default:8Number of spaces that used as tab replacement, i.e. tabs in source replaces for 8 spaces by default.
License
MIT