All files / src/processors gridProcessor.ts

95.12% Statements 39/41
60% Branches 3/5
100% Functions 7/7
95.12% Lines 39/41

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  5x   5x 71x 71x 71x     71x   71x     98x     99x 98x   98x 136x 38x   136x   98x 98x   1x           71x       234x 234x     234x 174x     174x   174x 174x     234x 174x 174x     234x       174x 174x 174x       174x 174x     174x 587x     587x 587x     174x      
import { Options } from '../options';
import { Properties } from 'src/transformers';
 
export class GridProcessor {
	private gridBlockRegex = /<\s*grid(?:(?!(<grid|<\/grid>)).)*<\/grid>/gs;
	private gridRegex = /<\s*grid([^>]+)>(.*?)<\/grid>/s;
	private gridPropertiesRegex = /([^=]*)\s*=\s*"([^"]*)"\s*|([^=]*)\s*=\s*'([^']*)'\s*/g;
 
	process(markdown: string, options: Options) {
		let output = markdown;
 
		markdown
			.split(new RegExp(options.separator, 'gmi'))
			.map(slidegroup => {
				return slidegroup
					.split(new RegExp(options.verticalSeparator, 'gmi'))
					.map(slide => {
						if (this.gridBlockRegex.test(slide)) {
							let before = this.transformSlide(slide);
							let after;
							while (after != before) {
								if (after) {
									before = after;
								}
								after = this.transformSlide(before);
							}
							output = output.split(slide).join(after);
							return after;
						}
						return slide;
					})
					.join(options.verticalSeparator);
			})
			.join(options.separator);
 
		return output;
	}
 
	transformSlide(slide: string) {
		const result: Map<string, string> = new Map<string, string>();
		this.gridBlockRegex.lastIndex = 0;
 
		let m;
		while ((m = this.gridBlockRegex.exec(slide)) !== null) {
			Iif (m.index === this.gridBlockRegex.lastIndex) {
				this.gridBlockRegex.lastIndex++;
			}
			const gridTag = m[0];
 
			const [match, attr, inner] = this.gridRegex.exec(gridTag);
			result.set(match, this.transformGrid(attr, inner));
		}
 
		for (const [key, value] of result) {
			if (value) {
				slide = slide.split(key).join(value);
			}
		}
		return slide;
	}
 
	transformGrid(attr: string, inner: string): string {
		const attributes = this.parseAttributes(attr.trim());
		const properties = new Properties(attributes);
		return `<div class="${properties.getClasses()}" style="${properties.getStyles()}" ${properties.getAttributes()}>\n${inner}</div>`;
	}
 
	parseAttributes(attributes: string): Map<string, string> {
		const result: Map<string, string> = new Map<string, string>();
		this.gridPropertiesRegex.lastIndex = 0;
 
		let m;
		while ((m = this.gridPropertiesRegex.exec(attributes)) !== null) {
			Iif (m.index === this.gridPropertiesRegex.lastIndex) {
				this.gridPropertiesRegex.lastIndex++;
			}
			const [, key, value] = m;
			result.set(key, value);
		}
 
		return result;
	}
}