Home / File/ rehype-optimize-static.ts — astro Source File

rehype-optimize-static.ts — astro Source File

Architecture documentation for rehype-optimize-static.ts, a typescript file in the astro codebase. 6 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 6 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  36422914_2d7c_adeb_c777_76a007e3cb87["rehype-optimize-static.ts"]
  82f345a2_2234_43f1_c3c4_eea191acdca8["markdown-remark"]
  36422914_2d7c_adeb_c777_76a007e3cb87 --> 82f345a2_2234_43f1_c3c4_eea191acdca8
  1caf2ecb_c0ee_cf7a_f9de_d47a6cecaf57["estree-util-visit"]
  36422914_2d7c_adeb_c777_76a007e3cb87 --> 1caf2ecb_c0ee_cf7a_f9de_d47a6cecaf57
  f76e2597_e3e8_c502_d293_a666b44d49ce["hast"]
  36422914_2d7c_adeb_c777_76a007e3cb87 --> f76e2597_e3e8_c502_d293_a666b44d49ce
  dc038e99_c0c6_1c85_4e72_46d18b119c84["hast-util-to-html"]
  36422914_2d7c_adeb_c777_76a007e3cb87 --> dc038e99_c0c6_1c85_4e72_46d18b119c84
  350bf1c9_161e_6d98_d477_5ac7e492812d["mdast-util-mdx"]
  36422914_2d7c_adeb_c777_76a007e3cb87 --> 350bf1c9_161e_6d98_d477_5ac7e492812d
  03fa0bc9_b24b_2cf3_1b96_2104cbcd14a3["mdast-util-mdx-jsx"]
  36422914_2d7c_adeb_c777_76a007e3cb87 --> 03fa0bc9_b24b_2cf3_1b96_2104cbcd14a3
  style 36422914_2d7c_adeb_c777_76a007e3cb87 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { RehypePlugin } from '@astrojs/markdown-remark';
import { SKIP, visit } from 'estree-util-visit';
import type { Element, RootContent, RootContentMap } from 'hast';
import { toHtml } from 'hast-util-to-html';
import type {} from 'mdast-util-mdx';
import type { MdxJsxFlowElementHast, MdxJsxTextElementHast } from 'mdast-util-mdx-jsx';

// Alias as the main hast node
type Node = RootContent;
// Nodes that have the `children` property
type ParentNode = Element | MdxJsxFlowElementHast | MdxJsxTextElementHast;
// Nodes that can have its children optimized as a single HTML string
type OptimizableNode = Element | MdxJsxFlowElementHast | MdxJsxTextElementHast;

export interface OptimizeOptions {
	ignoreElementNames?: string[];
}

interface ElementMetadata {
	parent: ParentNode;
	index: number;
}

const exportConstComponentsRe = /export\s+const\s+components\s*=/;

/**
 * For MDX only, collapse static subtrees of the hast into `set:html`. Subtrees
 * do not include any MDX elements.
 *
 * This optimization reduces the JS output as more content are represented as a
 * string instead, which also reduces the AST size that Rollup holds in memory.
 */
export const rehypeOptimizeStatic: RehypePlugin<[OptimizeOptions?]> = (options) => {
	return (tree) => {
		// A set of non-static components to avoid collapsing when walking the tree
		// as they need to be preserved as JSX to be rendered dynamically.
		const ignoreElementNames = new Set<string>(options?.ignoreElementNames);

		// Find `export const components = { ... }` and get it's object's keys to be
		// populated into `ignoreElementNames`. This configuration is used to render
		// some HTML elements as custom components, and we also want to avoid collapsing them.
		for (const child of tree.children) {
			if (child.type === 'mdxjsEsm' && exportConstComponentsRe.test(child.value)) {
				const keys = getExportConstComponentObjectKeys(child);
				if (keys) {
					for (const key of keys) {
						ignoreElementNames.add(key);
					}
				}
				break;
			}
		}

		// All possible elements that could be the root of a subtree
		const allPossibleElements = new Set<OptimizableNode>();
		// The current collapsible element stack while traversing the tree
		const elementStack: Node[] = [];
		// Metadata used by `findElementGroups` later
		const elementMetadatas = new WeakMap<OptimizableNode, ElementMetadata>();

// ... (241 more lines)

Domain

Subdomains

Dependencies

  • estree-util-visit
  • hast
  • hast-util-to-html
  • markdown-remark
  • mdast-util-mdx
  • mdast-util-mdx-jsx

Frequently Asked Questions

What does rehype-optimize-static.ts do?
rehype-optimize-static.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in rehype-optimize-static.ts?
rehype-optimize-static.ts defines 5 function(s): findElementGroups, getExportConstComponentObjectKeys, isMdxComponentNode, rehypeOptimizeStatic, simplifyPlainMdxComponentNode.
What does rehype-optimize-static.ts depend on?
rehype-optimize-static.ts imports 6 module(s): estree-util-visit, hast, hast-util-to-html, markdown-remark, mdast-util-mdx, mdast-util-mdx-jsx.
Where is rehype-optimize-static.ts in the architecture?
rehype-optimize-static.ts is located at packages/integrations/mdx/src/rehype-optimize-static.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/integrations/mdx/src).

Analyze Your Own Codebase

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

Try Supermodel Free