90 lines
3 KiB
JavaScript
90 lines
3 KiB
JavaScript
|
"use strict";
|
||
|
function __export(m) {
|
||
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||
|
}
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
var hljs = require("highlight.js");
|
||
|
var parse5 = require("parse5");
|
||
|
var theme_1 = require("./theme");
|
||
|
function colorizeNode(node, theme, context) {
|
||
|
if (theme === void 0) { theme = {}; }
|
||
|
switch (node.type) {
|
||
|
case 'text': {
|
||
|
var text = node.data;
|
||
|
if (context === undefined) {
|
||
|
return (theme.default || theme_1.DEFAULT_THEME.default || theme_1.plain)(text);
|
||
|
}
|
||
|
else {
|
||
|
return text;
|
||
|
}
|
||
|
}
|
||
|
case 'tag': {
|
||
|
var hljsClass = /hljs-(\w+)/.exec(node.attribs.class);
|
||
|
if (hljsClass) {
|
||
|
var token_1 = hljsClass[1];
|
||
|
var nodeData = node.childNodes
|
||
|
.map(function (node) { return colorizeNode(node, theme, token_1); })
|
||
|
.join('');
|
||
|
return (theme[token_1] || theme_1.DEFAULT_THEME[token_1] || theme_1.plain)(nodeData);
|
||
|
}
|
||
|
// Return the data itself when the class name isn't prefixed with a highlight.js token prefix.
|
||
|
// This is common in instances of sublanguages (JSX, Markdown Code Blocks, etc.)
|
||
|
return node.childNodes.map(function (node) { return colorizeNode(node, theme); }).join('');
|
||
|
}
|
||
|
}
|
||
|
throw new Error('Invalid node type ' + node.type);
|
||
|
}
|
||
|
function colorize(code, theme) {
|
||
|
if (theme === void 0) { theme = {}; }
|
||
|
var fragment = parse5.parseFragment(code, {
|
||
|
treeAdapter: parse5.treeAdapters.htmlparser2,
|
||
|
});
|
||
|
return fragment.childNodes.map(function (node) { return colorizeNode(node, theme); }).join('');
|
||
|
}
|
||
|
/**
|
||
|
* Apply syntax highlighting to `code` with ASCII color codes. The language is automatically
|
||
|
* detected if not set.
|
||
|
*
|
||
|
* ```ts
|
||
|
* import {highlight} from 'cli-highlight';
|
||
|
* import * as fs from 'fs';
|
||
|
*
|
||
|
* fs.readFile('package.json', 'utf8', (err: any, json: string) => {
|
||
|
* console.log('package.json:');
|
||
|
* console.log(highlight(json));
|
||
|
* });
|
||
|
* ```
|
||
|
*
|
||
|
* @param code The code to highlight
|
||
|
* @param options Optional options
|
||
|
*/
|
||
|
function highlight(code, options) {
|
||
|
if (options === void 0) { options = {}; }
|
||
|
var html;
|
||
|
if (options.language) {
|
||
|
html = hljs.highlight(options.language, code, options.ignoreIllegals, options.continuation).value;
|
||
|
}
|
||
|
else {
|
||
|
html = hljs.highlightAuto(code, options.languageSubset).value;
|
||
|
}
|
||
|
return colorize(html, options.theme);
|
||
|
}
|
||
|
exports.highlight = highlight;
|
||
|
/**
|
||
|
* Returns all supported languages
|
||
|
*/
|
||
|
function listLanguages() {
|
||
|
return hljs.listLanguages();
|
||
|
}
|
||
|
exports.listLanguages = listLanguages;
|
||
|
/**
|
||
|
* Returns true if the language is supported
|
||
|
* @param name A language name, alias or file extension
|
||
|
*/
|
||
|
function supportsLanguage(name) {
|
||
|
return !!hljs.getLanguage(name);
|
||
|
}
|
||
|
exports.supportsLanguage = supportsLanguage;
|
||
|
exports.default = highlight;
|
||
|
__export(require("./theme"));
|
||
|
//# sourceMappingURL=index.js.map
|