forked from code-hike/codehike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
202 lines (173 loc) · 5.47 KB
/
Copy pathtypes.ts
File metadata and controls
202 lines (173 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
export type Theme = string
type Color = string
export type Token = [string, Color?, React.CSSProperties?]
export type Whitespace = string
export type Tokens = (Token | Whitespace)[]
type Prettify<T> = {
[K in keyof T]: T[K]
} & {}
export type BlockAnnotation = {
/** Annotation name */
name: string
/** String metadata */
query: string
/** Line number where the annotation block starts */
fromLineNumber: number
/** Line number (inclusive) where the annotation block ends */
toLineNumber: number
/** Optional data */
data?: any
}
export type InlineAnnotation = {
/** Annotation name */
name: string
/** String metadata */
query: string
/** Line number */
lineNumber: number
/** Column number where the annotation starts */
fromColumn: number
/** Column number (inclusive) where the annotation ends */
toColumn: number
/** Optional data */
data?: any
}
export type CodeAnnotation = BlockAnnotation | InlineAnnotation
export function isBlockAnnotation(
annotation: CodeAnnotation,
): annotation is BlockAnnotation {
return !isInlineAnnotation(annotation)
}
export function isInlineAnnotation(
annotation: CodeAnnotation,
): annotation is InlineAnnotation {
return annotation.hasOwnProperty("lineNumber")
}
export function isWhitespace(token: Token | Whitespace): token is Whitespace {
return typeof token === "string"
}
/**
* Represents the basic structure for code data.
*/
export type RawCode = {
/** This is the raw code. May include annotation comments. */
value: string
/** The programming language. */
lang: string
/** Metadata string (the content after the language name in a markdown codeblock). */
meta: string
}
/**
* Represents detailed information about a piece of code, including its tokens and annotations.
*/
export type HighlightedCode = {
/** This is the raw code. May include annotation comments. */
value: string
/** The code with annotation comments removed. */
code: string
/** A list of code annotations. */
annotations: CodeAnnotation[]
/** The list of highlighted tokens. Whitespace tokens include newline characters. */
tokens: (Token | Whitespace)[]
/** The normalized (for example: py becomes python) language used for highlighting. */
lang: string
/** Metadata string */
meta: string
/** The name of the theme used for highlighting. */
themeName: string
/** The style object for the highlighted code. */
style: React.CSSProperties
}
export type BlockAnnotationComponent = React.ComponentType<{
annotation: BlockAnnotation
children: React.ReactNode
}>
export type InlineAnnotationComponent = React.ComponentType<{
annotation: InlineAnnotation
children: React.ReactNode
}>
export type InlineProps = React.HTMLAttributes<HTMLElement> & {
code: HighlightedCode
}
export type PreProps = React.HTMLAttributes<HTMLPreElement> & {
code: HighlightedCode
handlers?: AnnotationHandler[]
}
export type PreComponent = React.ForwardRefExoticComponent<
PreProps & React.RefAttributes<HTMLPreElement>
>
export type InternalToken = {
value: string
style?: React.CSSProperties
range: [number, number]
}
// Pre
export type CustomPreProps = React.ComponentProps<"pre"> & {
data?: Record<string, any>
_stack: React.ComponentType<CustomPreProps>[]
_ref: React.RefObject<HTMLPreElement>
}
export type CustomPre = React.ComponentType<CustomPreProps>
// Line
export type CustomLineProps = React.ComponentProps<"div"> & {
lineNumber: number
totalLines: number
indentation: number
annotation?: BlockAnnotation
data?: Record<string, any>
_stack: {
Component: CustomLine | CustomLineWithAnnotation
annotation?: BlockAnnotation
}[]
}
export type CustomLine = React.ComponentType<CustomLineProps>
export type CustomLineWithAnnotation = React.ComponentType<
CustomLineProps & {
annotation: BlockAnnotation
}
>
// Token
export type CustomTokenProps = React.ComponentProps<"span"> & {
value: string
style?: React.CSSProperties
data?: Record<string, any>
annotation?: BlockAnnotation | InlineAnnotation
_stack: {
Component: CustomToken | CustomTokenWithAnnotation
annotation?: BlockAnnotation | InlineAnnotation
}[]
}
export type CustomToken = React.ComponentType<CustomTokenProps>
export type CustomTokenWithAnnotation = React.ComponentType<
CustomTokenProps & { annotation: BlockAnnotation | InlineAnnotation }
>
type AnnotationTransformer<T> = (
annotation: T,
) => undefined | CodeAnnotation | CodeAnnotation[]
export type AnnotationHandler = {
/** Name of the annotation */
name: string
/** The Pre, Line and Token components are only used if there's at least one annotation in the whole codeblock */
onlyIfAnnotated?: boolean
/** Transform an annotation into one or more annotations */
transform?:
| AnnotationTransformer<InlineAnnotation>
| AnnotationTransformer<BlockAnnotation>
| AnnotationTransformer<CodeAnnotation>
/** Customize the pre element */
Pre?: CustomPre
/** Customize the pre element and use the ref to the DOM element */
PreWithRef?: CustomPre
/** Wrap an annotated block */
Block?: BlockAnnotationComponent
/** Customize how to render a line */
Line?: CustomLine
/** Customize how to render a line targeted by the annotation */
AnnotatedLine?: CustomLineWithAnnotation
/** Wrap an annotated part of a line */
Inline?: InlineAnnotationComponent
/** Customize how to render a token */
Token?: CustomToken
/** Customize how to render a token targeted by the annotation */
AnnotatedToken?: CustomTokenWithAnnotation
}