64 lines
		
	
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			64 lines
		
	
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
|   | import { visit } from "estree-util-visit"; | ||
|  | import { toHtml } from "hast-util-to-html"; | ||
|  | const exportConstComponentsRe = /export\s+const\s+components\s*=/; | ||
|  | function rehypeOptimizeStatic(options) { | ||
|  |   return (tree) => { | ||
|  |     var _a, _b, _c, _d, _e, _f; | ||
|  |     const customComponentNames = new Set(options == null ? void 0 : options.customComponentNames); | ||
|  |     for (const child of tree.children) { | ||
|  |       if (child.type === "mdxjsEsm" && exportConstComponentsRe.test(child.value)) { | ||
|  |         const objectPropertyNodes = (_d = (_c = (_b = (_a = child.data.estree.body[0]) == null ? void 0 : _a.declarations) == null ? void 0 : _b[0]) == null ? void 0 : _c.init) == null ? void 0 : _d.properties; | ||
|  |         if (objectPropertyNodes) { | ||
|  |           for (const objectPropertyNode of objectPropertyNodes) { | ||
|  |             const componentName = ((_e = objectPropertyNode.key) == null ? void 0 : _e.name) ?? ((_f = objectPropertyNode.key) == null ? void 0 : _f.value); | ||
|  |             if (componentName) { | ||
|  |               customComponentNames.add(componentName); | ||
|  |             } | ||
|  |           } | ||
|  |         } | ||
|  |       } | ||
|  |     } | ||
|  |     const allPossibleElements = /* @__PURE__ */ new Set(); | ||
|  |     const elementStack = []; | ||
|  |     visit(tree, { | ||
|  |       enter(node) { | ||
|  |         const isCustomComponent = node.tagName && customComponentNames.has(node.tagName); | ||
|  |         if (node.type.startsWith("mdx") || isCustomComponent) { | ||
|  |           for (const el of elementStack) { | ||
|  |             allPossibleElements.delete(el); | ||
|  |           } | ||
|  |           elementStack.length = 0; | ||
|  |         } | ||
|  |         if (node.type === "element" || node.type === "mdxJsxFlowElement") { | ||
|  |           elementStack.push(node); | ||
|  |           allPossibleElements.add(node); | ||
|  |         } | ||
|  |       }, | ||
|  |       leave(node, _, __, parents) { | ||
|  |         if (node.type === "element" || node.type === "mdxJsxFlowElement") { | ||
|  |           elementStack.pop(); | ||
|  |           const parent = parents[parents.length - 1]; | ||
|  |           if (allPossibleElements.has(parent)) { | ||
|  |             allPossibleElements.delete(node); | ||
|  |           } | ||
|  |         } | ||
|  |       } | ||
|  |     }); | ||
|  |     for (const el of allPossibleElements) { | ||
|  |       if (el.type === "mdxJsxFlowElement") { | ||
|  |         el.attributes.push({ | ||
|  |           type: "mdxJsxAttribute", | ||
|  |           name: "set:html", | ||
|  |           value: toHtml(el.children) | ||
|  |         }); | ||
|  |       } else { | ||
|  |         el.properties["set:html"] = toHtml(el.children); | ||
|  |       } | ||
|  |       el.children = []; | ||
|  |     } | ||
|  |   }; | ||
|  | } | ||
|  | export { | ||
|  |   rehypeOptimizeStatic | ||
|  | }; |