Home / Function/ rehypeComponent() — ui Function Reference

rehypeComponent() — ui Function Reference

Architecture documentation for the rehypeComponent() function in rehype-component.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  c38c6bfc_2ef9_6855_c303_4188603dc06e["rehypeComponent()"]
  a572535f_b25f_7501_dc60_c7a2d320a739["rehype-component.ts"]
  c38c6bfc_2ef9_6855_c303_4188603dc06e -->|defined in| a572535f_b25f_7501_dc60_c7a2d320a739
  b0a57047_43fa_bc18_c737_e1385bb1e84c["getNodeAttributeByName()"]
  c38c6bfc_2ef9_6855_c303_4188603dc06e -->|calls| b0a57047_43fa_bc18_c737_e1385bb1e84c
  fcaaaacb_8d26_81d2_e1a8_d8baeeac2743["getComponentSourceFileContent()"]
  c38c6bfc_2ef9_6855_c303_4188603dc06e -->|calls| fcaaaacb_8d26_81d2_e1a8_d8baeeac2743
  style c38c6bfc_2ef9_6855_c303_4188603dc06e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

deprecated/www/lib/rehype-component.ts lines 10–264

export function rehypeComponent() {
  return async (tree: UnistTree) => {
    visit(tree, (node: UnistNode) => {
      // 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
        const fileName = getNodeAttributeByName(node, "fileName")?.value as
          | string
          | undefined

        if (!name && !srcPath) {
          return null
        }

        try {
          for (const style of styles) {
            let src: string

            if (srcPath) {
              src = path.join(process.cwd(), srcPath)
            } else {
              const component = Index[style.name][name]
              src = fileName
                ? 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 = src
            let source = fs.readFileSync(filePath, "utf8")

            // Replace imports.
            // TODO: Use @swc/core and a visitor to replace this.
            // For now a simple regex should do.
            source = source.replaceAll(
              `@/registry/${style.name}/`,
              "@/components/"
            )
            source = source.replaceAll("export default", "export")

            // Add code as children so that rehype can take over at build time.
            node.children?.push(
              u("element", {
                tagName: "pre",
                properties: {
                  __src__: src,
                  __style__: style.name,
                },
                attributes: [
                  {
                    name: "styleName",
                    type: "mdxJsxAttribute",
                    value: style.name,
                  },
                ],
                children: [
                  u("element", {
                    tagName: "code",
                    properties: {
                      className: ["language-tsx"],
                    },
                    children: [
                      {
                        type: "text",
                        value: source,
                      },

Subdomains

Frequently Asked Questions

What does rehypeComponent() do?
rehypeComponent() is a function in the ui codebase, defined in deprecated/www/lib/rehype-component.ts.
Where is rehypeComponent() defined?
rehypeComponent() is defined in deprecated/www/lib/rehype-component.ts at line 10.
What does rehypeComponent() call?
rehypeComponent() calls 2 function(s): getComponentSourceFileContent, getNodeAttributeByName.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free