UNPKG

legal-markdown-js

Version:

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

116 lines 4.37 kB
/** * Remark plugin for Legal Markdown date processing * * This plugin processes date references in Legal Markdown documents using * AST-based processing. It handles {{@today}} tokens with format specifiers and * arithmetic operations, integrating with the comprehensive date helper system. * * Features: * - {{@today}} token processing with format specifiers ({{@today[format]}}) * - Date arithmetic with +/- operations ({{@today+30}}, {{@today-365}}) * - Advanced date formatting (legal, ISO, US, European, etc.) * - Timezone and locale support * - Integration with field tracking for highlighting * - Comprehensive error handling with fallback formatting * * Architecture: * 1. Scan document content for {{@today}} patterns * 2. Parse format specifiers and arithmetic operations * 3. Process dates using advanced date helpers * 4. Replace tokens with formatted dates * 5. Integrate with field tracker for highlighting support * * Ordering: * - Run before `remarkTemplateFields` so `{{@today...}}` arithmetic expressions * are resolved before general `{{...}}` field expansion. * * @example * ```typescript * import { unified } from 'unified'; * import remarkParse from 'remark-parse'; * import remarkDates from './dates.js'; * * const processor = unified() * .use(remarkParse) * .use(remarkDates, { * metadata: { 'date-format': 'legal', timezone: 'America/New_York' }, * enableFieldTracking: true * }); * * const result = await processor.process('Contract signed on {{@today[legal]}}.'); * // Result: "Contract signed on January 15th, 2024." * ``` * * @module */ import type { Plugin } from 'unified'; import type { Root } from 'mdast'; import type { YamlValue } from '../../types/index.js'; /** * Plugin options for date processing */ interface DateProcessingOptions { /** Document metadata containing date formatting options */ metadata: Record<string, YamlValue>; /** Enable debug logging */ debug?: boolean; /** Enable field tracking with highlighting during AST processing */ enableFieldTracking?: boolean; } /** A single arithmetic step to apply to a date */ interface ArithmeticOp { type: 'days' | 'months' | 'years'; amount: number; } /** * Parse date token to extract a list of arithmetic operations and an optional format. * @param token - Everything after @today (e.g. "+2y-90d[US]", "[legal]", "+1y", "") * @returns { arithmeticList, format, isValid } */ declare function parseDateToken(token: string): { arithmeticList: ArithmeticOp[]; format: string | null; isValid: boolean; }; /** * Apply arithmetic operation to a date */ declare function applyDateArithmetic(baseDate: Date, arithmetic: { type: 'days' | 'months' | 'years'; amount: number; } | null): Date; /** * Get ordinal suffix for a day number (st, nd, rd, th) * * Returns the appropriate English ordinal suffix for a given day number. * Special handling for 11-13: these always end in "th" (11th, 12th, 13th) * rather than following the last-digit rule (which would give 11st, 12nd, 13rd). * This is because the teens are irregular in English ordinal numbering. * * @param day - The day number (1-31) * @returns The ordinal suffix ('st', 'nd', 'rd', or 'th') * * @example * getOrdinalSuffix(1) // returns 'st' -> "1st" * getOrdinalSuffix(2) // returns 'nd' -> "2nd" * getOrdinalSuffix(3) // returns 'rd' -> "3rd" * getOrdinalSuffix(11) // returns 'th' -> "11th" (special case) * getOrdinalSuffix(21) // returns 'st' -> "21st" */ declare function getOrdinalSuffix(day: number): string; /** * Basic date formatting fallback */ declare function formatDateBasic(date: Date, format: string): string; /** * Format date value with optional field tracking wrapper */ declare function formatDateValue(value: string, originalToken: string, enableFieldTracking?: boolean, hasArithmetic?: boolean): string; /** * Remark plugin for processing date references in Legal Markdown documents */ declare const remarkDates: Plugin<[DateProcessingOptions], Root>; export default remarkDates; export type { DateProcessingOptions }; export { parseDateToken as _parseDateToken, applyDateArithmetic as _applyDateArithmetic, getOrdinalSuffix as _getOrdinalSuffix, formatDateBasic as _formatDateBasic, formatDateValue as _formatDateValue, }; //# sourceMappingURL=dates.d.ts.map