fix: fix subsections ids so subsections with the same name are allowed

fixes #529
This commit is contained in:
Roman Hotsiy 2018-06-20 19:29:46 +03:00
parent c8a5f36f17
commit 4c76d52cc6
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
2 changed files with 20 additions and 6 deletions

View File

@ -41,9 +41,13 @@ export class MarkdownRenderer {
this.headingEnhanceRenderer.heading = this.headingRule;
}
saveHeading(name: string, container: MarkdownHeading[] = this.headings): MarkdownHeading {
saveHeading(
name: string,
container: MarkdownHeading[] = this.headings,
parentId?: string,
): MarkdownHeading {
const item = {
id: 'section' + '/' + slugify(name),
id: parentId ? `${parentId}/${slugify(name)}` : `section/${slugify(name)}`,
name,
items: [],
};
@ -95,7 +99,11 @@ export class MarkdownRenderer {
`<a class="share-link" href="#${id}"></a>${text}</h${level}>`
);
} else if (level === 2) {
const { id } = this.saveHeading(text, this.currentTopHeading && this.currentTopHeading.items);
const { id } = this.saveHeading(
text,
this.currentTopHeading && this.currentTopHeading.items,
this.currentTopHeading && this.currentTopHeading.id,
);
return (
`<a name="${id}"></a>` +
`<h${level} ${SECTION_ATTR}="${id}" id="${id}">` +

View File

@ -2,6 +2,7 @@ import { action, observable } from 'mobx';
import slugify from 'slugify';
import { OpenAPIExternalDocumentation, OpenAPITag } from '../../types';
import { MarkdownHeading } from '../MarkdownRenderer';
import { ContentItemModel } from '../MenuBuilder';
import { IMenuItem, MenuItemGroupType } from '../MenuStore';
@ -25,13 +26,18 @@ export class GroupModel implements IMenuItem {
depth: number;
//#endregion
constructor(type: MenuItemGroupType, tagOrGroup: OpenAPITag, parent?: GroupModel) {
this.id = type + '/' + slugify(tagOrGroup.name);
constructor(
type: MenuItemGroupType,
tagOrGroup: OpenAPITag | MarkdownHeading,
parent?: GroupModel,
) {
// markdown headings already have ids calculated as they are needed for heading anchors
this.id = (tagOrGroup as MarkdownHeading).id || type + '/' + slugify(tagOrGroup.name);
this.type = type;
this.name = tagOrGroup['x-displayName'] || tagOrGroup.name;
this.description = tagOrGroup.description || '';
this.parent = parent;
this.externalDocs = tagOrGroup.externalDocs;
this.externalDocs = (tagOrGroup as OpenAPITag).externalDocs;
// groups are active (expanded) by default
if (this.type === 'group') {