Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// sudo easy_install -U Pygments
// alias c='pygmentize -O style=monokai -f console256 -g'
// Performance-wise they appear to be pretty much the same.
const fs = require('fs');
const emphasize = require('emphasize');
const filePath = process.argv[2];
if (!filePath) {
console.error('USAGE: c filename.ext');
process.exit(1);
}
const doc = fs.readFileSync(filePath, 'utf8');
const output = emphasize.highlightAuto(doc).value;
process.stdout.write(output);
// Handle pretty-printing common types of content (by header).
if (response.headers && response.headers['content-type']) {
let type = response.headers['content-type'];
if (type.includes('application/json')) {
return printJson(response.data);
} else if (type.includes('text/html') || type.includes('application/xml')) {
return print(emphasize.highlight('xml', response.data).value);
} else if (type.includes('text/plain')) {
return print(response.data);
}
}
// ...or try to guess!
return print(emphasize.highlightAuto(response.data).value);
}
}
const highlightSyntax = (txt: string, lang?: string): string => {
if (lang) {
try {
return emphasize.highlight(lang, txt).value;
} catch (e) {
// fallback for unknown languages...
}
}
return emphasize.highlightAuto(txt).value;
};
function highlight (error) {
let source = error.source.replace(/\t/g, ' ');
if (chalk.enabled) {
const extname = path.extname(error.fileName).slice(1);
if (extname) {
try {
return emphasize.highlight(extname.slice(1), source).value;
} catch (ex) {
//
}
}
source = emphasize.highlightAuto(source).value;
}
return source;
}