Skip to content

Commit ea2b270

Browse files
committed
Refactor
1 parent e149e3d commit ea2b270

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

packages/mdx/src/annotations.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,27 @@ export function extractJSXAnnotations(
198198
"mdxJsxFlowElement" &&
199199
parent.children[nextIndex].name === "CH.Annotation"
200200
) {
201-
const { attributes } = parent.children[nextIndex] as any
201+
const jsxAnnotation = parent.children[nextIndex] as any
202+
203+
// copy attributes to props
202204
const props = {} as any
203-
attributes.forEach((attr: any) => {
205+
jsxAnnotation.attributes.forEach((attr: any) => {
204206
props[attr.name] = attr.value
205207
})
206208
const { as, focus, ...data } = props
209+
207210
const Component = annotationsMap[as] || as
208-
annotations.push({ Component, focus, data })
209-
console.log(parent.children[nextIndex])
211+
annotations.push({
212+
Component,
213+
focus,
214+
data: isEmpty(data) ? undefined : data,
215+
})
216+
// console.log(jsxAnnotation)
210217
parent.children.splice(nextIndex, 1)
211218
}
212219
return annotations
213220
}
221+
222+
function isEmpty(obj: any) {
223+
return Object.keys(obj).length === 0
224+
}

packages/mdx/src/code.tsx

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ export function isEditorNode(node: Node) {
6767
)
6868
}
6969

70+
async function transformCode(
71+
nodeInfo: NodeInfo,
72+
config: { theme: any }
73+
) {
74+
toJSX(nodeInfo.node, {
75+
name: "CH.Code",
76+
props: await mapCode(nodeInfo, config),
77+
})
78+
}
79+
export async function transformEditor(
80+
nodeInfo: NodeInfo,
81+
config: { theme: any }
82+
) {
83+
toJSX(nodeInfo.node, {
84+
name: "CH.Code",
85+
props: await mapEditor(nodeInfo, config),
86+
})
87+
}
88+
7089
export async function mapAnyCodeNode(
7190
nodeInfo: NodeInfo,
7291
config: { theme: any }
@@ -98,39 +117,6 @@ async function mapCode(
98117
return props
99118
}
100119

101-
async function transformCode(
102-
nodeInfo: NodeInfo,
103-
config: { theme: any }
104-
) {
105-
const file = await mapFile(nodeInfo, config)
106-
const props = {
107-
northPanel: {
108-
tabs: [file.name],
109-
active: file.name,
110-
heightRatio: 1,
111-
},
112-
files: [file],
113-
codeConfig: {
114-
theme: config.theme,
115-
},
116-
}
117-
toJSX(nodeInfo.node, {
118-
name: "CH.Code",
119-
props,
120-
})
121-
}
122-
export async function transformEditor(
123-
nodeInfo: NodeInfo,
124-
config: { theme: any }
125-
) {
126-
const props = await mapEditor(nodeInfo, config)
127-
128-
toJSX(nodeInfo.node, {
129-
name: "CH.Code",
130-
props,
131-
})
132-
}
133-
134120
export async function mapEditor(
135121
{ node }: NodeInfo,
136122
config: { theme: any }

packages/playground/content/custom-annotations.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ function adipiscing(...elit) {
1919
url="https://github.com/code-hike"
2020
title="Code Hike"
2121
/>
22+
<CH.Annotation as="label" focus="8">
23+
24+
**Hello**
25+
26+
</CH.Annotation>

0 commit comments

Comments
 (0)