@tanstack/highlight
Version:
Tiny class-based syntax highlighting for documentation.
91 lines (90 loc) • 3.44 kB
TypeScript
export type HighlightTokenClass = 'attr' | 'code-inline' | 'command' | 'comment' | 'deleted' | 'function' | 'heading' | 'inserted' | 'keyword' | 'link' | 'literal' | 'meta' | 'number' | 'operator' | 'property' | 'selector' | 'string' | 'tag' | 'type' | 'variable';
export type HighlightToken = {
className?: HighlightTokenClass;
value: string;
};
export type TokenRange = {
className: HighlightTokenClass;
end: number;
start: number;
};
export type TokenizerContext = {
hasLanguage: (lang: string) => boolean;
tokenize: (code: string, lang: string) => Array<TokenRange>;
};
export type LanguageDefinition<Name extends string = string> = {
aliases?: ReadonlyArray<string>;
name: Name;
tokenize: (code: string, context: TokenizerContext) => ReadonlyArray<TokenRange>;
};
export type HighlightDecorationData = Record<string, boolean | number | string>;
type DecorationBase = {
className?: string;
data?: HighlightDecorationData;
};
export type HighlightRangeDecoration = DecorationBase & {
lines?: never;
range: readonly [start: number, end: number];
};
export type HighlightLineDecoration = DecorationBase & {
lines: number | readonly [start: number, end: number];
range?: never;
};
export type HighlightDecoration = HighlightLineDecoration | HighlightRangeDecoration;
export type HighlightOptions = {
decorations?: ReadonlyArray<HighlightDecoration>;
lang?: string;
lineNumbers?: boolean;
};
export type HighlightTokenResult = {
code: string;
lang: string;
tokens: Array<HighlightToken>;
};
export type HighlightResult = HighlightTokenResult & {
html: string;
};
export type RenderCodeBlockOptions = HighlightOptions & {
title?: string;
};
export type RenderedCodeBlockData = {
copyText: string;
htmlMarkup: string;
lang: string;
title?: string;
tokens: Array<HighlightToken>;
};
export type HighlightTextNode = {
type: 'text';
value: string;
};
export type HighlightElementNode = {
children: Array<HighlightRenderNode>;
classNames: Array<string>;
data?: Record<string, string>;
type: 'element';
};
export type HighlightRenderNode = HighlightElementNode | HighlightTextNode;
export type Highlighter = {
highlight: (code: string, options?: HighlightOptions) => HighlightResult;
highlightToHtml: (code: string, options?: HighlightOptions) => string;
listLanguages: () => Array<string>;
normalizeLanguage: (lang?: string) => string;
renderCodeBlockData: (options: {
code: string;
decorations?: ReadonlyArray<HighlightDecoration>;
lang?: string;
lineNumbers?: boolean;
title?: string;
}) => RenderedCodeBlockData;
tokenize: (code: string, options?: HighlightOptions) => HighlightTokenResult;
};
export declare function defineLanguage<const Name extends string>(definition: LanguageDefinition<Name>): LanguageDefinition<Name>;
export declare function createHighlighter({ fallbackLanguage, languages, }: {
fallbackLanguage?: string;
languages: ReadonlyArray<LanguageDefinition>;
}): Highlighter;
export declare function renderTokens(tokens: ReadonlyArray<HighlightToken>, options?: Pick<HighlightOptions, 'decorations' | 'lineNumbers'>): Array<HighlightRenderNode>;
export declare function renderNodesToHtml(nodes: ReadonlyArray<HighlightRenderNode>): string;
export declare function escapeHtml(value: string): string;
export {};