legal-markdown-js
Version:
Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version
151 lines • 5.1 kB
TypeScript
/**
* YAML Front Matter Parser for Legal Markdown Documents
*
* This module provides functionality to parse YAML front matter from Legal Markdown
* documents, extracting metadata and configuration options for document processing.
* It handles both valid and invalid YAML gracefully, with options for strict error
* handling when needed.
*
* Features:
* - YAML front matter parsing with js-yaml library
* - Graceful error handling for malformed YAML
* - Metadata extraction and validation
* - Content separation from front matter
* - YAML serialization utilities
* - Metadata output configuration extraction
*
* @example
* ```typescript
* import { parseYamlFrontMatter } from './yaml-parser';
*
* const content = `---
* title: Legal Agreement
* date: 2024-01-01
* parties:
* - name: Company A
* role: Provider
* ---
* # Agreement Content
* This is the document content.`;
*
* const result = parseYamlFrontMatter(content);
* console.log(result.metadata.title); // "Legal Agreement"
* console.log(result.content); // "# Agreement Content\nThis is the document content."
* ```
*
* @module
*/
import { YamlParsingResult } from '../../types';
/**
* Parses YAML Front Matter from a markdown document
*
* Extracts and parses YAML metadata from the beginning of a document,
* separated by triple dashes (---). The parser handles malformed YAML
* gracefully unless strict error handling is enabled.
*
* @param {string} content - The content of the document to parse
* @param {boolean} [throwOnError=false] - Whether to throw errors on invalid YAML
* @returns {YamlParsingResult} Object containing the content without YAML and the parsed metadata
* @throws {Error} When throwOnError is true and YAML parsing fails
* @example
* ```typescript
* // Basic usage with valid YAML
* const content = `---
* title: Contract
* version: 1.0
* ---
* # Contract Content`;
*
* const result = parseYamlFrontMatter(content);
* // result.metadata = { title: "Contract", version: 1.0 }
* // result.content = "# Contract Content"
*
* // Usage with error handling
* const malformedContent = `---
* title: Contract
* invalid: yaml: content
* ---
* # Content`;
*
* const safeResult = parseYamlFrontMatter(malformedContent, false);
* // Returns original content with empty metadata
*
* const strictResult = parseYamlFrontMatter(malformedContent, true);
* // Throws Error: "Invalid YAML Front Matter: ..."
* ```
*/
export declare function parseYamlFrontMatter(content: string, throwOnError?: boolean): YamlParsingResult;
/**
* Serializes metadata to YAML format
*
* Converts a JavaScript object to YAML string format using js-yaml library.
* Handles serialization errors gracefully by returning an empty string and
* logging the error to the console.
*
* @param {Record<string, any>} metadata - The metadata object to serialize
* @returns {string} YAML string representation of the metadata
* @example
* ```typescript
* const metadata = {
* title: "Legal Agreement",
* date: "2024-01-01",
* parties: [
* { name: "Company A", role: "Provider" },
* { name: "Company B", role: "Client" }
* ]
* };
*
* const yamlString = serializeToYaml(metadata);
* console.log(yamlString);
* // Output:
* // title: Legal Agreement
* // date: '2024-01-01'
* // parties:
* // - name: Company A
* // role: Provider
* // - name: Company B
* // role: Client
* ```
*/
export declare function serializeToYaml(metadata: Record<string, any>): string;
/**
* Extracts specific metadata output configuration
*
* Parses document metadata to extract configuration options for metadata output,
* including file paths, formats, and inclusion settings. This function looks for
* specially named metadata fields that control how processed metadata is exported.
*
* @param {Record<string, any>} metadata - The document metadata to extract configuration from
* @returns {Object} Configuration object for metadata output
* @returns {string} [returns.yamlOutput] - Path for YAML metadata output file
* @returns {string} [returns.jsonOutput] - Path for JSON metadata output file
* @returns {string} [returns.outputPath] - General output path for metadata files
* @returns {boolean} [returns.includeOriginal] - Whether to include original metadata in output
* @example
* ```typescript
* const metadata = {
* title: "Contract",
* "meta-yaml-output": "contract-metadata.yml",
* "meta-json-output": "contract-metadata.json",
* "meta-output-path": "./output/",
* "meta-include-original": true
* };
*
* const config = extractMetadataOutputConfig(metadata);
* console.log(config);
* // Output:
* // {
* // yamlOutput: "contract-metadata.yml",
* // jsonOutput: "contract-metadata.json",
* // outputPath: "./output/",
* // includeOriginal: true
* // }
* ```
*/
export declare function extractMetadataOutputConfig(metadata: Record<string, any>): {
yamlOutput?: string;
jsonOutput?: string;
outputPath?: string;
includeOriginal?: boolean;
};
//# sourceMappingURL=yaml-parser.d.ts.map