All files / src obsidianMarkdownPreprocessor.ts

75.63% Statements 90/119
37.5% Branches 3/8
75% Functions 3/4
75.63% Lines 90/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 1845x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x                                                   71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x     71x   71x     71x 71x 76x 76x 5x   76x 76x     76x   76x           71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x   71x                                                     71x       71x     71x 12x   12x         71x 1x   1x         71x                    
import { BlockProcessor } from './processors/blockProcessor';
import { ExcalidrawProcessor } from './processors/excalidrawProcessor';
import { FootnoteProcessor } from './processors/footNoteProcessor';
import { FormatProcessor } from './processors/formatProcessor';
import { FragmentProcessor } from './processors/fragmentProcessor';
import { GridProcessor } from './processors/gridProcessor';
import { ImageProcessor } from './processors/imageProcessor';
import { InternalLinkProcessor } from './processors/internalLinkProcessor';
import { LatexProcessor } from './processors/latexProcessor';
import { MermaidProcessor } from './processors/mermaidProcessor';
import { MultipleFileProcessor } from './processors/multipleFileProcessor';
import { ObsidianUtils } from './obsidianUtils';
import { Options } from './options';
import { CommentProcessor } from './processors/commentProcessor';
import { DropProcessor } from './processors/dropProcessor';
import { YamlStore } from './yamlStore';
import { AutoClosingProcessor } from './processors/autoClosingProcessor';
import { EmojiProcessor } from './processors/emojiProcessor';
import { IconsProcessor } from './processors/iconsProcessor';
import { DebugViewProcessor } from './processors/debugViewProcessor';
import { CalloutProcessor } from './processors/calloutProcessor';
import { TemplateProcessor } from './processors/templateProcessor';
import { ChartProcessor } from './processors/chartProcessor';
import { DefaultBackgroundProcessor } from './processors/defaultBackgroundProcessor';
import { ReferenceProcessor } from './processors/referenceProcessor';
import { SkipSlideProcessor } from './processors/skipSlideProcessor';
 
export class ObsidianMarkdownPreprocessor {
	private multipleFileProcessor: MultipleFileProcessor;
	private blockProcessor: BlockProcessor;
	private imageProcessor: ImageProcessor;
	private internalLinkProcessor: InternalLinkProcessor;
	private footnoteProcessor: FootnoteProcessor;
	private latexProcessor: LatexProcessor;
	private formatProcessor: FormatProcessor;
	private excalidrawProcessor: ExcalidrawProcessor;
	private mermaidProcessor: MermaidProcessor;
	private fragmentProcessor: FragmentProcessor;
	private gridProcessor: GridProcessor;
	private commentProcessor: CommentProcessor;
	private dropProcessor: DropProcessor;
	private autoClosingProcessor: AutoClosingProcessor;
	private emojiProcessor: EmojiProcessor;
	private iconsProcessor: IconsProcessor;
	private debugViewProcessor: DebugViewProcessor;
	private calloutProcessor: CalloutProcessor;
	private templateProcessor: TemplateProcessor;
	private chartProcessor: ChartProcessor;
	private defaultBackgroundProcessor: DefaultBackgroundProcessor;
	private referenceProcessor: ReferenceProcessor;
	private skipSlideProcessor: SkipSlideProcessor;
 
	constructor(utils: ObsidianUtils) {
		this.multipleFileProcessor = new MultipleFileProcessor(utils);
		this.blockProcessor = new BlockProcessor();
		this.imageProcessor = new ImageProcessor(utils);
		this.internalLinkProcessor = new InternalLinkProcessor(utils);
		this.footnoteProcessor = new FootnoteProcessor();
		this.latexProcessor = new LatexProcessor();
		this.formatProcessor = new FormatProcessor();
		this.excalidrawProcessor = new ExcalidrawProcessor(utils);
		this.mermaidProcessor = new MermaidProcessor();
		this.fragmentProcessor = new FragmentProcessor();
		this.gridProcessor = new GridProcessor();
		this.commentProcessor = new CommentProcessor();
		this.dropProcessor = new DropProcessor();
		this.autoClosingProcessor = new AutoClosingProcessor();
		this.emojiProcessor = new EmojiProcessor();
		this.iconsProcessor = new IconsProcessor();
		this.debugViewProcessor = new DebugViewProcessor();
		this.calloutProcessor = new CalloutProcessor();
		this.templateProcessor = new TemplateProcessor(utils);
		this.chartProcessor = new ChartProcessor();
		this.defaultBackgroundProcessor = new DefaultBackgroundProcessor();
		this.referenceProcessor = new ReferenceProcessor();
		this.skipSlideProcessor = new SkipSlideProcessor();
	}
	process(markdown: string, options: Options) {
		YamlStore.getInstance().options = options;
 
		let before = this.trimEnding(markdown, options);
		let after;
 
		let circuitCounter = 0;
		while (before != after) {
			circuitCounter++;
			if (after) {
				before = after;
			}
			const afterMultipleFileProcessor = this.multipleFileProcessor.process(before);
			after = this.templateProcessor.process(afterMultipleFileProcessor, options);
 
			//Remove default templates after first pass
			options.defaultTemplate = null;
 
			Iif (circuitCounter > 9) {
				console.log('WARNING: Circuit in template hierarchy detected!');
				break;
			}
		}
 
		const afterSkipSlideProcessor = this.skipSlideProcessor.process(after, options);
		const afterReferenceProcessor = this.referenceProcessor.process(afterSkipSlideProcessor);
		const afterDebugViewProcessor = this.debugViewProcessor.process(afterReferenceProcessor, options);
		const afterAutoClosingProcessor = this.autoClosingProcessor.process(afterDebugViewProcessor);
		const defaultBackgroundProcessor = this.defaultBackgroundProcessor.process(afterAutoClosingProcessor, options);
		const afterCalloutProcessor = this.calloutProcessor.process(defaultBackgroundProcessor);
		const afterEmojiProcessor = this.emojiProcessor.process(afterCalloutProcessor);
		const afterIconsProcessor = this.iconsProcessor.process(afterEmojiProcessor);
		const afterMermaidProcessor = this.mermaidProcessor.process(afterIconsProcessor);
		const afterBlockProcessor = this.blockProcessor.process(afterMermaidProcessor);
		const afterFootNoteProcessor = this.footnoteProcessor.process(afterBlockProcessor, options);
		const afterExcalidrawProcessor = this.excalidrawProcessor.process(afterFootNoteProcessor);
		const afterImageProcessor = this.imageProcessor.process(afterExcalidrawProcessor);
		const afterInternalLinkProcessor = this.internalLinkProcessor.process(afterImageProcessor, options);
		const afterLatexProcessor = this.latexProcessor.process(afterInternalLinkProcessor);
		const afterFormatProcessor = this.formatProcessor.process(afterLatexProcessor);
		const afterFragmentProcessor = this.fragmentProcessor.process(afterFormatProcessor, options);
		const afterDropProcessor = this.dropProcessor.process(afterFragmentProcessor, options);
		const afterGridProcessor = this.gridProcessor.process(afterDropProcessor, options);
		const afterCommentProcessor = this.commentProcessor.process(afterGridProcessor);
		const afterChartProcessor = this.chartProcessor.process(afterCommentProcessor, options);
 
		Iif (options.log) {
			this.log('markdown', '', markdown);
			this.log('merge & template', markdown, after);
			this.log('afterSkipSlideProcessor', after, afterSkipSlideProcessor);
			this.log('afterReferenceProcessor', afterSkipSlideProcessor, afterReferenceProcessor);
			this.log('afterDebugViewProcessor', afterReferenceProcessor, afterDebugViewProcessor);
			this.log('afterAutoClosingProcessor', afterDebugViewProcessor, afterAutoClosingProcessor);
			this.log('defaultBackgroundProcessor', afterAutoClosingProcessor, defaultBackgroundProcessor);
			this.log('afterCalloutProcessor', defaultBackgroundProcessor, afterCalloutProcessor);
			this.log('afterEmojiProcessor', afterCalloutProcessor, afterEmojiProcessor);
			this.log('afterIconsProcessor', afterEmojiProcessor, afterIconsProcessor);
			this.log('afterMermaidProcessor', afterIconsProcessor, afterMermaidProcessor);
			this.log('afterBlockProcessor', afterMermaidProcessor, afterBlockProcessor);
			this.log('afterFootNoteProcessor', afterBlockProcessor, afterFootNoteProcessor);
			this.log('afterExcalidrawProcessor', afterFootNoteProcessor, afterExcalidrawProcessor);
			this.log('afterImageProcessor', afterExcalidrawProcessor, afterImageProcessor);
			this.log('afterInternalLinkProcessor', afterImageProcessor, afterInternalLinkProcessor);
			this.log('afterLatexProcessor', afterInternalLinkProcessor, afterLatexProcessor);
			this.log('afterFormatProcessor', afterLatexProcessor, afterFormatProcessor);
			this.log('afterFragmentProcessor', afterFormatProcessor, afterFragmentProcessor);
			this.log('afterDropProcessor', afterFragmentProcessor, afterDropProcessor);
			this.log('afterGridProcessor', afterDropProcessor, afterGridProcessor);
			this.log('afterCommentProcessor', afterGridProcessor, afterCommentProcessor);
			this.log('afterChartProcessor', afterCommentProcessor, afterChartProcessor);
 
		}
 
		return afterChartProcessor;
	}
	trimEnding(markdown: string, options: Options): string {
 
		const input = markdown + '\n';
 
		let m;
		if ((m = new RegExp(options.separator, 'gmi').exec(input)) !== null) {
			const [match] = m;
 
			Iif (input.endsWith(match)) {
				return input.substring(0, input.lastIndexOf(match));
			}
		}
 
		if ((m = new RegExp(options.verticalSeparator, 'gmi').exec(input)) !== null) {
			const [match] = m;
 
			Iif (input.endsWith(match)) {
				return input.substring(0, input.lastIndexOf(match));
			}
		}
 
		return markdown;
	}
 
	log(name: string, before: string, after: string) {
		Iif (before != after) {
			console.log(`${name}: ${after}`);
		}
	}
}