1import GithubSlugger from 'github-slugger'; 2import * as React from 'react'; 3 4import { ElementType, PageMetadata } from '../types/common'; 5import * as Utilities from './utilities'; 6 7/** 8 * These types directly correspond to MDAST node types 9 */ 10export enum HeadingType { 11 Text = 'text', 12 InlineCode = 'inlineCode', 13} 14 15/** 16 * Minimum heading level to display in sidebar. 17 * Example: When set to 2, the `H1` headers are unlisted, 18 * `H2`s are root level, and `H3`, `H4`... are nested. 19 * 20 * NOTE: Changing this needs additional adjustments in `translate-markdown.js`! 21 */ 22export const BASE_HEADING_LEVEL = 2; 23 24/** 25 * How deeply nested headings to display 26 * 0 - means only root headings 27 * 28 * Can be overriden in `.md` pages by setting 29 * `maxHeadingDepth` attribute 30 */ 31const DEFAULT_NESTING_LIMIT = 1; 32 33/** 34 * Those properties can be customized 35 * from markdown pages usign heading components 36 * from `plugins/Headings.tsx` 37 */ 38export type AdditionalProps = { 39 hideInSidebar?: boolean; 40 sidebarTitle?: string; 41 sidebarDepth?: number; 42 sidebarType?: HeadingType; 43}; 44 45type Metadata = Partial<PageMetadata> & Required<Pick<PageMetadata, 'headings'>>; 46 47/** 48 * Single heading entry 49 */ 50export type Heading = { 51 title: string; 52 slug: string; 53 level: number; 54 type: HeadingType; 55 ref: React.RefObject<any>; 56 metadata?: ElementType<Metadata['headings']>; 57}; 58 59/** 60 * Manages heading entries. Each entry corresponds to one markdown heading with specified level (#, ##, ### etc) 61 * 62 * This class uses Slugger instance to generate and manage unique slugs 63 */ 64export class HeadingManager { 65 private slugger: GithubSlugger; 66 private _headings: Heading[]; 67 private readonly _meta: Metadata; 68 private readonly _maxNestingLevel: number; 69 70 public get headings() { 71 return this._headings; 72 } 73 74 public get maxNestingLevel() { 75 return this._maxNestingLevel; 76 } 77 78 public get metadata() { 79 return this._meta; 80 } 81 82 /** 83 * @param slugger A _GithubSlugger_ instance 84 * @param meta Document metadata gathered by `headingsMdPlugin`. 85 */ 86 constructor(slugger: GithubSlugger, meta: Partial<PageMetadata>) { 87 this.slugger = slugger; 88 this._meta = { headings: meta.headings || [], ...meta }; 89 this._headings = []; 90 91 const maxHeadingDepth = meta.maxHeadingDepth ?? DEFAULT_NESTING_LIMIT; 92 this._maxNestingLevel = maxHeadingDepth + BASE_HEADING_LEVEL; 93 } 94 95 /** 96 * Creates heading object instance and stores it 97 * @param {string | Object} title Heading display title or `<code/>` element 98 * @param {number|undefined} nestingLevel Override metadata heading nesting level. 99 * @param {*} additionalProps Additional properties passed to heading component 100 * @returns {Object} Newly created heading instance 101 */ 102 addHeading( 103 title: string | object, 104 nestingLevel?: number, 105 additionalProps?: AdditionalProps 106 ): Heading { 107 // NOTE (barthap): workaround for complex titles containing both normal text and inline code 108 // changing this needs also change in `headingsMdPlugin.js` to make metadata loading correctly 109 title = Array.isArray(title) ? title.map(Utilities.toString).join(' ') : title; 110 111 const { hideInSidebar, sidebarTitle, sidebarDepth, sidebarType } = additionalProps ?? {}; 112 const levelOverride = sidebarDepth != null ? BASE_HEADING_LEVEL + sidebarDepth : undefined; 113 114 const slug = Utilities.generateSlug(this.slugger, title); 115 const realTitle = Utilities.toString(title); 116 const meta = this.findMetaForTitle(realTitle); 117 const level = levelOverride ?? nestingLevel ?? meta?.level ?? BASE_HEADING_LEVEL; 118 const type = sidebarType || (this.isCode(title) ? HeadingType.InlineCode : HeadingType.Text); 119 120 const heading = { 121 title: sidebarTitle ?? realTitle, 122 slug, 123 level, 124 type, 125 ref: React.createRef(), 126 metadata: meta, 127 }; 128 129 // levels out of range are unlisted 130 if (!hideInSidebar && level >= BASE_HEADING_LEVEL && level <= this.maxNestingLevel) { 131 this._headings.push(heading); 132 } 133 134 return heading; 135 } 136 137 /** 138 * Finds MDX-plugin metadata for specified title. Once found, it's marked as processed 139 * and will not be returned again. 140 * @param {string} realTitle Title to find metadata for 141 */ 142 private findMetaForTitle(realTitle: string) { 143 const entry = this._meta.headings.find( 144 heading => heading.title === realTitle && !heading._processed 145 ); 146 if (!entry) { 147 return; 148 } 149 entry._processed = true; 150 return entry; 151 } 152 153 /** 154 * Checks if header title is an inline code block. 155 * @param {any} title Heading object to check 156 * @returns {boolean} true if header is a code block 157 */ 158 private isCode(title: any): boolean { 159 if (!title.props) { 160 return false; 161 } 162 const { name, originalType, mdxType } = title.props; 163 return [name, originalType, mdxType].some(it => it === HeadingType.InlineCode); 164 } 165} 166