UNPKG

legal-markdown-js

Version:

Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version

113 lines 4.26 kB
/** * Enhanced Remark plugin for Legal Markdown cross-references using custom AST nodes * * This plugin creates custom 'reference' AST nodes for |key| patterns, enabling: * - Single-pass AST processing for better performance * - More extensible architecture with custom node types * - Support for additional attributes (type, format) on references * - Independent from complex regex patterns * - Better integration with other remark plugins * * Architecture: * 1. Parse phase: Convert |key| patterns into custom 'reference' nodes * 2. Collection phase: Extract definitions from headers in single pass * 3. Resolution phase: Resolve all reference nodes in single pass * 4. Cleanup phase: Remove definition markers from headers * * @example * ```typescript * import { unified } from 'unified'; * import remarkParse from 'remark-parse'; * import { remarkCrossReferencesAST } from './cross-references-ast.js'; * * const processor = unified() * .use(remarkParse) * .use(remarkCrossReferencesAST, { metadata: frontmatterData }); * * const result = await processor.process(content); * ``` * * @module */ import type { Plugin } from 'unified'; import type { Root } from 'mdast'; import type { YamlValue } from '../../types/index.js'; /** * Cross-reference definition extracted from headers */ interface CrossReferenceDefinition { key: string; level: number; sectionNumber: string; sectionText: string; headerText: string; position?: { line: number; column: number; }; } /** * Section counters for hierarchical numbering (extended to 9 levels) */ interface SectionCounters { level1: number; level2: number; level3: number; level4: number; level5: number; level6: number; level7: number; level8: number; level9: number; } /** * Plugin options for enhanced cross-reference processing */ interface CrossReferenceASTOptions { /** Document metadata containing level formats and other data */ metadata: Record<string, YamlValue>; /** Enable debug logging */ debug?: boolean; /** Enable field tracking with highlighting during AST processing */ enableFieldTracking?: boolean; } /** * Default level formats for section numbering (extended to 9 levels) */ /** * Parse |key| patterns and convert them to custom reference nodes * This happens in a single pass during the initial AST traversal */ declare function parseReferences(tree: Root): void; /** * Extract cross-reference definitions from reference nodes in headers * Single pass collection of all definitions */ declare function extractDefinitionsFromAST(root: Root, metadata: Record<string, YamlValue>, debug?: boolean): CrossReferenceDefinition[]; /** * Update section counters based on current level (expanded to 9 levels) */ declare function updateSectionCounters(counters: SectionCounters, level: number): void; /** * Generate section number using the enhanced header variable system */ declare function generateSectionNumber(level: number, counters: SectionCounters, levelFormats: Record<string, string>): string; /** * Convert number to Roman numeral */ declare function toRomanNumeral(num: number): string; /** * Resolve all reference nodes in a single pass */ declare function resolveReferences(root: Root, crossReferences: CrossReferenceDefinition[], metadata: Record<string, YamlValue>, enableFieldTracking?: boolean, debug?: boolean): void; /** * Format metadata values based on type and context */ declare function formatMetadataValue(value: YamlValue | undefined, key: string, metadata: Record<string, YamlValue>): string; /** * Enhanced remark plugin for processing cross-references using custom AST nodes */ export declare const remarkCrossReferencesAST: Plugin<[CrossReferenceASTOptions], Root>; export type { CrossReferenceASTOptions, CrossReferenceDefinition }; export { parseReferences as _parseReferences, extractDefinitionsFromAST as _extractDefinitionsFromAST, updateSectionCounters as _updateSectionCounters, generateSectionNumber as _generateSectionNumber, toRomanNumeral as _toRomanNumeral, resolveReferences as _resolveReferences, formatMetadataValue as _formatMetadataValue, }; //# sourceMappingURL=cross-references-ast.d.ts.map