All files / src/processors chartProcessor.ts

84.69% Statements 83/98
70.96% Branches 22/31
100% Functions 5/5
85.56% Lines 83/97

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                                    5x   71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x   71x     71x       73x   73x 70x   3x 3x 1x   2x   2x 2x 2x   2x 2x   2x               2x                           2x 2x 2x   2x   2x     2x 4x     4x   4x 4x     2x   2x 1x 1x   2x 1x 1x   2x 1x   1x 1x     1x 1x     1x     2x 1x   1x 1x     1x 1x     1x     2x 1x   1x       1x       1x   2x 1x   1x       1x     1x 1x     1x 1x     2x             2x   2x 2x                   2x     18x 18x 4x   18x 4x   18x        
import { Options } from "src/options";
 
type ChartObject = {
	data: DataObject
	options: any
}
 
type DataEntry = {
	data: number[],
	label: string,
	backgroundColor?: string
}
 
type DataObject = {
	labels: string[],
	datasets: DataEntry[]
}
 
export class ChartProcessor {
 
	private typeRegex = /type:\s(\w*)/;
	private labelRegex = /labels:\s(.*)/;
	private datasetRegex = /title:\s(.*)\s*data:\s(.*)/g
	private spanGapsRegex = /(spanGaps):\s(.*)/;
	private tensionRegex = /(tension):\s(.*)/;
	private beginAtZeroRegex = /(beginAtZero):\s(.*)/;
	private legendRegex = /(legend):\s(.*)/;
	private legendPositionRegex = /(legendPosition):\s(.*)/;
	private stackedRegex = /(stacked):\s(.*)/;
	private heightRegex = /(height):\s(.*)/;
	private useThemeColorsRegex = /(useThemeColors):\s(.*)/;
 
	private colorMap = ["#4285f4", "#ea4335", "#fbbc05", "#34a853", "#673ab7", "#cccccc", "#777777"]
 
	process(markdown: string, options: Options) {
		return this.transformChart(markdown, options);
	}
 
	transformChart(markdown: string, options: Options): string {
		const startIdx = markdown.indexOf('```chart');
 
		if (startIdx < 0) {
			return markdown;
		} else {
			const endIdx = markdown.indexOf('```', startIdx + 11);
			if (endIdx < 0) {
				return markdown;
			}
			const colorMap=[...this.colorMap];
 
			const before = markdown.substring(0, startIdx);
			const after = markdown.substring(endIdx + 3);
			const chartMarkup = markdown.substring(startIdx + 8, endIdx);
 
			if (this.typeRegex.test(chartMarkup)) {
				const [, type] = this.typeRegex.exec(chartMarkup);
 
				const chart: ChartObject = {
					data: {
						datasets: [],
						labels: []
					},
					options: { elements: {} }
				};
				
				Iif (this.useThemeColorsRegex.test(chartMarkup)){
					const [, , value] = this.useThemeColorsRegex.exec(chartMarkup);
 
					Iif (value.trim() == "true"){
						for (let i=0;i<7;i++){
							const style=getComputedStyle(document.body).getPropertyValue("--chart-color-"+(i+1));
							Iif (style != "") {
								colorMap[i]=style;
							}
							
						}
					}
 
				}
				if (this.labelRegex.test(chartMarkup)) {
					const [, labels] = this.labelRegex.exec(chartMarkup);
					chart.data.labels = parseLabels(labels);
 
					this.datasetRegex.lastIndex = 0;
 
					let i = 0;
 
					let m;
					while ((m = this.datasetRegex.exec(chartMarkup)) !== null) {
						Iif (m.index === this.datasetRegex.lastIndex) {
							this.datasetRegex.lastIndex++;
						}
						const [, title, data] = m;
 
						chart.data.datasets.push({ data: JSON.parse(data), label: title, backgroundColor: colorMap[i] })
						i++;
					}
 
					chart.options.elements[type] = {}
 
					if (this.spanGapsRegex.test(chartMarkup)) {
						const [, key, value] = this.spanGapsRegex.exec(chartMarkup);
						chart.options.elements[type][key] = JSON.parse(value);
					}
					if (this.tensionRegex.test(chartMarkup)) {
						const [, key, value] = this.tensionRegex.exec(chartMarkup);
						chart.options.elements[type][key] = JSON.parse(value);
					}
					if (this.beginAtZeroRegex.test(chartMarkup)) {
						const [, key, value] = this.beginAtZeroRegex.exec(chartMarkup);
 
						if (!chart.options.scales) {
							chart.options.scales = {}
						}
 
						if (!chart.options.scales.y) {
							chart.options.scales.y = {}
						}
 
						chart.options.scales.y[key] = JSON.parse(value);
 
					}
					if (this.legendRegex.test(chartMarkup)) {
						const [, , value] = this.legendRegex.exec(chartMarkup);
 
						if (!chart.options.plugins) {
							chart.options.plugins = {}
						}
 
						if (!chart.options.plugins.legend) {
							chart.options.plugins.legend = {}
						}
 
						chart.options.plugins.legend.display = JSON.parse(value);
					}
 
					if (this.legendPositionRegex.test(chartMarkup)) {
						const [, , value] = this.legendPositionRegex.exec(chartMarkup);
 
						Iif (!chart.options.plugins) {
							chart.options.plugins = {}
						}
 
						Iif (!chart.options.plugins.legend) {
							chart.options.plugins.legend = {}
						}
 
						chart.options.plugins.legend.position = value;
					}
					if (this.stackedRegex.test(chartMarkup)) {
						const [, key, value] = this.stackedRegex.exec(chartMarkup);
 
						Iif (!chart.options.scales) {
							chart.options.scales = {}
						}
 
						Iif (!chart.options.scales.y) {
							chart.options.scales.y = {}
						}
						if (!chart.options.scales.x) {
							chart.options.scales.x = {}
						}
 
						chart.options.scales.x[key] = JSON.parse(value);
						chart.options.scales.y[key] = JSON.parse(value);
 
					}
					Iif (this.heightRegex.test(chartMarkup)){
						const [, , value] = this.heightRegex.exec(chartMarkup);
						options.height = +value;
 
					}
 
 
					const canvas = `<canvas style="max-height:${options.height}px" data-chart="${type}" >\n<!--\n${JSON.stringify(chart)}-->\n</canvas>`
 
					const result = before.trimEnd() + '\n' + canvas + '\n' + after.trimStart();
					return this.transformChart(result, options);
 
				}
			}
			return markdown;
 
		}
	}
}
function parseLabels(labels: string): string[] {
	return labels.substring(1, labels.length - 1)
		.split(',')
		.map((label) => {
			let value = label.trim();
			if (value.startsWith("'") || value.startsWith('"')) {
				value = value.substring(1);
			}
			if (value.endsWith("'") || value.endsWith('"')) {
				value = value.substring(0, value.length - 1);
			}
			return `${value.trim()}`;
		});
}