rehype-highlight
Advanced tools
Comparing version 4.0.1 to 4.1.0
107
index.js
'use strict' | ||
var toText = require('hast-util-to-text') | ||
var lowlight = require('lowlight') | ||
var visit = require('unist-util-visit') | ||
var createPlugin = require('./core') | ||
module.exports = attacher | ||
function attacher(options) { | ||
var settings = options || {} | ||
var name = 'hljs' | ||
var pos | ||
if (settings.aliases) { | ||
lowlight.registerAlias(settings.aliases) | ||
} | ||
if (settings.languages) { | ||
// eslint-disable-next-line guard-for-in | ||
for (let name in settings.languages) { | ||
lowlight.registerLanguage(name, settings.languages[name]) | ||
} | ||
} | ||
if (settings.prefix) { | ||
pos = settings.prefix.indexOf('-') | ||
name = pos > -1 ? settings.prefix.slice(0, pos) : settings.prefix | ||
} | ||
return transformer | ||
function transformer(tree) { | ||
visit(tree, 'element', visitor) | ||
} | ||
function visitor(node, index, parent) { | ||
var props | ||
var result | ||
var lang | ||
if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') { | ||
return | ||
} | ||
lang = language(node) | ||
if ( | ||
lang === false || | ||
(!lang && settings.subset === false) || | ||
(settings.plainText && settings.plainText.indexOf(lang) > -1) | ||
) { | ||
return | ||
} | ||
props = node.properties | ||
if (!props.className) { | ||
props.className = [] | ||
} | ||
if (props.className.indexOf(name) < 0) { | ||
props.className.unshift(name) | ||
} | ||
try { | ||
result = lang | ||
? lowlight.highlight(lang, toText(parent), options) | ||
: lowlight.highlightAuto(toText(parent), options) | ||
} catch (error) { | ||
if (!settings.ignoreMissing || !/Unknown language/.test(error.message)) { | ||
throw error | ||
} | ||
result = {} | ||
} | ||
if (!lang && result.language) { | ||
props.className.push('language-' + result.language) | ||
} | ||
if (result.value) { | ||
node.children = result.value | ||
} | ||
} | ||
} | ||
// Get the programming language of `node`. | ||
function language(node) { | ||
var className = node.properties.className || [] | ||
var index = -1 | ||
var value | ||
while (++index < className.length) { | ||
value = className[index] | ||
if (value === 'no-highlight' || value === 'nohighlight') { | ||
return false | ||
} | ||
if (value.slice(0, 5) === 'lang-') { | ||
return value.slice(5) | ||
} | ||
if (value.slice(0, 9) === 'language-') { | ||
return value.slice(9) | ||
} | ||
} | ||
} | ||
module.exports = createPlugin(lowlight) |
{ | ||
"name": "rehype-highlight", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "rehype plugin to highlight code blocks with lowlight (highlight.js)", | ||
@@ -28,3 +28,5 @@ "license": "MIT", | ||
"files": [ | ||
"index.js" | ||
"core.js", | ||
"index.js", | ||
"light.js" | ||
], | ||
@@ -31,0 +33,0 @@ "dependencies": { |
@@ -108,2 +108,33 @@ # rehype-highlight | ||
## Browser | ||
It is not suggested to require `rehype-highlight` in the browser as it will | ||
include all the highlighters. | ||
> :warning: Please configure `languages`, as otherwise nothing gets highlighted. | ||
In the example below, only the JavaScript and TypeScript | ||
highlighters are included: | ||
```js | ||
var vfile = require('to-vfile') | ||
var report = require('vfile-reporter') | ||
var rehype = require('rehype') | ||
var highlight = require('rehype-highlight/light') | ||
rehype() | ||
.data('settings', {fragment: true}) | ||
.use(highlight, { | ||
// Don’t forget to define the languages you need | ||
languages: { | ||
javascript: require('highlight.js/lib/languages/javascript'), | ||
typescript: require('highlight.js/lib/languages/typescript') | ||
} | ||
}) | ||
.process(vfile.readSync('example.html'), function (error, file) { | ||
console.error(report(error || file)) | ||
console.log(String(file)) | ||
}) | ||
``` | ||
## Security | ||
@@ -110,0 +141,0 @@ |
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
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
11811
6
96
212