File tree Expand file tree Collapse file tree 4 files changed +45
-3
lines changed Expand file tree Collapse file tree 4 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ export class MarkdownRenderer {
6262 parentId ?: string ,
6363 ) : MarkdownHeading {
6464 name = unescapeHTMLChars ( name ) ;
65- const item = {
65+ const item : MarkdownHeading = {
6666 id : parentId ? `${ parentId } /${ safeSlugify ( name ) } ` : `section/${ safeSlugify ( name ) } ` ,
6767 name,
6868 level,
@@ -87,7 +87,7 @@ export class MarkdownRenderer {
8787 attachHeadingsDescriptions ( rawText : string ) {
8888 const buildRegexp = ( heading : MarkdownHeading ) => {
8989 return new RegExp (
90- `##?\\s+${ heading . name . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) } \s*(\n|\r\n)` ,
90+ `##?\\s+${ heading . name . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) } \s*(\n|\r\n|$|\s* )` ,
9191 ) ;
9292 } ;
9393
Original file line number Diff line number Diff line change @@ -97,4 +97,22 @@ describe('Markdown renderer', () => {
9797 expect ( part . component ) . toBe ( TestComponent ) ;
9898 expect ( part . props ) . toEqual ( { children : ' Test Test ' } ) ;
9999 } ) ;
100+
101+ test ( 'should properly extract title from text' , ( ) => {
102+ const rawTexts = [ 'text before\n# Test' , 'text before\n # Test' , 'text before\n# Test\n' ] ;
103+ rawTexts . forEach ( text => {
104+ const headings = renderer . extractHeadings ( text ) ;
105+ expect ( headings ) . toHaveLength ( 1 ) ;
106+ expect ( headings [ 0 ] . name ) . toEqual ( 'Test' ) ;
107+ expect ( headings [ 0 ] . description ) . toEqual ( '' ) ;
108+ } ) ;
109+
110+ const rawTexts2 = [ '# Test \n text after' , '# Test \ntext after' ] ;
111+ rawTexts2 . forEach ( text => {
112+ const headings = renderer . extractHeadings ( text ) ;
113+ expect ( headings ) . toHaveLength ( 1 ) ;
114+ expect ( headings [ 0 ] . name ) . toEqual ( 'Test' ) ;
115+ expect ( headings [ 0 ] . description ) . toEqual ( 'text after' ) ;
116+ } ) ;
117+ } ) ;
100118} ) ;
Original file line number Diff line number Diff line change @@ -47,6 +47,30 @@ describe('Models', () => {
4747 expect ( info . summary ) . toEqual ( 'Test summary\nsome text\n## Heading\n test' ) ;
4848 } ) ;
4949
50+ test ( 'should correctly populate description when 2nd line is started by white space' , ( ) => {
51+ parser . spec = {
52+ openapi : '3.0.0' ,
53+ info : {
54+ description : 'text before\n # Test' ,
55+ } ,
56+ } as any ;
57+
58+ const info = new ApiInfoModel ( parser ) ;
59+ expect ( info . description ) . toEqual ( 'text before\n' ) ;
60+ } ) ;
61+
62+ test ( 'should correctly populate description when 2nd line is only white space' , ( ) => {
63+ parser . spec = {
64+ openapi : '3.0.0' ,
65+ info : {
66+ description : 'text before\n \n # Test' ,
67+ } ,
68+ } as any ;
69+
70+ const info = new ApiInfoModel ( parser ) ;
71+ expect ( info . description ) . toEqual ( 'text before\n' ) ;
72+ } ) ;
73+
5074 test ( 'should correctly populate license identifier' , ( ) => {
5175 parser . spec = {
5276 openapi : '3.1.0' ,
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ export class ApiInfoModel implements OpenAPIInfo {
2424 this . description = parser . spec . info . description || '' ;
2525 this . summary = parser . spec . info . summary || '' ;
2626
27- const firstHeadingLinePos = this . description . search ( / ^ # # ? \s + / m) ;
27+ const firstHeadingLinePos = this . description . search ( / ^ \s * # # ? \s + / m) ;
2828 if ( firstHeadingLinePos > - 1 ) {
2929 this . description = this . description . substring ( 0 , firstHeadingLinePos ) ;
3030 }
You can’t perform that action at this time.
0 commit comments