diff --git a/src/services/MenuBuilder.ts b/src/services/MenuBuilder.ts index dd1104d1..e4b026ff 100644 --- a/src/services/MenuBuilder.ts +++ b/src/services/MenuBuilder.ts @@ -11,7 +11,6 @@ import { SECURITY_DEFINITIONS_COMPONENT_NAME, setSecuritySchemePrefix, JsonPointer, - extractContent, } from '../utils'; import { MarkdownRenderer } from './MarkdownRenderer'; import { GroupModel, OperationModel } from './models'; @@ -54,35 +53,29 @@ export class MenuBuilder { const items: ContentItemModel[] = []; const tagsMap = MenuBuilder.getTagsWithOperations(spec); - let sectionsBefore = ''; - let sectionsAfter = ''; + const mdHeadings = MenuBuilder.addMarkdownItems( + spec.info.description || '', + undefined, + 1, + options, + ); + const mdHeadingsBefore = mdHeadings.filter(h => !options.sectionsAtTheEnd.includes(h.name)); + const mdHeadingsAfter = mdHeadings.filter(h => options.sectionsAtTheEnd.includes(h.name)); - new MarkdownRenderer(options).extractHeadings(spec.info.description || '').forEach(function(h) { - if (options.sectionsAtTheEnd.includes(h.name)) { - sectionsAfter += extractContent(spec.info.description || '', h.name); - } else { - sectionsBefore += extractContent(spec.info.description || '', h.name); - } - }); - - items.push(...MenuBuilder.addMarkdownItems(sectionsBefore, undefined, 1, options)); + items.push(...mdHeadingsBefore); if (spec['x-tagGroups'] && spec['x-tagGroups'].length > 0) { items.push( ...MenuBuilder.getTagGroupsItems(parser, undefined, spec['x-tagGroups'], tagsMap, options), ); + + if (mdHeadingsAfter.length > 0) { + mdHeadingsAfter[0].topMargin = true; + } } else { items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options)); } - items.push( - ...MenuBuilder.addMarkdownItems( - sectionsAfter, - undefined, - 1, - options, - spec['x-tagGroups'] && spec['x-tagGroups'].length > 0, // If tagGroups, add topMargin to side menu item - ), - ); + items.push(...mdHeadingsAfter); return items; } @@ -96,7 +89,6 @@ export class MenuBuilder { parent: GroupModel | undefined, initialDepth: number, options: RedocNormalizedOptions, - topMargin: boolean = false, ): ContentItemModel[] { const renderer = new MarkdownRenderer(options); const headings = renderer.extractHeadings(description || ''); @@ -111,10 +103,6 @@ export class MenuBuilder { const mapHeadingsDeep = (_parent, items, depth = 1) => items.map(heading => { const group = new GroupModel('section', heading, _parent); - if (topMargin) { - group.topMargin = true; - topMargin = false; - } group.depth = depth; if (heading.items) { group.items = mapHeadingsDeep(group, heading.items, depth + 1); diff --git a/src/services/models/Operation.ts b/src/services/models/Operation.ts index 2bb63a01..08502aa3 100644 --- a/src/services/models/Operation.ts +++ b/src/services/models/Operation.ts @@ -75,6 +75,7 @@ export class OperationModel implements IMenuItem { security: SecurityRequirementModel[]; extensions: Record; isCallback: boolean; + topMargin: boolean; constructor( private parser: OpenAPIParser, diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 9d318ad3..d84217a9 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -82,18 +82,6 @@ export function appendToMdHeading(md: string, heading: string, content: string) } } -export function extractContent(md: string, heading: string): string { - // return the content of section with given heading - const testRegex = new RegExp(`(^|\\n)#\\s?${heading}\\s*\\n`, 'i'); - const replaceRegex = new RegExp(`((\\n|^)#\\s*${heading}\\s*(\\n|$)(?:.|\\n)*?)(?=\\n#|$)`, 'i'); - if (testRegex.test(md)) { - const extractedContent = replaceRegex.exec(md); - return extractedContent != null ? extractedContent[0] : ''; - } - - return ''; -} - // credits https://stackoverflow.com/a/46973278/1749888 export const mergeObjects = (target: any, ...sources: any[]): any => { if (!sources.length) {