mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-04 04:10:19 +03:00
fix: more section spliting simplification
This commit is contained in:
parent
01910e0622
commit
5551d47bba
|
@ -11,7 +11,6 @@ import {
|
||||||
SECURITY_DEFINITIONS_COMPONENT_NAME,
|
SECURITY_DEFINITIONS_COMPONENT_NAME,
|
||||||
setSecuritySchemePrefix,
|
setSecuritySchemePrefix,
|
||||||
JsonPointer,
|
JsonPointer,
|
||||||
extractContent,
|
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
import { MarkdownRenderer } from './MarkdownRenderer';
|
import { MarkdownRenderer } from './MarkdownRenderer';
|
||||||
import { GroupModel, OperationModel } from './models';
|
import { GroupModel, OperationModel } from './models';
|
||||||
|
@ -54,35 +53,29 @@ export class MenuBuilder {
|
||||||
const items: ContentItemModel[] = [];
|
const items: ContentItemModel[] = [];
|
||||||
const tagsMap = MenuBuilder.getTagsWithOperations(spec);
|
const tagsMap = MenuBuilder.getTagsWithOperations(spec);
|
||||||
|
|
||||||
let sectionsBefore = '';
|
const mdHeadings = MenuBuilder.addMarkdownItems(
|
||||||
let sectionsAfter = '';
|
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) {
|
items.push(...mdHeadingsBefore);
|
||||||
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));
|
|
||||||
if (spec['x-tagGroups'] && spec['x-tagGroups'].length > 0) {
|
if (spec['x-tagGroups'] && spec['x-tagGroups'].length > 0) {
|
||||||
items.push(
|
items.push(
|
||||||
...MenuBuilder.getTagGroupsItems(parser, undefined, spec['x-tagGroups'], tagsMap, options),
|
...MenuBuilder.getTagGroupsItems(parser, undefined, spec['x-tagGroups'], tagsMap, options),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (mdHeadingsAfter.length > 0) {
|
||||||
|
mdHeadingsAfter[0].topMargin = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
|
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
items.push(
|
items.push(...mdHeadingsAfter);
|
||||||
...MenuBuilder.addMarkdownItems(
|
|
||||||
sectionsAfter,
|
|
||||||
undefined,
|
|
||||||
1,
|
|
||||||
options,
|
|
||||||
spec['x-tagGroups'] && spec['x-tagGroups'].length > 0, // If tagGroups, add topMargin to side menu item
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
@ -96,7 +89,6 @@ export class MenuBuilder {
|
||||||
parent: GroupModel | undefined,
|
parent: GroupModel | undefined,
|
||||||
initialDepth: number,
|
initialDepth: number,
|
||||||
options: RedocNormalizedOptions,
|
options: RedocNormalizedOptions,
|
||||||
topMargin: boolean = false,
|
|
||||||
): ContentItemModel[] {
|
): ContentItemModel[] {
|
||||||
const renderer = new MarkdownRenderer(options);
|
const renderer = new MarkdownRenderer(options);
|
||||||
const headings = renderer.extractHeadings(description || '');
|
const headings = renderer.extractHeadings(description || '');
|
||||||
|
@ -111,10 +103,6 @@ export class MenuBuilder {
|
||||||
const mapHeadingsDeep = (_parent, items, depth = 1) =>
|
const mapHeadingsDeep = (_parent, items, depth = 1) =>
|
||||||
items.map(heading => {
|
items.map(heading => {
|
||||||
const group = new GroupModel('section', heading, _parent);
|
const group = new GroupModel('section', heading, _parent);
|
||||||
if (topMargin) {
|
|
||||||
group.topMargin = true;
|
|
||||||
topMargin = false;
|
|
||||||
}
|
|
||||||
group.depth = depth;
|
group.depth = depth;
|
||||||
if (heading.items) {
|
if (heading.items) {
|
||||||
group.items = mapHeadingsDeep(group, heading.items, depth + 1);
|
group.items = mapHeadingsDeep(group, heading.items, depth + 1);
|
||||||
|
|
|
@ -75,6 +75,7 @@ export class OperationModel implements IMenuItem {
|
||||||
security: SecurityRequirementModel[];
|
security: SecurityRequirementModel[];
|
||||||
extensions: Record<string, any>;
|
extensions: Record<string, any>;
|
||||||
isCallback: boolean;
|
isCallback: boolean;
|
||||||
|
topMargin: boolean;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private parser: OpenAPIParser,
|
private parser: OpenAPIParser,
|
||||||
|
|
|
@ -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
|
// credits https://stackoverflow.com/a/46973278/1749888
|
||||||
export const mergeObjects = (target: any, ...sources: any[]): any => {
|
export const mergeObjects = (target: any, ...sources: any[]): any => {
|
||||||
if (!sources.length) {
|
if (!sources.length) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user