/** * 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`\ *

Heading 1

*

Heading 2

*

Heading 3

*

Heading 4

*
Heading 5
*
Heading 6
* `; * @param strings * @returns */ export function trimIndentation(strings: TemplateStringsArray) { const str = strings.toString(); // Count the number of spaces on the first line. let numSpaces = 0; while (str.charAt(numSpaces) == " " && numSpaces < str.length) { numSpaces++; } // Trim the indentation of the first line in all the lines. const lines = str.split("\n"); const output = []; for (let i = 0; i < lines.length; i++) { let numSpacesLine = 0; while (str.charAt(numSpacesLine) == " " && numSpacesLine < str.length) { numSpacesLine++; } output.push(lines[i].substring(numSpacesLine)); } return output.join("\n"); }