All files / src/processors excalidrawProcessor.ts

85.71% Statements 12/14
83.33% Branches 5/6
100% Functions 4/4
84.61% Lines 11/13

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 345x     5x 71x         71x       71x     865x 863x           2x 2x   2x       2x      
import { Notice } from 'obsidian';
import { ObsidianUtils } from '../obsidianUtils';
 
export class ExcalidrawProcessor {
	private excalidrawImageRegex = /!\[\[(.*\.excalidraw)\|?([^\]]*)??\]\]\s?(<!--.*-->)?/i;
 
	private utils: ObsidianUtils;
 
	constructor(utils: ObsidianUtils) {
		this.utils = utils;
	}
 
	process(markdown: string) {
		return markdown
			.split('\n')
			.map(line => {
				if (this.excalidrawImageRegex.test(line)) return this.transformLine(line);
				return line;
			})
			.join('\n');
	}
 
	private transformLine(line: string) {
		const [, image, ext, comment] = this.excalidrawImageRegex.exec(line);
		const imgFile = this.utils.findFile(image);
 
		Iif (imgFile === null) {
			new Notice(`Cannot find Image for ${image}. Make sure to activate Auto-export SVG/PNG in Excalidraw Settings.`, 8000);
			return line;
		}
		return `![[${imgFile}${ext == undefined ? '' : '|' + ext}]] ${comment ?? ''}`;
	}
}