babel-code-frame: add options for linesBefore, linesAfter (#4561)

* babel-code-frame: add options for linesBefore, linesAfter

* add example, use list of keywords

* a [skip ci]

* Update index.js
This commit is contained in:
Henry Zhu
2016-09-26 16:12:35 -04:00
committed by GitHub
parent 03d772c2ec
commit afbe3997a8
3 changed files with 82 additions and 8 deletions

View File

@@ -83,9 +83,12 @@ export default function (
let highlighted = opts.highlightCode && chalk.supportsColor;
if (highlighted) rawLines = highlight(rawLines);
let linesAbove = opts.linesAbove || 2;
let linesBelow = opts.linesBelow || 3;
let lines = rawLines.split(NEWLINE);
let start = Math.max(lineNumber - 3, 0);
let end = Math.min(lines.length, lineNumber + 3);
let start = Math.max(lineNumber - (linesAbove + 1), 0);
let end = Math.min(lines.length, lineNumber + linesBelow);
if (!lineNumber && !colNumber) {
start = 0;