forked from code-hike/codehike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.tsx
More file actions
129 lines (120 loc) · 3.09 KB
/
Copy pathblock.tsx
File metadata and controls
129 lines (120 loc) · 3.09 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
import { renderLineContent } from "./inline.js"
import { isGroup, LineGroup, LinesOrGroups, LineTokens } from "./lines.js"
import { toLineContent } from "./tokens.js"
import { InnerLine } from "./inner.js"
import {
AnnotationHandler,
BlockAnnotation,
CustomLineProps,
InlineAnnotation,
} from "./types.js"
export function renderLines({
linesOrGroups,
handlers,
inlineAnnotations,
annotationStack = [],
}: {
linesOrGroups: LinesOrGroups
handlers: AnnotationHandler[]
inlineAnnotations: InlineAnnotation[]
annotationStack?: BlockAnnotation[]
}) {
return linesOrGroups.map((group) =>
isGroup(group) ? (
<LineBlock
key={group.range[0]}
group={group}
handlers={handlers}
inlineAnnotations={inlineAnnotations}
annotationStack={annotationStack}
/>
) : (
<Line
key={group.lineNumber}
line={group}
handlers={handlers}
inlineAnnotations={inlineAnnotations}
annotationStack={annotationStack}
/>
),
)
}
function Line({
line,
handlers,
inlineAnnotations,
annotationStack = [],
}: {
line: LineTokens
handlers: AnnotationHandler[]
inlineAnnotations: InlineAnnotation[]
annotationStack?: BlockAnnotation[]
}) {
const { lineNumber, totalLines, indentation } = line
const lineAnnotations = inlineAnnotations.filter(
(annotation) => annotation.lineNumber === lineNumber,
)
const lineContent = toLineContent(line.tokens, lineAnnotations)
const stack = buildLineStack(handlers, annotationStack)
let children: React.ReactNode = renderLineContent({
content: lineContent,
handlers,
annotationStack,
})
const merge = { lineNumber, indentation, totalLines, _stack: stack }
return (
<InnerLine merge={merge} key={lineNumber}>
{children}
</InnerLine>
)
}
// A group of lines targeted by a block annotation
function LineBlock({
group,
handlers,
inlineAnnotations,
annotationStack,
}: {
group: LineGroup
handlers: AnnotationHandler[]
inlineAnnotations: InlineAnnotation[]
annotationStack: BlockAnnotation[]
}) {
const { annotation, lines } = group
const { name } = annotation
const Component = handlers.find((c) => c.name === name)?.Block
const children = renderLines({
linesOrGroups: lines,
handlers,
inlineAnnotations,
annotationStack: [...annotationStack, annotation],
})
return Component ? (
<Component annotation={annotation}>{children}</Component>
) : (
children
)
}
function buildLineStack(
handlers: AnnotationHandler[],
annotationStack: BlockAnnotation[],
) {
const stack = [] as CustomLineProps["_stack"]
handlers.forEach(({ name, Line, AnnotatedLine }) => {
const annotations = annotationStack.filter((a) => a.name === name)
if (AnnotatedLine) {
annotations.forEach((annotation) => {
stack.push({ Component: AnnotatedLine, annotation })
})
}
if (Line) {
if (!annotations.length) {
stack.push({ Component: Line })
}
annotations.forEach((annotation) => {
stack.push({ Component: Line, annotation })
})
}
})
return stack
}