markmv
Version:
TypeScript CLI for markdown file operations with intelligent link refactoring
226 lines • 8.16 kB
TypeScript
/**
* Represents a section of content to be merged into a target file.
*
* Contains content information, positioning strategy, and metadata for intelligent merging
* operations.
*
* @category Strategies
*/
export interface MergeSection {
/** Content of the section */
content: string;
/** Source file path */
sourceFile: string;
/** Position in the merge (before, after, or replace) */
position: 'before' | 'after' | 'replace' | 'interactive';
/** Header level if this section has a header */
headerLevel?: number;
/** Whether this is an Obsidian transclusion */
isTransclusion?: boolean;
/** Transclusion reference if applicable */
transclusionRef?: string;
}
/**
* Result of a merge operation containing combined content and metadata.
*
* Provides comprehensive information about the merging process including conflicts, transclusions,
* and any issues encountered.
*
* @category Strategies
*/
export interface MergeResult {
/** Whether the merge was successful */
success: boolean;
/** Final merged content */
content: string;
/** Combined frontmatter */
frontmatter?: string;
/** Source files that were merged */
sourceFiles: string[];
/** Conflicts that were resolved or need attention */
conflicts: MergeConflict[];
/** Warnings */
warnings: string[];
/** Errors */
errors: string[];
/** Transclusions that were created */
transclusions: string[];
}
/**
* Represents a conflict detected during the merge operation.
*
* Conflicts can arise from header collisions, content overlaps, or transclusion loops that require
* resolution.
*
* @category Strategies
*/
export interface MergeConflict {
/** Type of conflict */
type: 'header-collision' | 'content-overlap' | 'transclusion-loop' | 'frontmatter-conflict';
/** Description of the conflict */
description: string;
/** Source files involved */
sourceFiles: string[];
/** Suggested resolution strategy */
resolution?: string;
/** Line numbers where conflict occurs */
lines?: number[];
/** Whether conflict was auto-resolved */
autoResolved: boolean;
}
/**
* Configuration options for merge strategy operations.
*
* Controls various aspects of the merging process including conflict resolution, transclusion
* handling, and content formatting.
*
* @category Strategies
*/
export interface MergeStrategyOptions {
/** Strategy for handling conflicts */
conflictResolution?: 'auto' | 'interactive' | 'manual';
/** Separator between merged sections */
separator?: string;
/** Whether to create Obsidian transclusions */
createTransclusions?: boolean;
/** Whether to merge frontmatter */
mergeFrontmatter?: boolean;
/** Whether to preserve original structure */
preserveStructure?: boolean;
/** Custom transclusion template */
transclusionTemplate?: string;
/** Maximum depth for transclusion resolution */
maxTransclusionDepth?: number;
}
/**
* Abstract base class for all merge strategies.
*
* Provides common functionality for merging markdown files including transclusion handling,
* conflict detection, and frontmatter management. Concrete strategies implement specific merging
* approaches.
*
* @category Strategies
*
* @example
* Implementing a custom merge strategy
* ```typescript
* class CustomMergeStrategy extends BaseMergeStrategy {
* async merge(targetContent: string, sourceContent: string): Promise<MergeResult> {
* // Custom merging logic
* const conflicts = this.detectConflicts(targetContent, sourceContent);
* return this.buildResult(mergedContent, conflicts);
* }
* }
* ```
*/
export declare abstract class BaseMergeStrategy {
protected options: MergeStrategyOptions;
constructor(options?: MergeStrategyOptions);
abstract merge(targetContent: string, sourceContent: string, targetFile: string, sourceFile: string): Promise<MergeResult>;
/** Extract Obsidian transclusions from content */
protected extractTransclusions(content: string): Array<{
ref: string;
file: string;
section?: string;
line: number;
}>;
/** Create an Obsidian transclusion reference */
protected createTransclusion(file: string, section?: string): string;
/** Detect potential transclusion loops */
protected detectTransclusionLoops(targetFile: string, sourceFile: string, existingTransclusions: string[]): boolean;
/** Merge frontmatter from two sources */
protected mergeFrontmatter(targetFrontmatter: string, sourceFrontmatter: string): string;
private parseFrontmatter;
private stringifyFrontmatter;
/** Extract headers from content with their levels */
protected extractHeaders(content: string): Array<{
text: string;
level: number;
line: number;
}>;
/** Find potential header conflicts between target and source */
protected findHeaderConflicts(targetContent: string, sourceContent: string): Array<{
header: string;
targetLine: number;
sourceLine: number;
}>;
}
/**
* Merge strategy that appends source content to the end of target content.
*
* Simply adds the source file content to the end of the target file, with optional separator and
* frontmatter merging. This is the simplest merge strategy and works well for accumulating
* content.
*
* @category Strategies
*
* @example
* Append merge with transclusions
* ```typescript
* const strategy = new AppendMergeStrategy({
* createTransclusions: true,
* separator: '\n\n---\n\n'
* });
*
* const result = await strategy.merge(targetContent, sourceContent, 'target.md', 'source.md');
* console.log(`Appended content, ${result.transclusions.length} transclusions created`);
* ```
*/
export declare class AppendMergeStrategy extends BaseMergeStrategy {
merge(targetContent: string, sourceContent: string, targetFile: string, sourceFile: string): Promise<MergeResult>;
private extractFrontmatterFromContent;
private stripFrontmatter;
}
/**
* Merge strategy that prepends source content to the beginning of target content.
*
* Adds the source file content to the beginning of the target file, after any frontmatter. This is
* useful when you want new content to appear first in the document.
*
* @category Strategies
*
* @example
* Prepend merge with custom separator
* ```typescript
* const strategy = new PrependMergeStrategy({
* separator: '\n\n<!-- New Content Above -->\n\n',
* mergeFrontmatter: true
* });
*
* const result = await strategy.merge(targetContent, sourceContent, 'target.md', 'source.md');
* console.log('Source content prepended to target');
* ```
*/
export declare class PrependMergeStrategy extends BaseMergeStrategy {
merge(targetContent: string, sourceContent: string, targetFile: string, sourceFile: string): Promise<MergeResult>;
private extractFrontmatterFromContent;
private stripFrontmatter;
}
/**
* Merge strategy that provides intelligent conflict detection and resolution.
*
* Analyzes both files to detect potential conflicts such as duplicate headers, overlapping content,
* or structural issues. Provides automatic resolution where possible and clear reporting of
* conflicts that need manual attention.
*
* @category Strategies
*
* @example
* Interactive merge with conflict resolution
* ```typescript
* const strategy = new InteractiveMergeStrategy({
* conflictResolution: 'auto',
* createTransclusions: true,
* preserveStructure: true
* });
*
* const result = await strategy.merge(targetContent, sourceContent, 'target.md', 'source.md');
* console.log(`Merge completed with ${result.conflicts.length} conflicts detected`);
* ```
*/
export declare class InteractiveMergeStrategy extends BaseMergeStrategy {
merge(targetContent: string, sourceContent: string, targetFile: string, sourceFile: string): Promise<MergeResult>;
private extractFrontmatterFromContent;
private stripFrontmatter;
}
//# sourceMappingURL=merge-strategies.d.ts.map