Skip to content

Commit 396ff8e

Browse files
committed
fix:修正TCVB解析代码块
1 parent 8bf4034 commit 396ff8e

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

src/cvbManager.ts

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -382,46 +382,80 @@ export class TCVB
382382
));
383383
}
384384

385+
// 辅助方法:剥离 Markdown 代码块外部包裹的 ``` 标记
386+
private RemoveMarkdownCodeBlock(strContent: string) : string
387+
{
388+
let strTrimmedContent: string = strContent.trim();
389+
const arrLines: string[] = strTrimmedContent.split('\n');
390+
391+
if (arrLines.length >= 2)
392+
{
393+
const strFirstLine: string = arrLines[0].trim();
394+
const strLastLine: string = arrLines[arrLines.length - 1].trim();
395+
396+
// 检查第一行和最后一行是否为代码块标记
397+
if (strFirstLine.startsWith("```") && strLastLine.startsWith("```"))
398+
{
399+
// 去除第一行和最后一行后,重新拼接内容
400+
arrLines.shift();
401+
arrLines.pop();
402+
strTrimmedContent = arrLines.join('\n').trim();
403+
}
404+
}
405+
406+
return strTrimmedContent;
407+
}
408+
385409
// 辅助方法:解析操作正文中的各个段落(段落标记格式为 "## 段落名称")
386410
private parseSections(strContent: string, arrExpectedSections: string[]) : Record<string, string>
387411
{
388-
const recResult: Record<string, string> = { };
389-
let strCurrentSection: string | null = null;
390-
const arrBuffer: string[] = [ ];
391-
const arrLines: string[] = strContent.split('\n');
392-
for (const strLine of arrLines)
393-
{
394-
const arrSectionMatch = strLine.match(/^## ([A-Z_]+)/);
395-
if (arrSectionMatch)
412+
const recResult: Record<string, string> = { };
413+
let strCurrentSection: string | null = null;
414+
const arrBuffer: string[] = [ ];
415+
const arrLines: string[] = strContent.split('\n');
416+
417+
for (const strLine of arrLines)
396418
{
397-
if (strCurrentSection)
398-
{
399-
recResult[strCurrentSection] = arrBuffer.join('\n').trim();
400-
arrBuffer.length = 0;
401-
}
402-
strCurrentSection = arrSectionMatch[1];
403-
if (arrExpectedSections.indexOf(strCurrentSection) === -1)
404-
{
405-
throw new Error(`意外的段落: ${strCurrentSection}`);
406-
}
419+
const arrSectionMatch = strLine.match(/^## ([A-Z_]+)/);
420+
421+
if (arrSectionMatch)
422+
{
423+
if (strCurrentSection)
424+
{
425+
// 拼接当前段落内容,并剥离 Markdown 代码块包裹的 ``` 标记
426+
recResult[strCurrentSection] = this.RemoveMarkdownCodeBlock(arrBuffer.join('\n').trim());
427+
arrBuffer.length = 0;
428+
}
429+
430+
strCurrentSection = arrSectionMatch[1];
431+
432+
if (arrExpectedSections.indexOf(strCurrentSection) === -1)
433+
{
434+
throw new Error(`意外的段落: ${strCurrentSection}`);
435+
}
436+
}
437+
else if (strCurrentSection)
438+
{
439+
arrBuffer.push(strLine);
440+
}
407441
}
408-
else if (strCurrentSection)
409-
{
410-
arrBuffer.push(strLine);
442+
443+
// 处理最后一个段落
444+
if (strCurrentSection)
445+
{
446+
recResult[strCurrentSection] = this.RemoveMarkdownCodeBlock(arrBuffer.join('\n').trim());
411447
}
412-
}
413-
if (strCurrentSection)
414-
{
415-
recResult[strCurrentSection] = arrBuffer.join('\n').trim();
416-
}
417-
for (const strSection of arrExpectedSections)
418-
{
419-
if (!(strSection in recResult))
448+
449+
// 检查是否缺少必需的段落
450+
for (const strSection of arrExpectedSections)
420451
{
421-
throw new Error(`缺失必需的段落: ${strSection}`);
452+
if (!(strSection in recResult))
453+
{
454+
throw new Error(`缺失必需的段落: ${strSection}`);
455+
}
422456
}
423-
}
424-
return recResult;
457+
458+
return recResult;
425459
}
426460

427461
public getOperations() : TcvbOperation[]

0 commit comments

Comments
 (0)