Skip to content

Commit 6f905bc

Browse files
authored
fix template additional newline bug (#1419)
1 parent 9148dbe commit 6f905bc

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

libraries/botbuilder-lg/src/lgResource.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class LGResource {
5555

5656
const templateNameLine: string = this.buildTemplateNameLine(newTemplateName, parameters);
5757
const newTemplateBody: string = this.convertTemplateBody(templateBody);
58-
const content = `${ templateNameLine }\r\n${ newTemplateBody }`;
58+
const content = `${ templateNameLine }\r\n${ newTemplateBody }\r\n`;
5959
const startLine: number = template.parseTree.start.line - 1;
6060
const stopLine: number = template.parseTree.stop.line - 1;
6161

@@ -79,7 +79,7 @@ export class LGResource {
7979

8080
const templateNameLine: string = this.buildTemplateNameLine(templateName, parameters);
8181
const newTemplateBody: string = this.convertTemplateBody(templateBody);
82-
const newContent = `${ this.content }\r\n${ templateNameLine }\r\n${ newTemplateBody }`;
82+
const newContent = `${ this.content.trimRight() }\r\n\r\n${ templateNameLine }\r\n${ newTemplateBody }\r\n`;
8383

8484
return LGParser.parse(newContent, this.id);
8585
}
@@ -115,15 +115,55 @@ export class LGResource {
115115
throw new Error(`index out of range.`);
116116
}
117117

118-
destList.push(...originList.slice(0, startLine));
118+
destList.push(...this.trimList(originList.slice(0, startLine)));
119119

120-
if (replaceString !== undefined && replaceString.length > 0) {
121-
destList.push(replaceString);
120+
if (stopLine < originList.length - 1) {
121+
// insert at the middle of the content
122+
destList.push('\r\n');
123+
if (replaceString !== undefined && replaceString.length > 0){
124+
destList.push(replaceString);
125+
destList.push('\r\n');
126+
}
127+
128+
destList.push(...this.trimList(originList.slice(stopLine + 1)));
129+
} else {
130+
// insert at the tail of the content
131+
if (replaceString !== undefined && replaceString.length > 0){
132+
destList.push('\r\n');
133+
destList.push(replaceString);
134+
}
122135
}
123136

124-
destList.push(...originList.slice(stopLine + 1));
137+
return this.buildNewLGContent(this.trimList(destList));
138+
}
139+
140+
/**
141+
* trim the newlines at the beginning or at the tail of the array
142+
* @param input input array
143+
*/
144+
private trimList(input: string[]): string[] {
145+
if (input === undefined) {
146+
return undefined;
147+
}
148+
149+
let startIndex = 0;
150+
let endIndex = input.length;
151+
152+
for(let i = 0; i< input.length; i++) {
153+
if (input[i].trim() !== '') {
154+
startIndex = i;
155+
break;
156+
}
157+
}
158+
159+
for(let i = input.length - 1; i >= 0; i--) {
160+
if (input[i].trim() !== '') {
161+
endIndex = i + 1;
162+
break;
163+
}
164+
}
125165

126-
return this.buildNewLGContent(destList);
166+
return input.slice(startIndex, endIndex);
127167
}
128168

129169
private buildNewLGContent(destList: string[]): string {
@@ -133,7 +173,7 @@ export class LGResource {
133173
result = result.concat(currentItem);
134174
if (currentItem.endsWith('\r')) {
135175
result = result.concat('\n');
136-
} else if (i < destList.length - 1) {
176+
} else if (i < destList.length - 1 && !currentItem.endsWith('\r\n')) {
137177
result = result.concat('\r\n');
138178
}
139179
}

0 commit comments

Comments
 (0)