Skip to main content

highlight-utils

React-free helpers for code highlighting pipelines. Use these from build tools, server-side renderers, Web Workers, or small syntax-highlighting services that need Mantle-compatible Code Block HTML without importing React components.

For ordinary React UI, prefer CodeBlock and mantleCode() from @ngrok/mantle/code-block.

import {	decorateHighlightedHtml,	isSupportedLanguage,	normalizeIndentation,	parseLanguage,} from "@ngrok/mantle/highlight-utils";

Usage

Decorate Shiki HTML

decorateHighlightedHtml takes trusted Shiki-highlighted HTML and wraps each line in the markup Mantle's CodeBlock.Code expects for line numbers and highlighted lines.

import { decorateHighlightedHtml } from "@ngrok/mantle/highlight-utils"; const html = `<span class="line">const tunnel = await ngrok.forward();</span>`; const decoratedHtml = decorateHighlightedHtml({	html,	showLineNumbers: true,	lineNumberStart: 10,	highlightLines: [10],});

Only pass HTML produced by a trusted highlighter such as Shiki. The returned string is intended to be rendered as HTML by a Code Block pipeline.

Normalize indentation

normalizeIndentation trims leading and trailing blank space, removes the common indentation prefix, and rewrites leading whitespace to tabs or spaces.

import { normalizeIndentation } from "@ngrok/mantle/highlight-utils"; const code = normalizeIndentation(	`		function main() {			return "ready";		}	`,	{ indentation: "tabs" },);

Parse languages

Use parseLanguage and isSupportedLanguage when accepting language names from Markdown fences, user input, or service payloads.

import { isSupportedLanguage, parseLanguage } from "@ngrok/mantle/highlight-utils"; parseLanguage("language-tsx"); // "tsx"parseLanguage(undefined); // "text"isSupportedLanguage("python"); // true

API

ExportDescription
decorateHighlightedHtmlAdds Mantle line wrappers, line numbers, and highlighted-line classes.
normalizeIndentationNormalizes multiline code indentation to spaces or tabs.
inferIndentationChooses an indentation style from language and optional preference.
isIndentationType guard for "tabs" or "spaces".
supportedLanguagesRead-only list of languages Mantle recognizes for code highlighting.
isSupportedLanguageType guard for supported language strings.
parseLanguageParses language-* / lang-* class names with a "text" fallback.
defaultShowLineNumbersReturns Mantle's default line-number behavior for a language and code.
parseCodeBlockHighlightLinesParses line highlight input from arrays or comma-separated strings.
parseCodeBlockLineNumberStartParses a positive line-number start value.
parseCodeBlockShowLineNumbersParses boolean or "true" / "false" line-number flags.
tokenizeMetastring, normalizeValueLow-level helpers for code fence metadata parsing.

Types

import type {	DecorateHighlightedHtmlInput,	Indentation,	LineRange,	SupportedLanguage,} from "@ngrok/mantle/highlight-utils";