Skip to content

Commit ddbc47a

Browse files
committed
fix: Fix trim bug in trimCode
1 parent f81eb1c commit ddbc47a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/bin/readme-assertions.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import _ from 'lodash';
77
import glob from 'glob';
88

99
const trimCode = (code) => {
10-
let lines = code.trim().split('\n');
10+
let lines = code.replace(/^\n/, '').trimEnd().split('\n');
1111

12-
const indendation = lines[lines.length - 1].match(/^\s+/);
12+
const firsLineIndentation = lines[0].match(/^\s+/);
13+
const lastLineIndentation = lines[lines.length - 1].match(/^\s+/);
1314

14-
const indentSize = indendation ? indendation[0].length : 0;
15+
const firstIndentSize = firsLineIndentation ? firsLineIndentation[0].length : 0;
16+
const lastIndentSize = lastLineIndentation ? lastLineIndentation[0].length : 0;
1517

1618
lines = lines.map((line, index) => {
1719
if (index === 0) {
18-
return line;
20+
return line.slice(Math.min(firstIndentSize, lastIndentSize));
1921
}
2022

21-
return line.slice(indentSize);
23+
return line.slice(lastIndentSize);
2224
});
2325

2426
return lines.join('\n');

0 commit comments

Comments
 (0)