1 import {
2 CloudAnnouncement,
3 FrontMatter,
4 ObjectExpression,
5 ToCItem,
6 UnistFunctionDeclarationNode,
7 UnistNode,
8 UnistProgram,
9 UnistReturnStatementNode,
10 } from "types"
11 import type { Transformer } from "unified"
12 import { getToc, getTocJSX } from "./utils/toc.js"
13
14 type DataToc = Record<string, unknown>[]
15
16 type Options = {
17 isRemoteMdx?: boolean
18 announcements?: CloudAnnouncement[]
19 mode?: "development" | "production"
20 }
21
22 export function recmaInjectMdxDataPlugin(options?: Options): Transformer {
23 const isRemoteMdx = options?.isRemoteMdx || false
24 const mode = options?.mode || "development"
25 return async (tree, file) => {
26 const frontmatter: FrontMatter = file.data.matter || {}
27 const tocMaxDepth = frontmatter.toc_max_depth || 4
28 let dataToc = (file.data.toc || []) as DataToc
29 if (dataToc.length > 0 && dataToc[0].depth === 1) {
30 dataToc = (dataToc[0].children || []) as DataToc
31 }
32 const toc: ToCItem[] = frontmatter.generate_toc
33 ? []
34 : getToc(dataToc, tocMaxDepth)
35
36 const program = tree as UnistProgram
37 for (const node of program.body) {
38 if (node.type === "FunctionDeclaration") {
39 const fnNode = node as UnistFunctionDeclarationNode
40 const returnStatement = fnNode.body.body.find(
41 (n) => n.type === "ReturnStatement"
42 )
43
44 if (!returnStatement) {
45 continue
46 }
47
48 const returnChildren = getReturnChildren(
49 returnStatement as UnistReturnStatementNode
50 )
51
52 if (isRemoteMdx) {
53 handleRemoteMdx({
54 returnChildren,
55 frontmatter,
56 toc,
57 mode,
58 })
59 } else {
60 handleDefaultMdx({
61 program,
62 returnChildren,
63 frontmatter,
64 toc,
65 })
66 }
67
68 return
69 }
70 }
71 }
72 }
73
74 function handleDefaultMdx({
75 program,
76 returnChildren,
77 frontmatter,
78 toc,
79 }: {
80 program: UnistProgram
81 returnChildren: UnistNode[]
82 frontmatter: FrontMatter
83 toc: ToCItem[]
84 }) {
85 program.body.unshift({
86 type: "ImportDeclaration",
87 source: { type: "Literal", value: "docs-ui" },
88 specifiers: [
89 {
90 type: "ImportSpecifier",
91 imported: { type: "Identifier", name: "InjectedMDXData" },
92 local: { type: "Identifier", name: "InjectedMDXData" },
93 },
94 ],
95 })
96
97 returnChildren.push({
98 type: "JSXElement",
99 openingElement: {
100 type: "JSXOpeningElement",
101 name: {
102 type: "JSXIdentifier",
103 name: "InjectedMDXData",
104 },
105 attributes: [
106 {
107 type: "JSXAttribute",
108 name: {
109 type: "JSXIdentifier",
110 name: "frontmatter",
111 },
112 value: {
113 type: "JSXExpressionContainer",
114 expression: {
115 type: "ObjectExpression",
116 properties: Object.entries(frontmatter).map(([key, value]) => ({
117 type: "Property",
118 key: {
119 type: "Identifier",
120 name: key,
121 },
122 value: {
123 type: "Literal",
124 value: value,
125 raw: JSON.stringify(value),
126 },
127 kind: "init",
128 computed: false,
129 method: false,
130 shorthand: false,
131 })),
132 },
133 },
134 },
135 {
136 type: "JSXAttribute",
137 name: {
138 type: "JSXIdentifier",
139 name: "toc",
140 },
141 value: {
142 type: "JSXExpressionContainer",
143 expression: {
144 type: "ArrayExpression",
145 elements: getTocJSX(toc),
146 },
147 },
148 },
149 ],
150 selfClosing: true,
151 },
152 } as any)
153 }
154
155 function handleRemoteMdx({
156 returnChildren,
157 frontmatter,
158 toc,
159 mode,
160 }: {
161 returnChildren: UnistNode[]
162 frontmatter: FrontMatter
163 toc: ToCItem[]
164 mode: "development" | "production"
165 }) {
166 const functionName = mode === "development" ? "_jsxDEV" : "_jsx"
167
168 returnChildren.push({
169 type: "Literal",
170 value: "\n",
171 })
172
173 returnChildren.push({
174 type: "CallExpression",
175 callee: {
176 type: "Identifier",
177 name: functionName,
178 },
179 arguments: [
180 {
181 type: "MemberExpression",
182 object: {
183 type: "Identifier",
184 name: "_components",
185 },
186 property: {
187 type: "Identifier",
188 name: "InjectedMDXData",
189 },
190 computed: false,
191 optional: false,
192 shorthand: false,
193 },
194 {
195 type: "ObjectExpression",
196 properties: [
197 {
198 type: "Property",
199 key: { type: "Identifier", name: "frontmatter" },
200 value: {
201 type: "ObjectExpression",
202 properties: Object.entries(frontmatter).map(([key, value]) => ({
203 type: "Property",
204 key: {
205 type: "Identifier",
206 name: key,
207 },
208 value: {
209 type: "Literal",
210 value: value,
211 raw: JSON.stringify(value),
212 },
213 kind: "init",
214 computed: false,
215 optional: false,
216 shorthand: false,
217 })),
218 },
219 kind: "init",
220 computed: false,
221 optional: false,
222 shorthand: false,
223 },
224 {
225 type: "Property",
226 key: { type: "Identifier", name: "toc" },
227 value: {
228 type: "ArrayExpression",
229 elements: getTocJSX(toc),
230 },
231 kind: "init",
232 computed: false,
233 optional: false,
234 shorthand: false,
235 },
236 ],
237 },
238 ],
239 } as any)
240 }
241
242 function getReturnChildren(node: UnistReturnStatementNode): UnistNode[] {
243 const rootJSX = node.argument
244
245 if (rootJSX.type === "JSXFragment") {
246 return rootJSX.children
247 } else if (rootJSX.type === "JSXElement") {
248 return []
249 }
250
251 const props = rootJSX.arguments.find(
252 (arg) => arg.type === "ObjectExpression"
253 ) as ObjectExpression | undefined
254
255 if (!props) {
256 return []
257 }
258
259 const childrenProp = props.properties.find(
260 (prop) => prop.key.name === "children"
261 )
262
263 if (!childrenProp || childrenProp.value.type !== "ArrayExpression") {
264 return []
265 }
266
267 return childrenProp.value.elements as UnistNode[]
268 }