Legal Markdown JS features an intelligent archiving system that automatically organizes processed files based on content analysis. The system compares original and processed content to determine the optimal archiving strategy, ensuring both templates and results are preserved appropriately.
- Overview
- Archive Logic
- Content Comparison
- Archive Scenarios
- Configuration and Usage
- Archive Features
- Use Cases
- Best Practices
The smart archiving system automatically decides whether to save one or two versions of your documents based on whether processing changed the content. This intelligent approach ensures:
- Template preservation for reusable documents
- Result preservation for processed outputs
- Storage efficiency by avoiding unnecessary duplicates
- Clear organization with descriptive file naming
- Automatic Decision Making: No manual choices needed
- Template Safety: Original templates are never lost
- Audit Trail: Complete record of processing results
- Storage Optimization: Eliminates redundant file storage
The smart archiving system makes decisions based on content comparison:
// Smart archiving workflow
const originalContent = readFile('document.md');
const processedContent = await processLegalMarkdown(originalContent);
if (contentsAreIdentical(originalContent, processedContent)) {
// Archive only the original file
archive('document.md' → 'archive/document.md');
} else {
// Archive both versions with clear suffixes
archive('document.md' → 'archive/document.ORIGINAL.md'); // Template
writeFile('archive/document.PROCESSED.md', processedContent); // Result
}Document Processing
│
▼
Content Comparison
│
┌───┴───┐
▼ ▼
Identical Different
│ │
▼ ▼
Single Dual
Archive Archive
The system performs intelligent content comparison that:
- Normalizes line endings (handles Windows/Unix differences)
- Trims whitespace (ignores formatting differences)
- Compares actual content (focuses on meaningful changes)
// Content normalization for comparison
function areContentsIdentical(content1: string, content2: string): boolean {
const normalize = (content: string) => content.replace(/\r\n/g, '\n').trim();
return normalize(content1) === normalize(content2);
}- Cross-platform compatibility: Handles different line ending formats
- Whitespace tolerance: Ignores insignificant formatting changes
- Content focus: Detects meaningful content modifications
- Binary safety: Works with various file encodings
For documents where processing doesn't change the content (e.g., documents with only frontmatter):
---
title: Legal Notice
client: Acme Corp
date: 2024-01-01
---
# Legal Notice
This is a static legal notice.Archive Result:
archive/legal-notice.md # Single file - template is preservedThe document contains no template variables, imports, or conditional content, so processing produces identical output.
For documents with imports, mixins, or variable substitution:
---
title: Service Agreement
client: Acme Corp
effective_date: 2024-01-01
---
# {{title}}
@import clauses/standard-terms.md
This agreement between Legal Services Inc. and {{client}}
is effective {{formatDate(effective_date, "MMMM Do, YYYY")}}.
[Confidentiality clause applies]{client.confidentiality_required}Archive Result:
archive/service-agreement.ORIGINAL.md # Template file
archive/service-agreement.PROCESSED.md # Processed resultThe document contains template variables and imports, so processing produces different output that needs to be preserved separately.
When files with the same name already exist in the archive:
# First document
legal-md contract.md --archive-source ./archive
# Creates: archive/contract.md
# Second document with same name
legal-md contract.md --archive-source ./archive
# Creates: archive/contract_1.md (automatic renaming)
# With different content (template processing)
legal-md template.md --archive-source ./archive
# Creates: archive/template.ORIGINAL_1.md
# archive/template.PROCESSED_1.mdFor documents with multiple template features:
---
clients:
- name: "Acme Corp"
premium: true
- name: "Beta Inc"
premium: false
base_rate: 500
---
@import headers/legal-header.md
{{#clients}}
## Service Agreement for {{name}}
Rate: {{formatCurrency(base_rate, "USD")}}
{{#if premium}}
- Premium support included
- 24/7 availability
{{/if}}
{{/clients}}Archive Result: Dual archiving (template + processed) due to multiple dynamic elements.
# Enable smart archiving with default directory
legal-md document.md --archive-source
# Custom archive directory
legal-md document.md --archive-source ./completed
# With PDF generation
legal-md document.md --pdf --highlight --archive-source ./processed# Set default archive directory
export ARCHIVE_DIR="./processed-documents"
# Use default directory
legal-md document.md --archive-sourceimport { CliService } from 'legal-markdown-js';
const cliService = new CliService({
archiveSource: './archive',
});
// Smart archiving happens automatically
await cliService.processFile('template.md', 'output.md');# Archive with metadata export
legal-md template.md --archive-source ./archive --export-yaml --export-json
# Archive with custom CSS and formatting
legal-md template.md --archive-source ./archive --css custom.css --pdf --highlight
# Force archiving for debugging
legal-md template.md --archive-source ./debug --debug- Intelligent Decision Making: Automatically determines whether to archive one or two files
- Template Preservation: Keeps original templates intact for reuse
- Result Preservation: Saves processed content for reference
- Clear Naming: Uses
.ORIGINALand.PROCESSEDsuffixes for clarity - Conflict Resolution: Automatic renaming when files already exist
- Error Handling: Graceful handling of archive failures
- Cross-Platform: Works consistently across different operating systems
| Scenario | Original File | Archive Result |
|---|---|---|
| No changes | document.md |
archive/document.md |
| With changes | document.md |
archive/document.ORIGINAL.md and archive/document.PROCESSED.md |
| Name conflict | document.md |
archive/document_1.md |
| Complex conflict | document.md |
archive/document.ORIGINAL_1.md and archive/document.PROCESSED_1.md |
When archiving, the system preserves:
- Original YAML frontmatter in
.ORIGINALfiles - Processed metadata in accompanying files
- File timestamps and basic metadata
- Directory structure relationships
# Process multiple templates
for template in templates/*.md; do
legal-md "$template" --pdf --archive-source ./completed
done
# Templates with imports → dual archiving
# Static templates → single archivingThis approach allows you to:
- Maintain template libraries
- Preserve processing results
- Track template evolution
- Support template reuse
# Process and archive in production pipeline
legal-md contract-template.md --pdf --highlight --archive-source ./production-archive
# Preserves both template (for future use) and result (for records)Benefits for workflows:
- Audit compliance: Complete processing history
- Template reuse: Original templates remain available
- Result tracking: Processed outputs are preserved
- Quality assurance: Compare inputs and outputs
# Archive for review and compliance
legal-md legal-document.md --pdf --export-json --archive-source ./compliance
# Smart archiving helps maintain audit trailQA applications:
- Compliance audits: Complete document history
- Process validation: Verify template processing
- Change tracking: Monitor document evolution
- Backup strategy: Automated document preservation
# Process entire directories with archiving
find ./contracts -name "*.md" -exec legal-md {} --pdf --archive-source ./processed-contracts \;Batch benefits:
- Efficient processing: Handle multiple documents
- Consistent archiving: Uniform organization
- Storage optimization: Intelligent space usage
- Scalable workflows: Handle large document sets
# Archive with debug information
legal-md test-template.md --archive-source ./debug --debug --export-yaml
# Compare processing results
diff ./debug/test-template.ORIGINAL.md ./debug/test-template.PROCESSED.mdDevelopment advantages:
- Template debugging: Compare inputs and outputs
- Processing verification: Validate template logic
- Performance testing: Track processing changes
- Regression testing: Ensure consistent results
Structure your archive directories logically:
archives/
├── production/ # Live document processing
├── staging/ # Testing and validation
├── development/ # Template development
└── compliance/ # Audit and regulatoryUse descriptive archive directory names:
# ✅ Good - descriptive purposes
--archive-source ./completed-contracts
--archive-source ./processed-invoices
--archive-source ./compliance-archive
# ❌ Avoid - generic names
--archive-source ./archive
--archive-source ./files
--archive-source ./done# Periodic cleanup of old archives
find ./archives -name "*.md" -mtime +365 -delete
# Compress old archives
tar -czf archives-2023.tar.gz ./archives/2023/# Archive to version-controlled directory
legal-md template.md --archive-source ./git-tracked-archives
# Exclude from git if too large
echo "large-archives/" >> .gitignore# Regular archive backups
rsync -av ./archives/ ./backup/archives/
# Cloud backup integration
aws s3 sync ./archives/ s3://legal-document-archives/# Log archiving operations
legal-md template.md --archive-source ./archive --debug 2>&1 | tee archive.log
# Monitor archive directory sizes
du -sh ./archives/*/# Set appropriate permissions
chmod 750 ./archives/
chmod 640 ./archives/*.md
# Restrict access to sensitive archives
chown legal-team:legal-group ./archives/compliance/# Archive with metadata export
legal-md contract.md --archive-source ./archive --export-yaml --export-jsonResult:
archive/contract.ORIGINAL.md
archive/contract.PROCESSED.md
archive/contract.yaml
archive/contract.json
# Archive with PDF output
legal-md template.md --pdf --archive-source ./archivePDF files are also archived alongside markdown files.
Templates with force commands are intelligently archived:
---
title: 'Auto-configured Document'
force_commands: '--pdf --highlight --archive-source ./auto-archive'
---The force commands trigger archiving automatically.
Archive directory creation fails:
# Ensure parent directory exists
mkdir -p ./path/to/archive
legal-md document.md --archive-source ./path/to/archivePermission denied:
# Check directory permissions
ls -la ./archive/
chmod 755 ./archive/Disk space issues:
# Check available space
df -h ./archive/
# Clean old archives if needed# Enable debug output for archiving
legal-md document.md --archive-source ./archive --debugThis shows:
- Content comparison results
- Archive decision reasoning
- File operation details
- Error messages and warnings
- CLI Reference - Archive-related command options
- Force Commands - Auto-archiving with force commands
- YAML Frontmatter - Metadata that affects archiving decisions
- Partial Imports - Import processing affects archiving strategy