@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
154 lines • 6.35 kB
text/typescript
/**
* HAST type detection utilities for analyzing syntax-highlighted TypeScript types.
*
* These utilities work on already-highlighted HAST nodes to detect type patterns
* (unions, functions, objects) and derive shortType/detailedType variants.
*/
import type { Root as HastRoot, Element } from 'hast';
import { getHastTextContent, getShallowTextContent } from "../hastUtils/index.mjs";
export { getHastTextContent, getShallowTextContent };
/**
* Checks if a HAST element has a specific CSS class.
* Handles both string and array class representations.
*/
export declare function hasClass(element: Element, className: string): boolean;
/**
* Checks if a HAST element is a span with class `line`.
*/
export declare function isLineSpan(node: {
type: string;
tagName?: string;
properties?: Record<string, unknown>;
}): node is Element;
/**
* Checks if a HAST element is a comment span (pl-c).
*/
export declare function isCommentSpan(element: Element): boolean;
/**
* Checks if a HAST element is a property-name span (pl-v).
*/
export declare function isPropertyNameSpan(element: Element): boolean;
/**
* Checks if a HAST element is an entity-name span (pl-en).
*/
export declare function isEntityNameSpan(element: Element): boolean;
/**
* Checks if a HAST element is a keyword span (pl-k).
*/
export declare function isKeywordSpan(element: Element): boolean;
/**
* Checks if a HAST element is a string literal span (pl-s).
*/
export declare function isStringLiteralSpan(element: Element): boolean;
/**
* Checks if a HAST element is a constant/primitive span (pl-c1).
*/
export declare function isConstantSpan(element: Element): boolean;
/**
* Checks if a HAST tree contains a top-level pipe operator (|) indicating a union type.
* Top-level means not nested inside parentheses or braces.
*
* Starry Night highlights `|` with the `pl-k` (keyword) class.
*/
export declare function isUnionHast(hast: HastRoot): boolean;
/**
* Checks if a HAST tree represents a function type.
*
* Function types contain `=>` which Starry Night highlights with `pl-k` (keyword) class.
*/
export declare function isFunctionHast(hast: HastRoot): boolean;
/**
* Checks if a HAST tree represents an object type.
*
* Object types contain `{` and `}` braces at the top level.
*/
export declare function isObjectHast(hast: HastRoot): boolean;
/**
* Checks if a HAST tree represents an array type.
*
* Array types end with `[]`.
*/
export declare function isArrayHast(hast: HastRoot): boolean;
/**
* Checks if a HAST tree represents a tuple type.
*
* Tuple types start with `[` and end with `]` but are not arrays.
*/
export declare function isTupleHast(hast: HastRoot): boolean;
/**
* Derives the short type string from a highlighted HAST based on its structure.
*
* This function analyzes the HAST structure to determine what simplified label
* to show (e.g., "Union", "function") without needing the original plain text.
*
* @param name - The property name (used for special cases like className, render)
* @param hast - The syntax-highlighted HAST to analyze
* @returns A short type string, or undefined if no shortening is needed
*/
export declare function getShortTypeFromHast(name: string, hast: HastRoot): string | undefined;
/**
* Determines whether a type should have a detailed expanded view based on its HAST structure.
*
* @param name - The property name (used for special cases)
* @param hast - The syntax-highlighted HAST to analyze
* @returns true if the type should have a detailed view
*/
export declare function shouldShowDetailedTypeFromHast(name: string, hast: HastRoot): boolean;
/**
* Result of collecting type references from HAST.
*/
export interface TypeReference {
/** The full dotted name (e.g., "Slider.Root.State" or "DirectionProvider") */
name: string;
/** Start index in parent's children array */
startIndex: number;
/** End index in parent's children array (exclusive) */
endIndex: number;
/** The parent element containing this reference */
parent: Element;
}
/**
* Collects all type references (pl-en spans) from a HAST tree.
*
* This function walks the HAST and identifies type references that could be
* replaced with their definitions. It handles both:
* - Single identifiers (e.g., `DirectionProvider`)
* - Dotted identifiers (e.g., `Slider.Root.State`)
*
* @param hast - The HAST root to analyze
* @returns Array of type references found
*/
export declare function collectTypeReferences(hast: HastRoot): TypeReference[];
/**
* Replaces type references in a HAST with their expanded definitions.
*
* @param hast - The HAST to modify (will be cloned)
* @param highlightedExports - Map of export names to their highlighted HAST definitions
* @returns A new HAST with references replaced, or the original if no replacements needed
*/
export declare function replaceTypeReferences(hast: HastRoot, highlightedExports: Record<string, HastRoot>): HastRoot;
/**
* A JSON-serialized wrapper around a HastRoot. Defers tree allocation to
* render time: V8 stores only a string, and `JSON.parse` at render time
* provides both deserialization and a free deep clone.
*/
export interface SerializedHastRoot {
hastJson: string;
}
/**
* A DEFLATE-compressed (with shared dictionary), base64-encoded wrapper around a HastRoot.
* Smaller than JSON for transport; decompressed with the matching dictionary at render time.
*/
export interface SerializedHastCompressed {
hastCompressed: string;
}
/** Controls the output format of HAST fields in type metadata. */
export type TypesOutputFormat = 'hast' | 'hastJson' | 'hastCompressed';
/** Converts a HastRoot to a JSON-serialized wrapper. */
export declare function serializeHastRoot(hast: HastRoot): SerializedHastRoot;
/** Converts a HastRoot to a dictionary-compressed, base64-encoded wrapper. */
export declare function compressHastRoot(hast: HastRoot, textContent?: string): SerializedHastCompressed;
/** Returns the appropriate serializer function for the given output format. */
export declare function resolveSerializer(output: TypesOutputFormat, textContent?: string): (hast: HastRoot) => HastRoot | SerializedHastRoot | SerializedHastCompressed;
/** No-op passthrough — avoids allocating a fresh closure on every call. */
export declare function hastIdentity(hast: HastRoot): HastRoot;