fix: markdown parent name (#2062)

Co-authored-by: Alex Varchuk <olexandr.varchuk@gmail.com>
This commit is contained in:
徒言 2022-09-06 20:10:54 +08:00 committed by GitHub
parent 3241000894
commit da9ed0b4d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 5 deletions

View File

@ -79,7 +79,11 @@ export class SectionItem extends React.Component<ContentItemProps> {
</Header>
</MiddlePanel>
</Row>
<AdvancedMarkdown source={description || ''} htmlWrap={middlePanelWrap} />
<AdvancedMarkdown
parentId={this.props.item.id}
source={description || ''}
htmlWrap={middlePanelWrap}
/>
{externalDocs && (
<Row>
<MiddlePanel>

View File

@ -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<AdvancedMarkdownProps> {
@ -28,7 +29,7 @@ export class AdvancedMarkdown extends React.Component<AdvancedMarkdownProps> {
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) {

View File

@ -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: [],

View File

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