fix: more section spliting simplification

This commit is contained in:
Roberto Fernández 2020-06-30 08:18:55 +02:00
parent 01910e0622
commit 5551d47bba
3 changed files with 15 additions and 38 deletions

View File

@ -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);

View File

@ -75,6 +75,7 @@ export class OperationModel implements IMenuItem {
security: SecurityRequirementModel[];
extensions: Record<string, any>;
isCallback: boolean;
topMargin: boolean;
constructor(
private parser: OpenAPIParser,

View File

@ -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) {