@pho9ubenaa/remark-mask-text-beta
Version:
A remark plugin to mask text content with block characters
121 lines (120 loc) • 3.87 kB
TypeScript
/**
* Centralized AST type definitions for remark-mask-text plugin
*
* This module provides unified type definitions for Abstract Syntax Tree nodes
* used throughout the plugin. These types ensure type safety and consistency
* across all modules while providing clear interfaces for AST manipulation.
*
* Why (Business Logic Background):
* - Type safety: To enable compile-time error checking during AST node manipulation
* - Development efficiency: To leverage IDE auto-completion and refactoring features
* - Maintainability: To clarify impact scope during AST structure changes and prevent breaking changes
* - Compatibility: To maintain consistency with remark ecosystem type definitions
* - Extensibility: To facilitate adding new node types
*
* These types bridge the gap between the minimal types needed for development
* and the full types from @types/mdast and @types/unist packages.
*/
import { NODE_TYPES } from "./constants.js";
/**
* Base interface for all AST nodes
*
* Represents the fundamental structure of any node in the Abstract Syntax Tree.
* All other node types extend this base interface.
*/
export interface BaseNode {
/**
* The type identifier for the node, used for type discrimination
*/
type: string;
}
/**
* Parent node interface for nodes that can contain children
*
* Used for structural nodes like paragraphs, lists, and other containers
* that need to be traversed recursively during processing.
*/
export interface ParentNode extends BaseNode {
/**
* Array of child nodes contained within this parent
*/
children: ASTNode[];
}
/**
* Text node interface for nodes containing string content
*
* These nodes are the primary targets for mask processing as they contain
* the actual text content that may need to be masked.
*/
export interface TextNode extends BaseNode {
type: typeof NODE_TYPES.TEXT;
/**
* The string content of the text node
*/
value: string;
}
/**
* HTML node interface for raw HTML content
*
* Used to insert masked content as HTML to ensure proper rendering
* without additional processing by subsequent remark plugins.
*/
export interface HtmlNode extends BaseNode {
type: typeof NODE_TYPES.HTML;
/**
* The HTML content as a string
*/
value: string;
}
/**
* Link node interface for hyperlink elements
*
* Special handling may be required for links when they are surrounded
* by mask delimiters.
*/
export interface LinkNode extends ParentNode {
type: typeof NODE_TYPES.LINK;
/**
* The URL that the link points to
*/
url: string;
/**
* Optional title attribute for the link
*/
title?: string;
}
/**
* Union type representing all possible AST node types
*
* This discriminated union allows for type-safe pattern matching
* and ensures comprehensive coverage of all node types.
*/
export type ASTNode = TextNode | HtmlNode | LinkNode | ParentNode;
/**
* Type guard to check if a node is a parent node
*
* @param node - The node to check
* @returns true if the node has children property
*/
export declare const isParentNode: (node: unknown) => node is ParentNode;
/**
* Type guard to check if a node is a text node
*
* @param node - The node to check
* @returns true if the node is a text node with string value
*/
export declare const isTextNode: (node: unknown) => node is TextNode;
/**
* Type guard to check if a node is an HTML node
*
* @param node - The node to check
* @returns true if the node is an HTML node with string value
*/
export declare const isHtmlNode: (node: unknown) => node is HtmlNode;
/**
* Type guard to check if a node is a link node
*
* @param node - The node to check
* @returns true if the node is a link node with url property
*/
export declare const isLinkNode: (node: unknown) => node is LinkNode;