syntax-highlight Syntax-highlight code with language detection
Flags
--lang string default:
Examples
Syntax-highlight a JavaScript function
Usage
"function greet(name) {
return `Hello, ${name}!`;
}
console..." | syntax-highlight Highlight a Python code snippet for a tutorial blog post
Usage
"def fibonacci(n):
a, b = 0, 1
for _ in range(n):
..." | syntax-highlight Format and highlight a SQL query for presentation slides
Usage
"SELECT name, email
FROM users
WHERE active = true
ORDER BY n..." | syntax-highlight View source
async (input, opts = {})=>{
if (!input.trim()) return "";
try {
const hljs = await import('highlight.js/lib/common').then(async (m)=>{
await m.__tla;
return m;
});
const lang = opts.lang?.trim();
const result = lang ? hljs.default.highlight(input, {
language: lang
}) : hljs.default.highlightAuto(input);
const styleMap = {
"hljs-keyword": "color:#ff7b72",
"hljs-selector-tag": "color:#ff7b72",
"hljs-literal": "color:#ff7b72",
"hljs-type": "color:#ff7b72",
"hljs-string": "color:#a5d6ff",
"hljs-addition": "color:#a5d6ff;background:rgba(63,185,80,0.1)",
"hljs-number": "color:#79c0ff",
"hljs-comment": "color:#8b949e;font-style:italic",
"hljs-quote": "color:#8b949e;font-style:italic",
"hljs-title": "color:#d2a8ff;font-weight:600",
"hljs-section": "color:#d2a8ff;font-weight:600",
"hljs-built_in": "color:#ffa657",
"hljs-name": "color:#ffa657",
"hljs-attr": "color:#79c0ff",
"hljs-attribute": "color:#79c0ff",
"hljs-variable": "color:#ffa657",
"hljs-template-variable": "color:#ffa657",
"hljs-regexp": "color:#a5d6ff",
"hljs-deletion": "color:#ffa198;background:rgba(248,81,73,0.1)",
"hljs-params": "color:#c9d1d9",
"hljs-meta": "color:#8b949e",
"hljs-punctuation": "color:#8b949e"
};
const inlined = result.value.replace(/class="([^"]*)"/g, (_, classes)=>{
const hljsClass = classes.split(" ").find((c)=>styleMap[c]);
const style = hljsClass && styleMap[hljsClass];
return style ? `style="${style}"` : "";
});
return `<pre><code style="color:#c9d1d9">${inlined}</code></pre>`;
} catch (e) {
return `Error: ${e.message}`;
}
}