From da9ed0b4d1a4070d326ecb472459f0ff916c6036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=92=E8=A8=80?= Date: Tue, 6 Sep 2022 20:10:54 +0800 Subject: [PATCH] fix: markdown parent name (#2062) Co-authored-by: Alex Varchuk --- src/components/ContentItems/ContentItems.tsx | 6 +++++- src/components/Markdown/AdvancedMarkdown.tsx | 3 ++- src/services/MarkdownRenderer.ts | 7 +++++-- src/services/MenuBuilder.ts | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/components/ContentItems/ContentItems.tsx b/src/components/ContentItems/ContentItems.tsx index 9c2eae74..2be1fc64 100644 --- a/src/components/ContentItems/ContentItems.tsx +++ b/src/components/ContentItems/ContentItems.tsx @@ -79,7 +79,11 @@ export class SectionItem extends React.Component { - + {externalDocs && ( diff --git a/src/components/Markdown/AdvancedMarkdown.tsx b/src/components/Markdown/AdvancedMarkdown.tsx index dba75e67..85da302b 100644 --- a/src/components/Markdown/AdvancedMarkdown.tsx +++ b/src/components/Markdown/AdvancedMarkdown.tsx @@ -9,6 +9,7 @@ import { StoreConsumer } from '../StoreBuilder'; export interface AdvancedMarkdownProps extends BaseMarkdownProps { htmlWrap?: (part: JSX.Element) => JSX.Element; + parentId?: string; } export class AdvancedMarkdown extends React.Component { @@ -28,7 +29,7 @@ export class AdvancedMarkdown extends React.Component { throw new Error('When using components in markdown, store prop must be provided'); } - const renderer = new MarkdownRenderer(options); + const renderer = new MarkdownRenderer(options, this.props.parentId); const parts = renderer.renderMdWithComponents(source); if (!parts.length) { diff --git a/src/services/MarkdownRenderer.ts b/src/services/MarkdownRenderer.ts index 7ba6752f..ba0c3d77 100644 --- a/src/services/MarkdownRenderer.ts +++ b/src/services/MarkdownRenderer.ts @@ -46,7 +46,8 @@ export class MarkdownRenderer { private headingEnhanceRenderer: marked.Renderer; private originalHeadingRule: typeof marked.Renderer.prototype.heading; - constructor(public options?: RedocNormalizedOptions) { + constructor(public options?: RedocNormalizedOptions, public parentId?: string) { + this.parentId = parentId; this.parser = new marked.Parser(); this.headingEnhanceRenderer = new marked.Renderer(); this.originalHeadingRule = this.headingEnhanceRenderer.heading.bind( @@ -63,7 +64,9 @@ export class MarkdownRenderer { ): MarkdownHeading { name = unescapeHTMLChars(name); const item: MarkdownHeading = { - id: parentId ? `${parentId}/${safeSlugify(name)}` : `section/${safeSlugify(name)}`, + id: parentId + ? `${parentId}/${safeSlugify(name)}` + : `${this.parentId || 'section'}/${safeSlugify(name)}`, name, level, items: [], diff --git a/src/services/MenuBuilder.ts b/src/services/MenuBuilder.ts index fb8cf5a5..95b12ee3 100644 --- a/src/services/MenuBuilder.ts +++ b/src/services/MenuBuilder.ts @@ -41,7 +41,7 @@ export class MenuBuilder { initialDepth: number, options: RedocNormalizedOptions, ): ContentItemModel[] { - const renderer = new MarkdownRenderer(options); + const renderer = new MarkdownRenderer(options, parent?.id); const headings = renderer.extractHeadings(description || ''); if (headings.length && parent && parent.description) {