Open History to see the last commit
1import { requireOptionalNativeModule } from "expo";
2
3import { highlightLanguageId } from "./highlightLanguage";
4import { HIGHLIGHTS_THEME_STYLES } from "./highlights.generated";
5
6export type HighlightScheme = "light" | "dark";
7
8export interface HighlightedToken {
9 readonly content: string;
10 readonly color: string | null;
11 readonly fontStyle: number | null;
12}
13
14export type HighlightedLines = ReadonlyArray<ReadonlyArray<HighlightedToken>>;
15
16const LINE_END = 0xffffffff;
17// Longer lines stay one plain token so a minified line does not become
18// thousands of native text runs.
19const MAX_HIGHLIGHTED_LINE_LENGTH = 1_000;
20
21// @pierre/highlights compiled to native code (modules/t3-highlights). Missing
22// from binaries built before it shipped, where code renders as plain text.
23const nativeHighlights = requireOptionalNativeModule<{
24 tokenize(source: string, language: number): ArrayBuffer | null;
25}>("T3Highlights");
26
27export interface TokenizedCode {
28 /** Retained UTF-16 source, compact native records, and line indexes. */
29 readonly byteLength: number;
30 readonly lineCount: number;
31 readonly highlightLine: (
32 index: number,
33 scheme: HighlightScheme,
34 ) => ReadonlyArray<HighlightedToken> | undefined;
35}
36
37/** Keep native records compact; expand only lines a client will display. */
38export function tokenizeCode(code: string, language: string): TokenizedCode {
39 const id = highlightLanguageId(language);
40 const buffer = id === 0 ? null : (nativeHighlights?.tokenize(code, id) ?? null);
41 const records = buffer ? new Uint32Array(buffer) : null;
42 const offsets = [0];
43 if (records) {
44 for (let record = 0; record < records.length; record += 2) {
45 if (records[record + 1] === LINE_END) offsets.push(record + 2);
46 }
47 } else {
48 for (let at = code.indexOf("\n"); at !== -1; at = code.indexOf("\n", at + 1))
49 offsets.push(at + 1);
50 }
51 const lineStarts = Uint32Array.from(offsets);
52 return {
53 byteLength: code.length * 2 + (records?.byteLength ?? 0) + lineStarts.byteLength,
54 lineCount: lineStarts.length,
55 highlightLine(index, scheme) {
56 if (index < 0 || index >= lineStarts.length) return undefined;
57 const first = lineStarts[index]!;
58 const next = lineStarts[index + 1];
59 if (!records) {
60 const end = next === undefined ? code.length : next - (code[next - 2] === "\r" ? 2 : 1);
61 const content = code.slice(first, end);
62 return content ? [{ content, color: null, fontStyle: null }] : [];
63 }
64 let start = first === 0 ? 0 : records[first - 2]!;
65 const last = next === undefined ? records.length : next - 2;
66 const end = last === first ? start : records[last - 2]!;
67 if (end - start > MAX_HIGHLIGHTED_LINE_LENGTH) {
68 return [{ content: code.slice(start, end), color: null, fontStyle: null }];
69 }
70 const styles = HIGHLIGHTS_THEME_STYLES[scheme];
71 const line: HighlightedToken[] = [];
72 for (let at = first; at < last; at += 2) {
73 const tokenEnd = records[at]!;
74 if (tokenEnd <= start) continue;
75 const [color, fontStyle] = styles[records[at + 1]!] ?? styles[0]!;
76 line.push({ content: code.slice(start, tokenEnd), color, fontStyle });
77 start = tokenEnd;
78 }
79 return line;
80 },
81 };
82}
83
84/** LF and CRLF split lines; a trailing line break yields a final empty line. */
85export function highlightCode(
86 code: string,
87 language: string,
88 scheme: HighlightScheme,
89): HighlightedLines {
90 const source = tokenizeCode(code, language);
91 return Array.from({ length: source.lineCount }, (_, index) =>
92 source.highlightLine(index, scheme)!,
93 );
94}