Home / Function/ rehypeAnalyzeAstroMetadata() — astro Function Reference

rehypeAnalyzeAstroMetadata() — astro Function Reference

Architecture documentation for the rehypeAnalyzeAstroMetadata() function in rehype.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d["rehypeAnalyzeAstroMetadata()"]
  89da4403_ab4a_62fa_64f0_d53bd0b3aee8["rehype.ts"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|defined in| 89da4403_ab4a_62fa_64f0_d53bd0b3aee8
  2026ae0c_4235_962c_702f_19eb6663c65a["parseImports()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| 2026ae0c_4235_962c_702f_19eb6663c65a
  c9e47d6e_f1fe_bc09_ecda_94db7203141d["isComponent()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| c9e47d6e_f1fe_bc09_ecda_94db7203141d
  30bb266c_6336_f21e_2a8b_19a031810743["hasClientDirective()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| 30bb266c_6336_f21e_2a8b_19a031810743
  f27c758f_c47e_e187_586b_bb0dc981b9f7["hasServerDeferDirective()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| f27c758f_c47e_e187_586b_bb0dc981b9f7
  a078dbe3_ce8d_013a_b061_8bceac48c778["findMatchingImport()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| a078dbe3_ce8d_013a_b061_8bceac48c778
  6969d9a3_74d2_c053_67bd_9a8d8bb78253["hasClientOnlyDirective()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| 6969d9a3_74d2_c053_67bd_9a8d8bb78253
  4e3bfe6a_df90_e8eb_8780_b01939be9909["addClientOnlyMetadata()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| 4e3bfe6a_df90_e8eb_8780_b01939be9909
  ece91fc6_f6dc_b8c0_28bc_a93eb3cc59e9["addClientMetadata()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| ece91fc6_f6dc_b8c0_28bc_a93eb3cc59e9
  604614d7_2c85_5d45_05c7_7a02b9064597["addServerDeferMetadata()"]
  d17ed63a_5ec6_bc29_96d7_b3145cec7d5d -->|calls| 604614d7_2c85_5d45_05c7_7a02b9064597
  style d17ed63a_5ec6_bc29_96d7_b3145cec7d5d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/jsx/rehype.ts lines 19–101

export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
	return (tree, file) => {
		// Initial metadata for this MDX file, it will be mutated as we traverse the tree
		const metadata = createDefaultAstroMetadata();

		// Parse imports in this file. This is used to match components with their import source
		const imports = parseImports(tree.children);

		visit(tree, (node) => {
			if (node.type !== 'mdxJsxFlowElement' && node.type !== 'mdxJsxTextElement') return;

			const tagName = node.name;
			if (
				!tagName ||
				!isComponent(tagName) ||
				!(hasClientDirective(node) || hasServerDeferDirective(node))
			)
				return;

			// From this point onwards, `node` is confirmed to be an island component

			// Match this component with its import source
			const matchedImport = findMatchingImport(tagName, imports);
			if (!matchedImport) {
				throw new AstroError({
					...AstroErrorData.NoMatchingImport,
					message: AstroErrorData.NoMatchingImport.message(node.name!),
				});
			}

			// If this is an Astro component, that means the `client:` directive is misused as it doesn't
			// work on Astro components as it's server-side only. Warn the user about this.
			if (matchedImport.path.endsWith('.astro')) {
				const clientAttribute = node.attributes.find(
					(attr) => attr.type === 'mdxJsxAttribute' && attr.name.startsWith('client:'),
				) as MdxJsxAttribute | undefined;
				if (clientAttribute) {
					console.warn(
						`You are attempting to render <${node.name!} ${
							clientAttribute.name
						} />, but ${node.name!} is an Astro component. Astro components do not render in the client and should not have a hydration directive. Please use a framework component for client rendering.`,
					);
				}
			}

			const resolvedPath = resolvePath(matchedImport.path, file.path);

			if (hasClientOnlyDirective(node)) {
				// Add this component to the metadata
				metadata.clientOnlyComponents.push({
					exportName: matchedImport.name,
					localName: '',
					specifier: tagName,
					resolvedPath,
				});
				// Mutate node with additional island attributes
				addClientOnlyMetadata(node, matchedImport, resolvedPath);
			} else if (hasClientDirective(node)) {
				// Add this component to the metadata
				metadata.hydratedComponents.push({
					exportName: '*',
					localName: '',
					specifier: tagName,
					resolvedPath,
				});
				// Mutate node with additional island attributes
				addClientMetadata(node, matchedImport, resolvedPath);
			} else if (hasServerDeferDirective(node)) {
				metadata.serverComponents.push({
					exportName: matchedImport.name,
					localName: tagName,
					specifier: matchedImport.path,
					resolvedPath,
				});
				// Mutate node with additional island attributes
				addServerDeferMetadata(node, matchedImport, resolvedPath);
			}
		});

		// Attach final metadata here, which can later be retrieved by `getAstroMetadata`
		file.data.__astroMetadata = metadata;

Domain

Subdomains

Frequently Asked Questions

What does rehypeAnalyzeAstroMetadata() do?
rehypeAnalyzeAstroMetadata() is a function in the astro codebase, defined in packages/astro/src/jsx/rehype.ts.
Where is rehypeAnalyzeAstroMetadata() defined?
rehypeAnalyzeAstroMetadata() is defined in packages/astro/src/jsx/rehype.ts at line 19.
What does rehypeAnalyzeAstroMetadata() call?
rehypeAnalyzeAstroMetadata() calls 9 function(s): addClientMetadata, addClientOnlyMetadata, addServerDeferMetadata, findMatchingImport, hasClientDirective, hasClientOnlyDirective, hasServerDeferDirective, isComponent, and 1 more.

Analyze Your Own Codebase

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

Try Supermodel Free