Skip to content

Commit

Permalink
Handle template starting with empty line
Browse files Browse the repository at this point in the history
  • Loading branch information
jvasseur committed Aug 29, 2022
1 parent d0205cf commit 5c7925e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ test('It reindent placeholders', () => {
`).toBe('<test>\n test\n value\n</test>');
});

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'];

Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 5c7925e

Please sign in to comment.