-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathcode.ts
183 lines (164 loc) · 4.2 KB
/
code.ts
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
import { highlight } from "../highlighter"
import { extractLinks } from "./links"
import { NodeInfo, splitChildren } from "./unist-utils"
import { CodeStep } from "../smooth-code"
import { EditorProps } from "../mini-editor"
import {
getAnnotationsFromMetastring,
extractAnnotationsFromCode,
extractJSXAnnotations,
} from "./annotations"
import { mergeFocus } from "../utils"
import { CodeNode, SuperNode } from "./nodes"
import { CodeHikeConfig } from "./config"
export function isEditorNode(node: SuperNode) {
return (
node.type === "code" ||
(node.type === "mdxJsxFlowElement" &&
node.name === "CH.Code")
)
}
export async function mapAnyCodeNode(
nodeInfo: NodeInfo,
config: CodeHikeConfig
) {
const { node } = nodeInfo
if (node.type === "code") {
return mapCode(nodeInfo as NodeInfo<CodeNode>, config)
} else {
return mapEditor(nodeInfo, config)
}
}
type Props = Omit<EditorProps, "codeConfig">
async function mapCode(
nodeInfo: NodeInfo<CodeNode>,
config: CodeHikeConfig
): Promise<Props> {
const file = await mapFile(nodeInfo, config)
const props: Props = {
northPanel: {
tabs: [file.name],
active: file.name,
heightRatio: 1,
},
files: [file],
}
return props
}
export async function mapEditor(
{ node }: NodeInfo,
config: CodeHikeConfig
): Promise<Props> {
const [northNodes, southNodes = []] = splitChildren(
node,
"thematicBreak"
)
const northFiles = await Promise.all(
northNodes
.filter(({ node }) => node.type === "code")
.map((nodeInfo: NodeInfo<CodeNode>) =>
mapFile(nodeInfo, config)
)
)
const southFiles = await Promise.all(
southNodes
.filter(({ node }) => node.type === "code")
.map((nodeInfo: NodeInfo<CodeNode>) =>
mapFile(nodeInfo, config)
)
)
const allFiles = [...northFiles, ...southFiles]
const northActive =
northFiles.find(f => f.active) || northFiles[0]
const southActive = southFiles.length
? southFiles.find(f => f.active) || southFiles[0]
: null
const northLines = northActive.code.lines.length || 1
const southLines = southActive?.code.lines.length || 0
const northRatio = southActive
? (northLines + 2) / (southLines + northLines + 4)
: 1
const southRatio = 1 - northRatio
const props = {
northPanel: {
tabs: northFiles.map(x => x.name) as any,
active: northActive.name as any,
heightRatio: northRatio,
},
southPanel: southFiles.length
? {
tabs: southFiles.map(x => x.name) as any,
active: southActive!.name as any,
heightRatio: southRatio,
}
: undefined,
files: allFiles as any,
}
return props
}
async function mapFile(
{ node, index, parent }: NodeInfo<CodeNode>,
config: CodeHikeConfig
): Promise<CodeStep & FileOptions & { name: string }> {
const { theme } = config
const lang = (node.lang as string) || "text"
const code = await highlight({
code: node.value as string,
lang,
theme,
})
const [commentAnnotations, commentFocus] =
extractAnnotationsFromCode(code)
const options = parseMetastring(
typeof node.meta === "string" ? node.meta : ""
)
const metaAnnotations = getAnnotationsFromMetastring(
options as any
)
const linkAnnotations = extractLinks(
node,
index,
parent,
node.value as string
)
const jsxAnnotations = extractJSXAnnotations(
node,
index,
parent
)
const file = {
...options,
focus: mergeFocus(options.focus, commentFocus),
code,
name: options.name || "",
annotations: [
...metaAnnotations,
...commentAnnotations,
...jsxAnnotations,
],
}
return file
}
type FileOptions = {
focus?: string
active?: string
hidden?: boolean
}
function parseMetastring(
metastring: string
): FileOptions & { name: string } {
const params = metastring.split(" ")
const options = {} as FileOptions
let name: string | null = null
params.forEach(param => {
const [key, value] = param.split("=")
if (value != null) {
;(options as any)[key] = value
} else if (name === null) {
name = key
} else {
;(options as any)[key] = true
}
})
return { name: name || "", ...options }
}