Home / Function/ findElementGroups() — astro Function Reference

findElementGroups() — astro Function Reference

Architecture documentation for the findElementGroups() function in rehype-optimize-static.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  101ea6c0_f1fc_8678_855e_10f17e6047e5["findElementGroups()"]
  36422914_2d7c_adeb_c777_76a007e3cb87["rehype-optimize-static.ts"]
  101ea6c0_f1fc_8678_855e_10f17e6047e5 -->|defined in| 36422914_2d7c_adeb_c777_76a007e3cb87
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384["rehypeOptimizeStatic()"]
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 -->|calls| 101ea6c0_f1fc_8678_855e_10f17e6047e5
  style 101ea6c0_f1fc_8678_855e_10f17e6047e5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/mdx/src/rehype-optimize-static.ts lines 187–241

function findElementGroups(
	allPossibleElements: Set<OptimizableNode>,
	elementMetadatas: WeakMap<OptimizableNode, ElementMetadata>,
	isNodeNonStatic: (node: Node) => boolean,
): ElementGroup[] {
	const elementGroups: ElementGroup[] = [];

	for (const el of allPossibleElements) {
		// Non-static nodes can't be grouped. It can only optimize its static children.
		if (isNodeNonStatic(el)) continue;

		// Get the metadata for the element node, this should always exist
		const metadata = elementMetadatas.get(el);
		if (!metadata) {
			throw new Error(
				'Internal MDX error: rehype-optimize-static should have metadata for element node',
			);
		}

		// For this element, iterate through the next siblings and add them to this array
		// if they are text nodes or elements that are in `allPossibleElements` (optimizable).
		// If one of the next siblings don't match the criteria, break the loop as others are no longer siblings.
		const groupableElements: Node[] = [el];
		for (let i = metadata.index + 1; i < metadata.parent.children.length; i++) {
			const node = metadata.parent.children[i];

			// If the node is non-static, we can't group it with the current element
			if (isNodeNonStatic(node)) break;

			if (node.type === 'element') {
				// This node is now (presumably) part of a group, remove it from `allPossibleElements`
				const existed = allPossibleElements.delete(node);
				// If this node didn't exist in `allPossibleElements`, it's likely that one of its children
				// are non-static, hence this node can also not be grouped. So we break out here.
				if (!existed) break;
			}

			groupableElements.push(node);
		}

		// If group elements are more than one, add them to the `elementGroups`.
		// Grouping is most effective if there's multiple elements in it.
		if (groupableElements.length > 1) {
			elementGroups.push({
				parent: metadata.parent,
				startIndex: metadata.index,
				children: groupableElements,
			});
			// The `el` is also now part of a group, remove it from `allPossibleElements`
			allPossibleElements.delete(el);
		}
	}

	return elementGroups;
}

Domain

Subdomains

Frequently Asked Questions

What does findElementGroups() do?
findElementGroups() is a function in the astro codebase, defined in packages/integrations/mdx/src/rehype-optimize-static.ts.
Where is findElementGroups() defined?
findElementGroups() is defined in packages/integrations/mdx/src/rehype-optimize-static.ts at line 187.
What calls findElementGroups()?
findElementGroups() is called by 1 function(s): rehypeOptimizeStatic.

Analyze Your Own Codebase

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

Try Supermodel Free