Skip to content

Commit

Permalink
Merge pull request #2 from jvasseur/handle-empty-lines
Browse files Browse the repository at this point in the history
Handle template starting with empty line
  • Loading branch information
jvasseur authored Aug 29, 2022
2 parents 6ac5ac7 + 5d886ac commit 1f6e0e7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ indentation:
- First it treats the start and end of the template to remove the lines
that contains the backticks.
- Then it removes the indentation of each line base on the indentation of the
first line.
first non-empty line.
- Finaly, it indents strings that are passed into the placeholders based of the
indentation preceding them.
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 1f6e0e7

Please sign in to comment.