@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
180 lines (179 loc) • 6.8 kB
TypeScript
/**
* Converts LaTeX bracket delimiters to dollar sign delimiters.
* Converts \[...\] to $$...$$ and \(...\) to $...$
* Preserves code blocks during the conversion.
*
* @param text The input string containing LaTeX expressions
* @returns The string with LaTeX bracket delimiters converted to dollar sign delimiters
*/
export declare function convertLatexDelimiters(text: string): string;
/**
* Escapes mhchem commands in LaTeX expressions to ensure proper rendering.
*
* @param text The input string containing LaTeX expressions with mhchem commands
* @returns The string with escaped mhchem commands
*/
export declare function escapeMhchemCommands(text: string): string;
/**
* Escapes pipe characters within LaTeX expressions to prevent them from being interpreted
* as table column separators in markdown tables.
*
* @param text The input string containing LaTeX expressions
* @returns The string with pipe characters escaped in LaTeX expressions
*/
export declare function escapeLatexPipes(text: string): string;
/**
* Escapes underscores within \text{...} commands in LaTeX expressions
* that are not already escaped.
* For example, \text{node_domain} becomes \text{node\_domain},
* but \text{node\_domain} remains \text{node\_domain}.
*
* @param text The input string potentially containing LaTeX expressions
* @returns The string with unescaped underscores escaped within \text{...} commands
*/
export declare function escapeTextUnderscores(text: string): string;
/**
* Escapes dollar signs that appear to be currency symbols to prevent them from being
* interpreted as LaTeX math delimiters.
*
* This function identifies currency patterns such as:
* - $20, $100, $1,000
* - $20-50, $100+
* - Patterns within markdown tables
*
* @param text The input string containing potential currency symbols
* @returns The string with currency dollar signs escaped
*/
export declare function escapeCurrencyDollars(text: string): string;
/**
* Checks if the last LaTeX formula in the text is renderable.
* Only validates the formula after the last $$ if there's an odd number of $$.
*
* @param text The input string containing LaTeX formulas
* @returns True if the last formula is renderable or if there's no incomplete formula
*/
export declare const isLastFormulaRenderable: (text: string) => boolean;
/**
* Fixes common LaTeX syntax errors automatically
* - Balances unmatched braces
* - Balances \left and \right delimiters
*
* @param text The input string containing LaTeX expressions
* @returns The string with fixed LaTeX expressions
*/
export declare function fixCommonLaTeXErrors(text: string): string;
/**
* Normalizes whitespace in LaTeX expressions
* - Removes extra spaces around $ delimiters
* - Normalizes multiple spaces to single space inside formulas
*
* @param text The input string containing LaTeX expressions
* @returns The string with normalized whitespace
*/
export declare function normalizeLatexSpacing(text: string): string;
/**
* Validates all LaTeX expressions in the text
* Returns detailed information about validation results
*
* @param text The input string containing LaTeX expressions
* @returns Validation results with errors if any
*/
export declare function validateLatexExpressions(text: string): {
errors: Array<{
formula: string;
message: string;
position: number;
type: 'display' | 'inline';
}>;
totalExpressions: number;
valid: boolean;
};
/**
* Handles CJK (Chinese, Japanese, Korean) characters mixed with LaTeX
* Optionally adds spaces between CJK characters and LaTeX expressions for better rendering
*
* @param text The input string
* @param addSpaces Whether to add spaces between CJK and LaTeX (default: false)
* @returns The processed string
*/
export declare function handleCJKWithLatex(text: string, addSpaces?: boolean): string;
export interface AdvancedPreprocessOptions {
/** Add spaces between CJK and LaTeX (default: false, requires handleCJK: true) */
addCJKSpaces?: boolean;
/** Convert bracket notation \[...\] to $$...$$ (default: true) */
convertBrackets?: boolean;
/** Enable currency escaping (default: true) */
escapeCurrency?: boolean;
/** Escape mhchem commands (default: true) */
escapeMhchem?: boolean;
/** Escape pipe symbols in LaTeX (default: true) */
escapePipes?: boolean;
/** Escape underscores in \text{} (default: true) */
escapeUnderscores?: boolean;
/** Automatically fix common LaTeX errors (default: false) */
fixErrors?: boolean;
/** Handle CJK characters (default: false) */
handleCJK?: boolean;
/** Normalize whitespace (default: false) */
normalizeSpacing?: boolean;
/** Throw error on validation failure (default: false, requires validate: true) */
throwOnValidationError?: boolean;
/** Validate LaTeX syntax (default: false) */
validate?: boolean;
}
/**
* Comprehensive LaTeX preprocessing with configurable options
*
* This is the main preprocessing function that handles:
* - Currency symbol escaping (e.g., $20 → \$20)
* - LaTeX delimiter conversion (\[...\] → $$...$$)
* - Special character escaping (pipes, underscores, mhchem)
* - Optional error fixing and validation
* - Optional CJK character handling
*
* @param text The input string containing LaTeX and Markdown
* @param options Configuration options for fine-grained control
* @returns The preprocessed string
*
* @example
* ```ts
* // Default behavior (same as old preprocessLaTeX)
* preprocessLaTeX('向量$90^\\circ$,非 $0^\\circ$ 和 $180^\\circ$')
*
* // With custom options
* preprocessLaTeX(text, {
* fixErrors: true,
* validate: true,
* handleCJK: true
* })
* ```
*/
export declare function preprocessLaTeX(text: string, options?: AdvancedPreprocessOptions): string;
/**
* Strict preprocessing mode - enables all safety features and validations
* Use this when you want maximum correctness and are willing to accept the performance cost
*
* @param text The input string
* @returns The preprocessed string with all features enabled
*
* @example
* ```ts
* const processed = preprocessLaTeXStrict(userInput)
* // Enables: error fixing, validation, CJK handling, space normalization
* ```
*/
export declare function preprocessLaTeXStrict(text: string): string;
/**
* Minimal preprocessing mode - only essential operations
* Use this for better performance when you control the input
*
* @param text The input string
* @returns The preprocessed string with minimal processing
*
* @example
* ```ts
* const processed = preprocessLaTeXMinimal(trustedInput)
* // Only escapes currency and converts brackets
* ```
*/
export declare function preprocessLaTeXMinimal(text: string): string;