| 
									
										
										
										
											2025-02-22 12:45:21 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Reads the level of indentation of the first line and trims the identation for all the text by that amount. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For example, for: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```json
 | 
					
						
							|  |  |  |  *          { | 
					
						
							|  |  |  |  *              "hello": "world" | 
					
						
							|  |  |  |  *          } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * it results in: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```json
 | 
					
						
							|  |  |  |  * { | 
					
						
							|  |  |  |  *     "hello": "world" | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is meant to be used as a template string, where it allows the indentation of the template without affecting whitespace changes. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @example const html = trimIndentation`\
 | 
					
						
							|  |  |  |  *           <h1>Heading 1</h1> | 
					
						
							|  |  |  |  *           <h2>Heading 2</h2> | 
					
						
							|  |  |  |  *           <h3>Heading 3</h3> | 
					
						
							|  |  |  |  *           <h4>Heading 4</h4> | 
					
						
							|  |  |  |  *           <h5>Heading 5</h5> | 
					
						
							|  |  |  |  *           <h6>Heading 6</h6> | 
					
						
							|  |  |  |  *       `;
 | 
					
						
							|  |  |  |  * @param strings | 
					
						
							|  |  |  |  * @returns | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-12-22 15:45:54 +02:00
										 |  |  | export function trimIndentation(strings: TemplateStringsArray) { | 
					
						
							| 
									
										
										
										
											2024-12-17 23:08:17 +02:00
										 |  |  |     const str = strings.toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Count the number of spaces on the first line.
 | 
					
						
							|  |  |  |     let numSpaces = 0; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     while (str.charAt(numSpaces) == " " && numSpaces < str.length) { | 
					
						
							| 
									
										
										
										
											2024-12-17 23:08:17 +02:00
										 |  |  |         numSpaces++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-22 15:45:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Trim the indentation of the first line in all the lines.
 | 
					
						
							| 
									
										
										
										
											2024-12-17 23:08:17 +02:00
										 |  |  |     const lines = str.split("\n"); | 
					
						
							| 
									
										
										
										
											2024-12-22 15:45:54 +02:00
										 |  |  |     const output = []; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     for (let i = 0; i < lines.length; i++) { | 
					
						
							| 
									
										
										
										
											2024-12-17 23:08:17 +02:00
										 |  |  |         let numSpacesLine = 0; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         while (str.charAt(numSpacesLine) == " " && numSpacesLine < str.length) { | 
					
						
							| 
									
										
										
										
											2024-12-17 23:08:17 +02:00
										 |  |  |             numSpacesLine++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         output.push(lines[i].substring(numSpacesLine)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return output.join("\n"); | 
					
						
							| 
									
										
										
										
											2024-12-22 15:45:54 +02:00
										 |  |  | } |