All files / src/processors calloutProcessor.ts

84.88% Statements 73/86
46.96% Branches 31/66
100% Functions 6/6
84.14% Lines 69/82

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 1805x 71x       71x 71x   71x 71x 71x 738x   738x 6x 6x 732x 6x   6x 96x 96x   6x 6x     738x 727x     71x         6x   6x 5x   5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x     5x 5x 5x     1x 1x 1x       6x     5x   5x                     1x                       1x               1x   1x     1x           5x 1x   4x         5x   5x                         1x                       1x             1x       1x     1x            
export class CalloutProcessor {
	private regex = />\s\[!([^\]]+)\]-* *(.*)/;
 
	process(markdown: string) {
 
		const lineArray = markdown.split('\n');
		const outArray = [];
 
		let startIdx = -1;
		let add = true;
		for (let i = 0; i < lineArray.length; i++) {
			const line = lineArray[i];
 
			if (line.trim().startsWith('>') && startIdx == -1) {
				startIdx = i;
				add = false;
			} else if (!line.trim().startsWith('>') && startIdx > -1) {
				const content = this.transformBlock(lineArray, startIdx, i - 1);
 
				for (let index = 0; index < content.length; index++) {
					const contentLine = content[index];
					outArray.push(contentLine);
				}
				startIdx = -1;
				add = true;
			}
 
			if (add) {
				outArray.push(lineArray[i]);
			}
		}
		return outArray.join('\n');
	}
 
	transformBlock(lines: string[], start: number, end: number) {
 
		const result: string[] = [];
 
		if (this.regex.test(lines[start])) {
			const [, type, titleLine] = this.regex.exec(lines[start]);
 
			const icon = this.iconFrom(type);
			const title = this.titleFrom(type, titleLine);
			const color = this.colorFrom(type);
 
			result.push(`<div class="callout ${color}">`);
			result.push('<div class="callout-title">');
			result.push('<div class="callout-icon">');
			result.push('');
			result.push(icon);
			result.push('');
			result.push('</div>');
			result.push('<div class="callout-title-inner">');
			result.push('');
			result.push(title);
			result.push('');
			result.push('</div>');
			result.push('</div>');
 
			result.push('<div class="callout-content">');
			for (let i = start + 1; i <= end; i++) {
				result.push('');
				const line = lines[i].trim().substring(1).trim();
				result.push(line);
 
			}
			result.push('');
			result.push('</div>');
			result.push('</div>');
 
		} else {
			for (let i = start; i <= end; i++) {
				const line = lines[i];
				result.push(line);
			}
		}
 
		return result;
	}
	colorFrom(type: string) {
		const input = type.toLowerCase();
 
		switch (input) {
 
			case 'abstract':
			case 'summary':
			case 'tldr':
			case 'info':
				return 'callout-color1';
			case 'todo':
			case 'tip':
			case 'hint':
			case 'important':
				return 'callout-color2'
			case 'success':
			case 'check':
			case 'done':
				return 'callout-color3'
			case 'question':
			case 'help':
			case 'faq':
				return 'callout-color4'
			case 'warning':
			case 'caution':
			case 'attention':
				return 'callout-color5'
			case 'failure':
			case 'fail':
			case 'missing':
				return 'callout-color6'
			case 'danger':
			case 'error':
			case 'bug':
				return 'callout-color7'
			case 'example':
				return 'callout-color8'
			case 'quote':
			case 'cite':
				return 'callout-color9'
			default:
				return 'callout-color-default'
		}
	}
	titleFrom(icon: string, titleLine: string): string {
		if (titleLine) {
			return titleLine;
		} else {
			return icon[0].toUpperCase() + icon.substring(1).toLowerCase();
		}
	}
 
	iconFrom(type: string): string {
		const input = type.toLowerCase();
 
		switch (input) {
 
			case 'abstract':
			case 'summary':
			case 'tldr':
				return ':fas_clipboard-list:';
			case 'info':
				return ':fas_info-circle:'
			case 'todo':
				return ':fas_check-circle:'
			case 'tip':
			case 'hint':
			case 'important':
				return ':fas_fire-alt:'
			case 'success':
			case 'check':
			case 'done':
				return ':fas_check:'
			case 'question':
			case 'help':
			case 'faq':
				return ':fas_question-circle:'
			case 'warning':
			case 'caution':
			case 'attention':
				return ':fas_exclamation-triangle:'
			case 'failure':
			case 'fail':
			case 'missing':
				return ':fas_times:'
			case 'danger':
			case 'error':
				return ':fas_bolt:'
			case 'bug':
				return ':fas_bug:'
			case 'example':
				return ':fas_list:'
			case 'quote':
			case 'cite':
				return ':fas_quote-left:'
			default:
				return ':fas_pencil-alt:'
		}
	}
}