
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
highlight-ts
Advanced tools
This is a port of the highlight.js to a TypeScript (as well as ECMAScript 6).
NOTE: Currently not all languages are supported. PRs are welcome.
import {
Options,
Highlighter,
// import basic APIs
registerLanguages,
htmlRender,
init,
process,
// import preferred languages
CPlusPlus,
TypeScript,
JavaScript,
Python,
Lua,
Markdown
} from 'highlight-ts';
// register languages
registerLanguages(
CPlusPlus,
TypeScript,
JavaScript,
Python,
Lua,
Markdown
);
const options: Options = {
classPrefix: '',
/* other options */
};
// initialize highlighter
const highlighter: Highlighter<string>
= init(htmlRender, options);
const source = `
interface Point {
x: number,
y: number,
}
function distance(a: Point, b: Point): number {
return Math.sqrt(
(a.x - b.x) ** 2 +
(a.y - b.y) ** 2);
}
`;
// render source with given language mode
const language = 'ts';
const html = process(highlighter, source, language);
// or highligh source using auto language detection
const languages = ['ts', 'js', 'md'];
const html = process(highlighter, source, languages);
// auto detection using all known language modes
const html = process(highlighter, source);
// display result
elm.innerHTML = `<pre>${html}</pre>`;
Currently the renderer API is quite simple:
export interface Renderer<Output> {
text(chunk: string): Output;
wrap(className: string, chunk: Output): Output;
join(chunks: Output[]): Output;
}
text()
call is using to render textual piece of source code.wrap()
call is using to wrap rendered chunk into span with specified class.join()
call is using to join several rendered chunks into single chunk.By example we can implement HTML renderer like this:
import { Renderer } from 'highligh-ts';
const htmlRender: Renderer<string> = {
text: (chunk: string) => chunk
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>'),
join: (chunks: string[]) => chunks.join(''),
wrap: (className: string, chunk: string) =>
`<span class="${className}">${chunk}</span>`
};
See the types.ts source to understand highlight mode definition syntax.
Read the original highlightjs docs to get more help how to implement syntax highlighting modes.
FAQs
Highlight.JS in TypeScript (and ES6).
The npm package highlight-ts receives a total of 361 weekly downloads. As such, highlight-ts popularity was classified as not popular.
We found that highlight-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.