All files / src/processors multipleFileProcessor.ts

93.93% Statements 31/33
83.33% Branches 10/12
100% Functions 5/5
93.93% Lines 31/33

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    5x     142x 142x 142x     142x       82x     771x 4x   767x             4x 4x 1x     4x 4x   4x 1x 1x 1x     4x   4x 2x     2x   2x 2x     2x                   4x 2x     2x 2x 2x   2x      
import { ObsidianUtils } from '../obsidianUtils';
 
export class MultipleFileProcessor {
	private utils: ObsidianUtils;
 
	private regex = /!\[\[(.*)\]\]/gm;
	private obsidianImageRegex = /!\[\[(.*(?:jpg|png|jpeg|gif|bmp|webp|svg))\s*\|?\s*([^\]]*)??\]\]\s?(<!--.*-->)?/i;
	private excalidrawRegex = /(.*\.excalidraw)/i;
 
	constructor(utils: ObsidianUtils) {
		this.utils = utils;
	}
 
	process(markdown: string): string {
		return markdown
			.split('\n')
			.map((line, index) => {
				if (this.regex.test(line) && !this.obsidianImageRegex.test(line)) {
					return this.transformLine(line);
				}
				return line;
			})
			.join('\n');
	}
 
	private transformLine(line: string) {
 
		let comment = '';
		if (line.includes('<!--')) {
			comment = line.substring(line.indexOf('<!--'));
		}
 
		let link: string = line.replace('![[', '').replace(']]', '').replace(comment, '').trim();
		let header: string = null;
 
		if (link.includes('#')) {
			const split = link.split('#');
			link = split[0];
			header = split[1];
		}
 
		const fileName = this.getMarkdownFile(link);
 
		if (fileName === null) {
			return line;
		}
 
		const content = this.utils.parseFile(fileName, header);
 
		if (content) {
			Iif (comment.length > 0) {
				return this.process(content + comment);
			} else {
				return this.process(content);
			}
 
		} else E{
			return line;
		}
 
	}
 
	private getMarkdownFile(line: string) {
		if (this.excalidrawRegex.test(line)) {
			return null; // Do not import excalidraw files
		}
 
		let file = line;
		if (!line.toLowerCase().endsWith('.md')) {
			file = file + '.md';
		}
		return file;
	}
}