xonecas.io
719
Le

lextide / files

lextide / src / types.ts

1.9 KB
1export interface RootData {
2	language?: string | undefined;
3	relevance?: number | undefined;
4}
5
6export interface Text {
7	type: "text";
8	value: string;
9}
10
11export interface Element {
12	type: "element";
13	tagName: "span";
14	properties: {
15		className: string[];
16	};
17	children: ElementContent[];
18}
19
20export type ElementContent = Element | Text;
21
22export interface Root {
23	type: "root";
24	children: ElementContent[];
25	data?: RootData;
26}
27
28export interface Options {
29	prefix?: string | null | undefined;
30}
31
32export interface AutoOptions extends Options {
33	subset?: readonly string[] | null | undefined;
34}
35
36export interface StreamOptions extends Options {
37	allowRecalls?: boolean | null | undefined;
38}
39
40export interface StreamUpdate {
41	recall: number;
42	stable: ElementContent[];
43	unstable: ElementContent[];
44}
45
46export interface HighlightStream {
47	write(chunk: string): StreamUpdate;
48	end(chunk?: string): StreamUpdate;
49	snapshot(): Root;
50}
51
52export interface GrammarRule {
53	match: RegExp | ((input: string, pos: number) => number | null);
54	scope?: string;
55	push?: string;
56	pop?: boolean;
57	children?: GrammarRule[];
58	relevance?: number;
59}
60
61export interface GrammarDefinition {
62	name: string;
63	aliases?: string[];
64	relevance?: number;
65	states: Record<string, GrammarRule[]>;
66	startState: string;
67}
68
69export type LanguageFn = () => GrammarDefinition;
70
71export interface Lowlight {
72	highlight(language: string, value: string, options?: Options | null): Root;
73	highlightAuto(value: string, options?: AutoOptions | null): Root;
74	listLanguages(): string[];
75	register(name: string, grammar: LanguageFn): void;
76	register(grammars: Readonly<Record<string, LanguageFn>>): void;
77	registerAlias(language: string, alias: readonly string[] | string): void;
78	registerAlias(
79		aliases: Readonly<Record<string, readonly string[] | string>>,
80	): void;
81	registered(aliasOrName: string): boolean;
82	stream(language: string, options?: StreamOptions | null): HighlightStream;
83}