File tree Expand file tree Collapse file tree 5 files changed +39
-7
lines changed Expand file tree Collapse file tree 5 files changed +39
-7
lines changed Original file line number Diff line number Diff line change 11import React from 'react' ;
2- import kebabCase from 'lodash/kebabCase' ;
32
4- const MyH2 = ( props ) => < h2 name = { kebabCase ( props . children ) } { ...props } /> ;
3+ import { getHashFromContent } from './getHashFromContent' ;
4+
5+ const MyH2 = ( props ) => {
6+ const hash = getHashFromContent ( props . children ) ;
7+ return ( < h2 name = { hash } { ...props } /> ) ;
8+ }
59export default MyH2 ;
Original file line number Diff line number Diff line change 11import React from 'react' ;
2- import kebabCase from 'lodash/kebabCase' ;
32import ScrollLink from '../templates/ScrollSpyLink' ;
43import * as styles from '../../../static/styles/index.module.scss' ;
54import cx from 'classnames' ;
65import { Icon } from 'antd' ;
76
7+ import { getHashFromContent } from './getHashFromContent' ;
8+
89const ScrollSpyH2 = ( props ) => {
9- const hash = kebabCase ( props . children ) ;
10+
11+ const hash = getHashFromContent ( props . children ) ;
1012
1113 return (
1214 < h2 name = { hash } className = { styles . hTag } >
Original file line number Diff line number Diff line change 1+ import kebabCase from 'lodash/kebabCase' ;
2+
3+ export const getHashFromContent = ( children ) => {
4+ const isString = typeof children === 'string' ;
5+ const nameString = isString ? children : children . props . children ;
6+ return kebabCase ( nameString ) ;
7+ }
Original file line number Diff line number Diff line change @@ -244,7 +244,18 @@ class DocTemplate extends Component<Props, State> {
244244 [ styles . postClearSection ] : isPreviousSectionClearable ,
245245 } ) ;
246246
247- currentID = kebabCase ( item . props . children [ 0 ] ) ;
247+ let elementId = item . props . children [ 0 ] ;
248+ let elementTitle = item . props . children [ 0 ] ;
249+ // Handle code-block H2 headers
250+ if ( Array . isArray ( item . props . children ) && item . props . children . length === 1 && typeof item . props . children [ 0 ] !== 'string' ) {
251+ elementId = item . props . children [ 0 ] . props . children [ 0 ] ;
252+ }
253+ // Handle code-block H3 headers with custom ID prefix
254+ if ( Array . isArray ( item . props . children ) && item . props . children . length > 1 ) {
255+ elementId = item . props . children [ 1 ] . props . children [ 0 ] ;
256+ }
257+
258+ currentID = kebabCase ( elementId ) ;
248259
249260 if ( item . type === 'h2' ) {
250261 prevSection . className = cx ( prevSection . className , {
@@ -262,7 +273,7 @@ class DocTemplate extends Component<Props, State> {
262273 : currentID ,
263274 type : item . type ,
264275 nodes : [ ] ,
265- title : item . props . children [ 0 ] ,
276+ title : elementTitle ,
266277 className,
267278 } ) ;
268279
@@ -276,7 +287,7 @@ class DocTemplate extends Component<Props, State> {
276287 type : 'link' ,
277288 className : styles . hTagIcon ,
278289 } ) ,
279- item . props . children [ 0 ]
290+ elementId
280291 )
281292 ) ;
282293 }
Original file line number Diff line number Diff line change 11@import ' variables' ;
22
3+ h2 , h3 {
4+ code {
5+ font-size : inherit ;
6+ line-height : inherit ;
7+ margin-bottom : inherit ;
8+ }
9+ }
10+
311pre , code , tt {
412 font-family : $code-font ;
513 font-size : 16px ;
You can’t perform that action at this time.
0 commit comments