All files / src/processors templateProcessor.ts

75% Statements 66/88
26.31% Branches 5/19
77.77% Functions 7/9
75.86% Lines 66/87

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 1725x     5x 5x   5x         71x 71x 71x   71x   71x     71x     71x 71x 71x       76x   76x                                                 76x   76x     104x     105x 4x     4x   4x 4x 4x 4x   4x         4x 4x 4x 4x     4x 4x             101x         76x         4x 4x       4x 4x     4x         4x 4x 4x 4x 4x   4x 4x 4x 4x               4x 4x     4x 1x         1x   1x       1x   1x 1x 1x 1x 1x 1x   4x   4x 1x     1x   4x      
import { CommentParser } from 'src/comment';
import { ObsidianUtils } from 'src/obsidianUtils';
import { Options } from 'src/options';
import { FootnoteProcessor } from './footNoteProcessor';
import { MultipleFileProcessor } from './multipleFileProcessor';
 
export class TemplateProcessor {
 
	private multipleFileProcessor: MultipleFileProcessor;
	private footnoteProcessor: FootnoteProcessor;
 
	private emptySlideCommentRegex = /<!--\s*(?:\.)?slide(?::)?\s*-->/g;
	private templateCommentRegex = /<!--\s*(?:\.)?slide.*(template="\[\[([^\]]+)\]\]"\s*).*-->/;
	private propertyRegex = /:::\s([^\n]+)\s*(.*?:::[^\n]*)/sg;
 
	private slideCommentRegex = /<!--\s*(?:\.)?slide.*-->/;
 
	private optionalRegex = /<%\?.*%>/g;
 
	private utils: ObsidianUtils;
	private parser = new CommentParser();
 
	constructor(utils: ObsidianUtils) {
		this.utils = utils;
		this.multipleFileProcessor = new MultipleFileProcessor(utils);
		this.footnoteProcessor = new FootnoteProcessor();
	}
 
	process(markdown: string, options: Options) {
		let input = markdown;
 
		Iif (options.defaultTemplate != null) {
 
			markdown
				.split(new RegExp(options.separator, 'gmi'))
				.map(slidegroup => {
					return slidegroup
						.split(new RegExp(options.verticalSeparator, 'gmi'))
						.map(slide => {
							if (this.slideCommentRegex.test(slide)) {
								const [slideAnnotation] = this.slideCommentRegex.exec(slide);
								const comment = this.parser.parseLine(slideAnnotation);
								Iif (!comment.hasAttribute('template')) {
									comment.addAttribute('template', options.defaultTemplate, false);
								}
								input = input.split(slide).join(slide.split(slideAnnotation).join(this.parser.commentToString(comment)));
							} else {
								input = input.split(slide).join(`<!-- slide template="${options.defaultTemplate}" -->\n` + slide);
							}
							return slide;
						})
						.join(options.verticalSeparator);
				})
				.join(options.separator);
		}
 
		let output = input;
 
		input
			.split(new RegExp(options.separator, 'gmi'))
			.map(slidegroup => {
				return slidegroup
					.split(new RegExp(options.verticalSeparator, 'gmi'))
					.map(slide => {
						if (this.templateCommentRegex.test(slide)) {
							try {
 
								// eslint-disable-next-line prefer-const
								let [md, notes] = this.extractNotes(slide, options);
 
								let circuitCounter = 0;
								while (this.templateCommentRegex.test(md)) {
									circuitCounter++;
									md = this.transformSlide(md);
 
									Iif (circuitCounter > 9) {
										console.log('WARNING: Circuit in template hierarchy detected!');
										break;
									}
								}
								md = md.replaceAll(this.emptySlideCommentRegex, '');
								md = md.trim();
								md = this.computeVariables(md);
								Iif (notes.length > 0) {
									md += '\n\n' + notes;
								}
								output = output.split(slide).join(md);
								return md;
							} catch (error) {
								console.log('Cannot process template: ' + error);
								return slide;
							}
 
						}
						return slide;
					})
					.join(options.verticalSeparator);
			})
			.join(options.separator);
		return output;
	}
 
	extractNotes(input: string, options: Options): [string, string] {
 
		let noteSeparator = 'note:';
		Iif (options.notesSeparator && options.notesSeparator.length > 0) {
			noteSeparator = options.notesSeparator;
		}
 
		const spliceIdx = input.indexOf(noteSeparator);
		Iif (spliceIdx > 0) {
			return [input.substring(0, spliceIdx), input.substring(spliceIdx)];
		} else {
			return [input, ''];
		}
	}
 
	transformSlide(slide: string) {
		if (this.templateCommentRegex.test(slide)) {
			const [, templateProperty, file] = this.templateCommentRegex.exec(slide);
			let fileWithExtension = file;
			if (!fileWithExtension.endsWith('.md')) {
				fileWithExtension = fileWithExtension + '.md';
			}
			let templateContent = this.utils.parseFile(fileWithExtension, null);
			templateContent = this.multipleFileProcessor.process(templateContent);
			templateContent = templateContent.split('<% content %>').join(slide.replaceAll(templateProperty, ''));
			return templateContent;
		} else E{
			return slide;
		}
	}
 
	computeVariables(slide: string): string {
 
		let result = slide;
		this.propertyRegex.lastIndex = 0;
 
		let m;
		while ((m = this.propertyRegex.exec(slide)) !== null) {
			Iif (m.index === this.propertyRegex.lastIndex) {
				this.propertyRegex.lastIndex++;
			}
 
			// eslint-disable-next-line prefer-const
			let [match, name, content] = m;
 
			Iif (name.includes('<!--')) {
				name = name.substring(0, name.indexOf('<!--'));
			}
 
			Iif (name.trim() == 'block') continue;
 
			content = '::: block\n' + content;
			const optionalName = '<%? ' + name.trim() + ' %>';
			name = '<% ' + name.trim() + ' %>';
			result = result.replaceAll(optionalName, content + '\n' + optionalName);
			result = result.replaceAll(name, content);
			result = result.replaceAll(match, '');
		}
		result = this.footnoteProcessor.transformFootNotes(result);
		//Remove optional template variables
		while ((m = this.optionalRegex.exec(result)) !== null) {
			Iif (m.index === this.optionalRegex.lastIndex) {
				this.optionalRegex.lastIndex++;
			}
			result = result.replaceAll(m[0], '');
		}
		return result;
	}
}