rehype-highlight
Advanced tools
Comparing version 1.0.0 to 1.1.0
176
index.js
@@ -1,142 +0,88 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2015 Titus Wormer | ||
* @license MIT | ||
* @module rehype:highlight | ||
* @fileoverview Highlight code blocks. | ||
*/ | ||
'use strict'; | ||
/* eslint-env commonjs */ | ||
/* Dependencies */ | ||
var visit = require('unist-util-visit'); | ||
var lowlight = require('lowlight'); | ||
var toString = require('hast-util-to-string'); | ||
/** | ||
* Get the programming language of `node`. | ||
* | ||
* @param {Element} node - Node to check. | ||
* @return {boolean|string?} - Programming language of | ||
* `node`. `false` if the node shouldn’t be highlighted. | ||
*/ | ||
function language(node) { | ||
var className = node.properties.className || []; | ||
var length = className.length; | ||
var index = -1; | ||
var value; | ||
module.exports = attacher; | ||
while (++index < length) { | ||
value = className[index]; | ||
function attacher(origin, options) { | ||
var settings = options || {}; | ||
var detect = settings.subset !== false; | ||
var prefix = settings.prefix; | ||
var name = 'hljs'; | ||
var pos; | ||
if (value === 'no-highlight' || value === 'nohighlight') { | ||
return false; | ||
} | ||
if (prefix) { | ||
pos = prefix.indexOf('-'); | ||
name = pos === -1 ? prefix : prefix.slice(0, pos); | ||
} | ||
if (value.slice(0, 5) === 'lang-') { | ||
return value.slice(5); | ||
} | ||
return transformer; | ||
if (value.slice(0, 9) === 'language-') { | ||
return value.slice(9); | ||
} | ||
} | ||
function transformer(tree) { | ||
visit(tree, 'element', visitor); | ||
} | ||
return null; | ||
} | ||
function visitor(node, index, parent) { | ||
var props = node.properties; | ||
var result; | ||
var lang; | ||
/** | ||
* Get the text content of `node`. | ||
* | ||
* @param {Node} node - Node to stringify. | ||
* @return {string} - Content. | ||
*/ | ||
function text(node) { | ||
var children = node.children; | ||
var length = children.length; | ||
var result = []; | ||
var index = -1; | ||
var child; | ||
var value; | ||
if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') { | ||
return; | ||
} | ||
while (++index < length) { | ||
child = children[index]; | ||
value = '' | ||
lang = language(node); | ||
if (child.children) { | ||
value = text(child); | ||
} else if (child.type === 'text') { | ||
value = child.value; | ||
} | ||
if (lang === false || (!lang && !detect)) { | ||
return; | ||
} | ||
result[index] = value; | ||
if (!props.className) { | ||
props.className = []; | ||
} | ||
return result.join(''); | ||
} | ||
if (props.className.indexOf(name) === -1) { | ||
props.className.unshift(name); | ||
} | ||
/** | ||
* Attacher. | ||
* | ||
* @param {Unified} origin - Origin processor. | ||
* @param {Object} [options={}] - Configuration. | ||
* @return {Function} - Transformer. | ||
*/ | ||
function attacher(origin, options) { | ||
var settings = options || {}; | ||
var detect = settings.subset !== false; | ||
var prefix = settings.prefix; | ||
var name = 'hljs'; | ||
var pos; | ||
if (lang) { | ||
result = lowlight.highlight(lang, toString(node), options); | ||
} else { | ||
result = lowlight.highlightAuto(toString(node), options); | ||
if (prefix) { | ||
pos = prefix.indexOf('-'); | ||
name = pos === -1 ? prefix : prefix.slice(0, pos); | ||
if (result.language) { | ||
props.className.push('language-' + result.language); | ||
} | ||
} | ||
return function (tree) { | ||
visit(tree, 'element', function (node, index, parent) { | ||
var props = node.properties; | ||
var result; | ||
var lang; | ||
node.children = result.value; | ||
} | ||
} | ||
if ( | ||
!parent || | ||
parent.tagName !== 'pre' || | ||
node.tagName !== 'code' | ||
) { | ||
return; | ||
} | ||
/* Get the programming language of `node`. */ | ||
function language(node) { | ||
var className = node.properties.className || []; | ||
var length = className.length; | ||
var index = -1; | ||
var value; | ||
lang = language(node); | ||
while (++index < length) { | ||
value = className[index]; | ||
if (lang === false || (!lang && !detect)) { | ||
return; | ||
} | ||
if (value === 'no-highlight' || value === 'nohighlight') { | ||
return false; | ||
} | ||
if (!props.className) { | ||
props.className = []; | ||
} | ||
if (value.slice(0, 5) === 'lang-') { | ||
return value.slice(5); | ||
} | ||
if (props.className.indexOf(name) === -1) { | ||
props.className.unshift(name); | ||
} | ||
if (value.slice(0, 9) === 'language-') { | ||
return value.slice(9); | ||
} | ||
} | ||
if (lang) { | ||
result = lowlight.highlight(lang, text(node), options); | ||
} else { | ||
result = lowlight.highlightAuto(text(node), options); | ||
if (result.language) { | ||
props.className.push('language-' + result.language); | ||
} | ||
} | ||
node.children = result.value; | ||
}); | ||
}; | ||
return null; | ||
} | ||
/* Expose. */ | ||
module.exports = attacher; |
{ | ||
"name": "rehype-highlight", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Highlight code blocks", | ||
@@ -13,9 +13,3 @@ "license": "MIT", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/rehype-highlight.git" | ||
}, | ||
"repository": "https://github.com/wooorm/rehype-highlight", | ||
"bugs": "https://github.com/wooorm/rehype-highlight/issues", | ||
@@ -26,3 +20,7 @@ "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"hast-util-to-string": "^1.0.0", | ||
"lowlight": "^1.2.0", | ||
@@ -33,29 +31,36 @@ "unist-util-visit": "^1.1.0" | ||
"browserify": "^13.0.1", | ||
"eslint": "^2.0.0", | ||
"esmangle": "^1.0.1", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^3.0.0", | ||
"jscs-jsdoc": "^2.0.0", | ||
"rehype": "^1.0.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-usage": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"nyc": "^10.0.0", | ||
"rehype": "^3.0.0", | ||
"remark-cli": "^2.1.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.0.0", | ||
"unist-util-inspect": "^4.0.0" | ||
"unist-util-inspect": "^4.0.0", | ||
"xo": "^0.17.1" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail", | ||
"build-md": "remark . -qfo", | ||
"build-bundle": "browserify index.js --bare -s rehypeHighlight > rehype-highlight.js", | ||
"build-mangle": "esmangle rehype-highlight.js > rehype-highlight.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint-api": "eslint .", | ||
"lint-style": "jscs --reporter inline .", | ||
"lint": "npm run lint-api && npm run lint-style", | ||
"test-api": "node test.js", | ||
"test-coverage": "istanbul cover test.js", | ||
"lint": "xo", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"xo": { | ||
"space": true, | ||
"ignores": [ | ||
"rehype-highlight.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"presets": "wooorm" | ||
} | ||
} |
# rehype-highlight [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
<!--lint disable heading-increment list-item-spacing--> | ||
Syntax highlighting for [**rehype**][rehype]. | ||
@@ -9,3 +7,3 @@ | ||
[npm][npm-install]: | ||
[npm][]: | ||
@@ -18,18 +16,14 @@ ```bash | ||
Dependencies: | ||
```javascript | ||
var rehype = require('rehype'); | ||
var highlight = require('rehype-highlight'); | ||
``` | ||
Transform: | ||
var file = rehype().use(highlight).process([ | ||
'<h1>Hello World!</h1>', | ||
'', | ||
'<pre><code class="language-js">var name = "World";', | ||
'console.warn("Hello, " + name + "!")</code></pre>' | ||
].join('\n'), {fragment: true}); | ||
```javascript | ||
var file = rehype().use(highlight).process([ | ||
'<h1>Hello World!</h1>', | ||
'', | ||
'<pre><code class="language-js">var name = "World";', | ||
'console.warn("Hello, " + name + "!")</code></pre>' | ||
].join('\n')); | ||
console.log(String(file)); | ||
``` | ||
@@ -48,3 +42,3 @@ | ||
### `rehype.use(highlight[, options])` | ||
### `rehype().use(highlight[, options])` | ||
@@ -60,3 +54,3 @@ Syntax highlight `pre > code`. Uses [**lowlight**][lowlight] under | ||
* `prefix` (`string`, default: 'hljs-') | ||
* `prefix` (`string`, default: `'hljs-'`) | ||
— Prefix to use before classes; | ||
@@ -81,3 +75,3 @@ * `subset` (`boolean` or `Array.<string>`, default: all languages) | ||
[npm-install]: https://docs.npmjs.com/cli/install | ||
[npm]: https://docs.npmjs.com/cli/install | ||
@@ -84,0 +78,0 @@ [license]: LICENSE |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
9
6745
3
4
67
82
1
+ Addedhast-util-to-string@^1.0.0
+ Addedhast-util-to-string@1.0.4(transitive)