Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle template starting with empty line #2

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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