highlighter
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,8 +0,8 @@ | ||
var hljs = require('highlight.js'); | ||
var hljs = require('highlight.js') | ||
var SEPARATOR = '\n'; | ||
var SEPARATOR = '\n' | ||
var CHUNK_REGEXP = /^\-{3} [^\-]+ \-{4}$|^\*{3} [^\*]+ \*{4}$|^@@ [^@]+ @@$/; | ||
var HEADER_REGEXP = /^Index\: |^[\+\-\*]{3}|^[\*\=]{5,}$/; | ||
var DIFF_REGEXP = /\.(?:diff|patch)$/i; | ||
var CHUNK_REGEXP = /^\-{3} [^\-]+ \-{4}$|^\*{3} [^\*]+ \*{4}$|^@@ [^@]+ @@$/ | ||
var HEADER_REGEXP = /^Index\: |^[\+\-\*]{3}|^[\*\=]{5,}$/ | ||
var DIFF_REGEXP = /\.(?:diff|patch)$/i | ||
@@ -13,3 +13,3 @@ var PATCH_TYPES = { | ||
'!': 'change' | ||
}; | ||
} | ||
@@ -19,3 +19,3 @@ /** | ||
*/ | ||
module.exports = highlighter; | ||
module.exports = highlighter | ||
@@ -30,4 +30,4 @@ /** | ||
return function (code, lang) { | ||
return highlight(code, lang, opts); | ||
}; | ||
return highlight(code, lang, opts) | ||
} | ||
} | ||
@@ -42,3 +42,3 @@ | ||
function supported (lang) { | ||
return !!hljs.getLanguage(lang); | ||
return !!hljs.getLanguage(lang) | ||
} | ||
@@ -55,16 +55,16 @@ | ||
if (!lang) { | ||
return code; | ||
return code | ||
} | ||
var isDiff = DIFF_REGEXP.test(lang); | ||
var isDiff = DIFF_REGEXP.test(lang) | ||
// Remove the diff suffix. E.g. "javascript.diff". | ||
lang = lang.replace(DIFF_REGEXP, ''); | ||
lang = lang.replace(DIFF_REGEXP, '') | ||
// Ignore unknown languages. | ||
if (!isDiff && !supported(lang)) { | ||
return code; | ||
return code | ||
} | ||
return isDiff ? diff(code, lang) : hljs.highlight(lang, code).value; | ||
return isDiff ? diff(code, lang) : hljs.highlight(lang, code).value | ||
} | ||
@@ -80,18 +80,18 @@ | ||
function diff (code, lang) { | ||
var sections = []; | ||
var sections = [] | ||
code.split(/\r?\n/g).forEach(function (line) { | ||
var type; | ||
var type | ||
if (CHUNK_REGEXP.test(line)) { | ||
type = 'chunk'; | ||
type = 'chunk' | ||
} else if (HEADER_REGEXP.test(line)) { | ||
type = 'header'; | ||
type = 'header' | ||
} else { | ||
type = PATCH_TYPES[line[0]] || 'null'; | ||
line = line.replace(/^[\+\-\! ]/, ''); | ||
type = PATCH_TYPES[line[0]] || 'null' | ||
line = line.replace(/^[\+\-\! ]/, '') | ||
} | ||
// Merge data with the previous section where possible. | ||
var previous = sections[sections.length - 1]; | ||
var previous = sections[sections.length - 1] | ||
@@ -102,18 +102,18 @@ if (!previous || previous.type !== type) { | ||
lines: [line] | ||
}); | ||
}) | ||
return; | ||
return | ||
} | ||
previous.lines.push(line); | ||
}); | ||
previous.lines.push(line) | ||
}) | ||
return highlightSections(sections, lang) | ||
.map(function (section) { | ||
var type = section.type; | ||
var value = section.lines.join(SEPARATOR); | ||
var type = section.type | ||
var value = section.lines.join(SEPARATOR) | ||
return '<span class="diff-' + type + '">' + value + '</span>'; | ||
return '<span class="diff-' + type + '">' + value + '</span>' | ||
}) | ||
.join(SEPARATOR); | ||
.join(SEPARATOR) | ||
} | ||
@@ -130,37 +130,37 @@ | ||
if (!supported(lang)) { | ||
return sections; | ||
return sections | ||
} | ||
// Keep track of the most recent stacks. | ||
var additionStack; | ||
var deletionStack; | ||
var additionStack | ||
var deletionStack | ||
sections | ||
.forEach(function (section) { | ||
var type = section.type; | ||
var type = section.type | ||
// Reset the stacks for metadata types. | ||
if (type === 'header' || type === 'chunk') { | ||
additionStack = deletionStack = null; | ||
additionStack = deletionStack = null | ||
return; | ||
return | ||
} | ||
var value = section.lines.join(SEPARATOR); | ||
var stack = type === 'deletion' ? deletionStack : additionStack; | ||
var highlight = hljs.highlight(lang, value, false, stack); | ||
var value = section.lines.join(SEPARATOR) | ||
var stack = type === 'deletion' ? deletionStack : additionStack | ||
var highlight = hljs.highlight(lang, value, false, stack) | ||
// Set the top of each stack, depending on context. | ||
if (type === 'addition') { | ||
additionStack = highlight.top; | ||
additionStack = highlight.top | ||
} else if (type === 'deletion') { | ||
deletionStack = highlight.top; | ||
deletionStack = highlight.top | ||
} else { | ||
additionStack = deletionStack = highlight.top; | ||
additionStack = deletionStack = highlight.top | ||
} | ||
section.lines = highlight.value.split(SEPARATOR); | ||
}); | ||
section.lines = highlight.value.split(SEPARATOR) | ||
}) | ||
return sections; | ||
return sections | ||
} |
{ | ||
"name": "highlighter", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Add syntax highlighting to all files, with built-in syntax highlighting of diffs and patches.", | ||
"main": "highlighter.js", | ||
"files": [ | ||
"highlighter.js", | ||
"LICENSE" | ||
], | ||
"scripts": { | ||
"test": "istanbul cover _mocha -- -R spec" | ||
"lint": "standard", | ||
"test-std": "mocha -- -R spec --bail", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec --bail", | ||
"test": "npm run lint && npm run test-cov" | ||
}, | ||
@@ -31,5 +38,7 @@ "repository": { | ||
"devDependencies": { | ||
"chai": "^1.10.0", | ||
"chai": "^2.0.0", | ||
"istanbul": "^0.3.5", | ||
"mocha": "^2.0.1" | ||
"mocha": "^2.0.1", | ||
"pre-commit": "^1.0.4", | ||
"standard": "^2.6.3" | ||
}, | ||
@@ -36,0 +45,0 @@ "dependencies": { |
# Highlighter | ||
[![NPM version][npm-image]][npm-url] | ||
[![NPM downloads][downloads-image]][downloads-url] | ||
[![Build status][travis-image]][travis-url] | ||
@@ -23,3 +24,3 @@ [![Test coverage][coveralls-image]][coveralls-url] | ||
**Highlighter** can be used programmatically, by passsing in the source string and the source language. | ||
**Highlighter** can be used programmatically, by passing in the source string and the source language. | ||
@@ -64,2 +65,4 @@ ```js | ||
[npm-url]: https://npmjs.org/package/highlighter | ||
[downloads-image]: https://img.shields.io/npm/dm/highlighter.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/highlighter | ||
[travis-image]: https://img.shields.io/travis/blakeembrey/highlighter.svg?style=flat | ||
@@ -66,0 +69,0 @@ [travis-url]: https://travis-ci.org/blakeembrey/highlighter |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70
7953
5
4
129
1