From 40ebfd2d63758b37665e2e4447732f671811e2a5 Mon Sep 17 00:00:00 2001 From: Mateusz Date: Tue, 9 Aug 2022 15:47:43 +0200 Subject: [PATCH] fix: duplication of title (#2119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateusz RomaƄski --- src/services/MarkdownRenderer.ts | 4 ++-- .../__tests__/MarkdownRenderer.test.ts | 18 ++++++++++++++ src/services/__tests__/models/ApiInfo.test.ts | 24 +++++++++++++++++++ src/services/models/ApiInfo.ts | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/services/MarkdownRenderer.ts b/src/services/MarkdownRenderer.ts index 4764e129..7ba6752f 100644 --- a/src/services/MarkdownRenderer.ts +++ b/src/services/MarkdownRenderer.ts @@ -62,7 +62,7 @@ export class MarkdownRenderer { parentId?: string, ): MarkdownHeading { name = unescapeHTMLChars(name); - const item = { + const item: MarkdownHeading = { id: parentId ? `${parentId}/${safeSlugify(name)}` : `section/${safeSlugify(name)}`, name, level, @@ -87,7 +87,7 @@ export class MarkdownRenderer { attachHeadingsDescriptions(rawText: string) { const buildRegexp = (heading: MarkdownHeading) => { return new RegExp( - `##?\\s+${heading.name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\s*(\n|\r\n)`, + `##?\\s+${heading.name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\s*(\n|\r\n|$|\s*)`, ); }; diff --git a/src/services/__tests__/MarkdownRenderer.test.ts b/src/services/__tests__/MarkdownRenderer.test.ts index 669a8fb0..777822c0 100644 --- a/src/services/__tests__/MarkdownRenderer.test.ts +++ b/src/services/__tests__/MarkdownRenderer.test.ts @@ -97,4 +97,22 @@ describe('Markdown renderer', () => { expect(part.component).toBe(TestComponent); expect(part.props).toEqual({ children: ' Test Test ' }); }); + + test('should properly extract title from text', () => { + const rawTexts = ['text before\n# Test', 'text before\n # Test', 'text before\n# Test\n']; + rawTexts.forEach(text => { + const headings = renderer.extractHeadings(text); + expect(headings).toHaveLength(1); + expect(headings[0].name).toEqual('Test'); + expect(headings[0].description).toEqual(''); + }); + + const rawTexts2 = ['# Test \n text after', '# Test \ntext after']; + rawTexts2.forEach(text => { + const headings = renderer.extractHeadings(text); + expect(headings).toHaveLength(1); + expect(headings[0].name).toEqual('Test'); + expect(headings[0].description).toEqual('text after'); + }); + }); }); diff --git a/src/services/__tests__/models/ApiInfo.test.ts b/src/services/__tests__/models/ApiInfo.test.ts index 4a67139e..867d50e9 100644 --- a/src/services/__tests__/models/ApiInfo.test.ts +++ b/src/services/__tests__/models/ApiInfo.test.ts @@ -47,6 +47,30 @@ describe('Models', () => { expect(info.summary).toEqual('Test summary\nsome text\n## Heading\n test'); }); + test('should correctly populate description when 2nd line is started by white space', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'text before\n # Test', + }, + } as any; + + const info = new ApiInfoModel(parser); + expect(info.description).toEqual('text before\n'); + }); + + test('should correctly populate description when 2nd line is only white space', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'text before\n \n # Test', + }, + } as any; + + const info = new ApiInfoModel(parser); + expect(info.description).toEqual('text before\n'); + }); + test('should correctly populate license identifier', () => { parser.spec = { openapi: '3.1.0', diff --git a/src/services/models/ApiInfo.ts b/src/services/models/ApiInfo.ts index 0b8e347d..fee23150 100644 --- a/src/services/models/ApiInfo.ts +++ b/src/services/models/ApiInfo.ts @@ -24,7 +24,7 @@ export class ApiInfoModel implements OpenAPIInfo { this.description = parser.spec.info.description || ''; this.summary = parser.spec.info.summary || ''; - const firstHeadingLinePos = this.description.search(/^##?\s+/m); + const firstHeadingLinePos = this.description.search(/^\s*##?\s+/m); if (firstHeadingLinePos > -1) { this.description = this.description.substring(0, firstHeadingLinePos); }