diff --git a/index.test.ts b/index.test.ts index 556c502..e8edb9b 100644 --- a/index.test.ts +++ b/index.test.ts @@ -20,6 +20,20 @@ test('It reindent placeholders', () => { `).toBe('\n test\n value\n'); }); +test('It correctly detect indentation when first line is empty', () => { + expect(indent` + + Test + `).toBe('\nTest'); +}); + +test('It correctly detect indentation when last line is empty', () => { + expect(indent` + Test + + `).toBe('Test\n'); +}); + test('Nested indent test', () => { const list = ['First', 'Second']; diff --git a/index.ts b/index.ts index c726a98..c8dc437 100644 --- a/index.ts +++ b/index.ts @@ -12,7 +12,7 @@ const indentTag = (strings: TemplateStringsArray, ... expressions: any[]) => { parts[strings.length - 1] = parts[parts.length - 1].replace(/\n[ \t]*$/, ''); // Dedent using indent from first part - const indent = parts[0].match(/^[ \t]*/)![0]; + const indent = parts[0].match(/^\n*([ \t]*)/)![1]; const deindent = new RegExp(`(^|\n)${indent}`, 'g');