legal-markdown-js
Version:
Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version
132 lines • 5.54 kB
TypeScript
/**
* @fileoverview Import Processing Module for Legal Markdown Documents
*
* This module provides functionality to process partial imports in Legal Markdown
* documents, allowing for modular document construction by including external
* files. It supports both absolute and relative import paths, recursive import
* processing, and comprehensive error handling for missing files.
*
* Features:
* - Import syntax: @import filename
* - Relative and absolute path resolution
* - Recursive import processing (nested imports)
* - Import tracking and cycle detection
* - Error handling with fallback content
* - Base path resolution for project organization
* - Import validation and file existence checking
*
* @example
* ```typescript
* import { processPartialImports } from './import-processor.js';
*
* // Main document content
* const content = `
* # Service Agreement
*
* @import ./clauses/standard-terms.md
*
* ## Specific Terms
* @import ./clauses/payment-terms.md
* @import ./clauses/termination.md
*
* @import ./signatures/signature-block.md
* `;
*
* const result = processPartialImports(content, './contracts');
* console.log(result.content); // Processed content with imports resolved
* console.log(result.importedFiles); // Array of imported file paths
* ```
*/
import { ImportProcessingResult, LegalMarkdownOptions, YamlValue } from '../../types/index.js';
/**
* Processes partial imports in a LegalMarkdown document
*
* This is the main function that processes import statements using the @import syntax.
* It recursively resolves and includes external files, tracking all imported files
* and handling errors gracefully when files cannot be found or loaded.
*
* By default, it also extracts YAML frontmatter from imported files and merges it using
* the "source always wins" strategy with flattened granular merging. This can be disabled
* with the disableFrontmatterMerge option.
*
* @deprecated This function is deprecated and will be removed in v4.0.0.
* Use `processLegalMarkdownWithRemark()` with the `remarkImports` plugin instead.
* The remark-based approach inserts content as AST nodes and provides better error handling.
* @see {@link https://github.com/yourrepo/legal-markdown-js/blob/main/docs/migration-guide.md Migration Guide}
*
* @param {string} content - The document content containing import statements
* @param {string} [basePath] - Optional base path for resolving relative imports
* @param {Record<string, any>} [currentMetadata] - Current document metadata for merging
* @param {LegalMarkdownOptions} [options] - Processing options including frontmatter merge settings
* @returns {ImportProcessingResult} Object containing processed content, list of imported files, and merged metadata
* @example
* ```typescript
* // Basic import processing
* const content = `
* # Main Document
* @import ./introduction.md
* @import ./body.md
* @import ./conclusion.md
* `;
*
* const result = processPartialImports(content, './documents');
* console.log(result.content); // Content with imports resolved
* console.log(result.importedFiles); // ['./documents/introduction.md', './documents/body.md', './documents/conclusion.md']
*
* // Nested imports example
* // main.md: @import ./sections/terms.md
* // terms.md: @import ./subsections/payment.md
* // Result will include content from all three files
*
* // Error handling
* const contentWithMissingFile = `
* # Document
* @import ./existing.md
* @import ./missing.md
* `;
*
* const result2 = processPartialImports(contentWithMissingFile);
* // Result will include content from existing.md and error comment for missing.md
* ```
*/
export declare function processPartialImports(content: string, basePath?: string, currentMetadata?: Record<string, YamlValue>, options?: LegalMarkdownOptions): ImportProcessingResult;
/**
* Validates that all import paths in a document exist
*
* Checks all import statements in a document to ensure the referenced files exist
* on the filesystem. Returns an array of error messages for any missing files,
* or an empty array if all imports are valid.
*
* @deprecated This function is deprecated and will be removed in v4.0.0.
* Use `processLegalMarkdownWithRemark()` with the `remarkImports` plugin instead.
* The remark-based approach handles validation automatically during import processing.
* @see {@link https://github.com/yourrepo/legal-markdown-js/blob/main/docs/migration-guide.md Migration Guide}
*
* @param {string} content - The document content containing import statements to validate
* @param {string} [basePath] - Optional base path for resolving relative imports
* @returns {string[]} Array of validation errors, empty if all imports are valid
* @example
* ```typescript
* const content = `
* # Document
* @import ./existing-file.md
* @import ./missing-file.md
* @import /absolute/path/to/file.md
* `;
*
* const errors = validateImports(content, './documents');
* console.log(errors);
* // Output (if files are missing):
* // [
* // "Import file not found: ./documents/missing-file.md",
* // "Import file not found: /absolute/path/to/file.md"
* // ]
*
* // For valid imports
* const validContent = `@import ./existing-file.md`;
* const noErrors = validateImports(validContent, './documents');
* console.log(noErrors); // [] (empty array)
* ```
*/
export declare function validateImports(content: string, basePath?: string): string[];
//# sourceMappingURL=import-processor.d.ts.map