markmv
Version:
TypeScript CLI for markdown file operations with intelligent link refactoring
207 lines • 7.38 kB
TypeScript
/**
* Represents a section of content extracted during a split operation.
*
* Contains all information needed to create a separate file from a portion of the original markdown
* content.
*
* @category Strategies
*/
export interface SplitSection {
/** Section title/identifier */
title: string;
/** Content of this section */
content: string;
/** Starting line number in original file */
startLine: number;
/** Ending line number in original file */
endLine: number;
/** Header level (1-6 for # to ######) */
headerLevel?: number;
/** Suggested filename for this section */
filename: string;
}
/**
* Result of a split operation containing extracted sections and metadata.
*
* Provides information about all sections that were created and any content that remains in the
* original file.
*
* @category Strategies
*/
export interface SplitResult {
/** Array of sections to create as separate files */
sections: SplitSection[];
/** Any content that should remain in the original file */
remainingContent: string | undefined;
/** Errors encountered during splitting */
errors: string[];
/** Warnings */
warnings: string[];
}
/**
* Configuration options for split strategy operations.
*
* Controls various aspects of the splitting process including output location, splitting criteria,
* and filename generation patterns.
*
* @category Strategies
*/
export interface SplitStrategyOptions {
/** Output directory for split files */
outputDir?: string;
/** Maximum file size in KB (for size-based strategy) */
maxSize?: number;
/** Header level to split on (for header-based strategy) */
headerLevel?: number;
/** Custom split markers (for manual strategy) */
splitMarkers?: string[];
/** Line numbers to split on (for line-based strategy) */
splitLines?: number[] | undefined;
/** Whether to preserve frontmatter in original file */
preserveFrontmatter?: boolean;
/** Filename pattern for generated files */
filenamePattern?: string;
}
/**
* Abstract base class for all split strategies.
*
* Provides common functionality for splitting markdown files including filename generation,
* frontmatter handling, and content sanitization. Concrete strategies implement specific splitting
* algorithms.
*
* @category Strategies
*
* @example
* Implementing a custom split strategy
* ```typescript
* class CustomSplitStrategy extends BaseSplitStrategy {
* async split(content: string, originalFilename: string): Promise<SplitResult> {
* // Custom splitting logic
* const sections = this.customSplit(content);
* return { sections, remainingContent: undefined, errors: [], warnings: [] };
* }
* }
* ```
*/
export declare abstract class BaseSplitStrategy {
protected options: SplitStrategyOptions;
constructor(options?: SplitStrategyOptions);
abstract split(content: string, originalFilename: string): Promise<SplitResult>;
/** Generate a safe filename from a title */
protected generateFilename(title: string, index: number, originalFilename: string): string;
/** Sanitize a string to be safe for use as filename */
protected sanitizeFilename(str: string): string;
/** Extract frontmatter from content */
protected extractFrontmatter(content: string): {
frontmatter: string;
content: string;
};
/** Extract title from header line */
protected extractTitleFromHeader(headerLine: string): string;
/** Count the header level (number of # characters) */
protected getHeaderLevel(line: string): number;
/** Check if a line is a header at or above the specified level */
protected isTargetHeader(line: string, targetLevel: number): boolean;
}
/**
* Split strategy that divides content based on markdown headers.
*
* Splits the file at headers of a specified level, creating a new file for each section. This is
* ideal for documents with clear hierarchical structure where each major section can stand alone.
*
* @category Strategies
*
* @example
* Header-based splitting
* ```typescript
* const strategy = new HeaderBasedSplitStrategy({
* headerLevel: 2, // Split on ## headers
* outputDir: './sections/',
* filenamePattern: '{title}'
* });
*
* const result = await strategy.split(content, 'document.md');
* console.log(`Created ${result.sections.length} sections`);
* ```
*/
export declare class HeaderBasedSplitStrategy extends BaseSplitStrategy {
split(content: string, originalFilename: string): Promise<SplitResult>;
}
/**
* Split strategy that divides content based on file size limits.
*
* Creates new files when the current section exceeds a specified size limit. This ensures that no
* generated file becomes too large, which is useful for performance or platform constraints.
*
* @category Strategies
*
* @example
* Size-based splitting
* ```typescript
* const strategy = new SizeBasedSplitStrategy({
* maxSize: 50, // 50KB per file
* outputDir: './chunks/',
* filenamePattern: '{original}-part-{index}'
* });
*
* const result = await strategy.split(content, 'large-document.md');
* console.log(`Split into ${result.sections.length} files under 50KB each`);
* ```
*/
export declare class SizeBasedSplitStrategy extends BaseSplitStrategy {
split(content: string, originalFilename: string): Promise<SplitResult>;
private findNearestHeader;
/** Generate filename for size-based sections, ensuring uniqueness */
private generateSizeBasedFilename;
}
/**
* Split strategy that divides content at manually specified markers.
*
* Looks for specific comment markers or text patterns in the content to determine split points.
* This provides precise control over where splits occur, regardless of content structure.
*
* @category Strategies
*
* @example
* Manual marker splitting
* ```typescript
* const strategy = new ManualSplitStrategy({
* splitMarkers: ['<!-- split -->', '---BREAK---'],
* outputDir: './parts/',
* filenamePattern: '{title}'
* });
*
* // Content with markers like: <!-- split -->
* const result = await strategy.split(content, 'document.md');
* ```
*/
export declare class ManualSplitStrategy extends BaseSplitStrategy {
split(content: string, originalFilename: string): Promise<SplitResult>;
private findSectionTitle;
}
/**
* Split strategy that divides content at specific line numbers.
*
* Allows precise splitting at user-specified line numbers. This is useful when you know exactly
* where you want to split a document, perhaps based on analysis or external requirements.
*
* @category Strategies
*
* @example
* Line-based splitting
* ```typescript
* const strategy = new LineBasedSplitStrategy({
* splitLines: [100, 250, 400], // Split at these line numbers
* outputDir: './sections/',
* filenamePattern: 'section-{index}'
* });
*
* const result = await strategy.split(content, 'document.md');
* console.log(`Split at lines: ${strategy.options.splitLines?.join(', ')}`);
* ```
*/
export declare class LineBasedSplitStrategy extends BaseSplitStrategy {
split(content: string, originalFilename: string): Promise<SplitResult>;
private findLineSectionTitle;
}
//# sourceMappingURL=split-strategies.d.ts.map