Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

fix: update rehype component setting

Dillion Vermaa6f09ed450c1
diff --git a/apps/www/lib/rehype-component.ts b/apps/www/lib/rehype-component.ts
index 9622f88a..3913346d 100644
--- a/apps/www/lib/rehype-component.ts
+++ b/apps/www/lib/rehype-component.ts
@@ -1,23 +1,21 @@
import fs from "fs";
import path from "path";
+import { UnistNode, UnistTree } from "types/unist";
import { u } from "unist-builder";
import { visit } from "unist-util-visit";
-import { UnistNode, UnistTree } from "@/types/unist";
-import Registry from "@/public/registry.json";
-export const styles = [
- {
- name: "default",
- label: "Default",
- },
-] as const;
-
-export type Style = (typeof styles)[number];
+import { Index } from "@/__registry__";
export function rehypeComponent() {
return async (tree: UnistTree) => {
visit(tree, (node: UnistNode) => {
- const { value: srcPath } = getNodeAttributeByName(node, "src") || {};
+ // src prop overrides both name and fileName.
+ const { value: srcPath } =
+ (getNodeAttributeByName(node, "src") as {
+ name: string;
+ value?: string;
+ type?: string;
+ }) || {};
if (node.name === "ComponentSource") {
const name = getNodeAttributeByName(node, "name")?.value as string;
@@ -33,35 +31,30 @@ export function rehypeComponent() {
let src: string;
if (srcPath) {
- src = srcPath as string;
+ src = path.join(process.cwd(), srcPath);
} else {
- const component = Registry.items.find((item) => item.name === name);
-
- if (!component) {
- return null;
- }
-
+ const component = Index[name];
src = fileName
- ? component.files.find((file) => {
- return (
- file.path.endsWith(`${fileName}.tsx`) ||
- file.path.endsWith(`${fileName}.ts`)
- );
- })?.path || component.files[0].path
- : component.files[0].path;
+ ? component.files.find((file: unknown) => {
+ if (typeof file === "string") {
+ return (
+ file.endsWith(`${fileName}.tsx`) ||
+ file.endsWith(`${fileName}.ts`)
+ );
+ }
+ return false;
+ }) || component.files[0]?.path
+ : component.files[0]?.path;
}
// Read the source file.
- const filePath = path.join(process.cwd(), src);
+ const filePath = src;
let source = fs.readFileSync(filePath, "utf8");
- // Handle direct magicui imports
- source = source.replaceAll(
- "@/registry/magicui/",
- "@/components/magicui/",
- );
-
- // Replace default exports
+ // Replace imports.
+ // TODO: Use @swc/core and a visitor to replace this.
+ // For now a simple regex should do.
+ source = source.replaceAll(`@/registry/${name}/`, "@/components/");
source = source.replaceAll("export default", "export");
// Add code as children so that rehype can take over at build time.
@@ -70,16 +63,21 @@ export function rehypeComponent() {
tagName: "pre",
properties: {
__src__: src,
+ __style__: name,
},
+ attributes: [
+ {
+ name: "styleName",
+ type: "mdxJsxAttribute",
+ value: name,
+ },
+ ],
children: [
u("element", {
tagName: "code",
properties: {
className: ["language-tsx"],
},
- data: {
- meta: `event="copy_source_code"`,
- },
children: [
{
type: "text",
@@ -95,7 +93,7 @@ export function rehypeComponent() {
}
}
- if (node.name === "ComponentPreview" || node.name === "BlockPreview") {
+ if (node.name === "ComponentPreview") {
const name = getNodeAttributeByName(node, "name")?.value as string;
if (!name) {
@@ -103,25 +101,17 @@ export function rehypeComponent() {
}
try {
- const component = Registry.items.find((item) => item.name === name);
-
- if (!component) {
- return null;
- }
-
- const src = component.files[0].path;
+ const component = Index[name];
+ const src = component.files[0]?.path;
// Read the source file.
- const filePath = path.join(process.cwd(), src);
+ const filePath = src;
let source = fs.readFileSync(filePath, "utf8");
- // Handle direct magicui imports
- source = source.replaceAll(
- "@/registry/magicui/",
- "@/components/magicui/",
- );
-
- // Replace default exports
+ // Replace imports.
+ // TODO: Use @swc/core and a visitor to replace this.
+ // For now a simple regex should do.
+ source = source.replaceAll(`@/registry/${name}/`, "@/components/");
source = source.replaceAll("export default", "export");
// Add code as children so that rehype can take over at build time.
@@ -137,9 +127,6 @@ export function rehypeComponent() {
properties: {
className: ["language-tsx"],
},
- data: {
- meta: `event="copy_usage_code"`,
- },
children: [
{
type: "text",
@@ -161,17 +148,3 @@ export function rehypeComponent() {
function getNodeAttributeByName(node: UnistNode, name: string) {
return node.attributes?.find((attribute) => attribute.name === name);
}
-
-function getComponentSourceFileContent(node: UnistNode) {
- const src = getNodeAttributeByName(node, "src")?.value as string;
-
- if (!src) {
- return null;
- }
-
- // Read the source file.
- const filePath = path.join(process.cwd(), src);
- const source = fs.readFileSync(filePath, "utf8");
-
- return source;
-}