Skip to content

Commit e4e9fce

Browse files
committed
docs: allow code syntax in headers
1 parent 4543ede commit e4e9fce

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import 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+
}
59
export default MyH2;

docs/src/components/Headers/ScrollSpyH2.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react';
2-
import kebabCase from 'lodash/kebabCase';
32
import ScrollLink from '../templates/ScrollSpyLink';
43
import * as styles from '../../../static/styles/index.module.scss';
54
import cx from 'classnames';
65
import { Icon } from 'antd';
76

7+
import { getHashFromContent } from './getHashFromContent';
8+
89
const 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}>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

docs/src/templates/DocTemplate.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

docs/static/styles/_code.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
@import 'variables';
22

3+
h2, h3 {
4+
code {
5+
font-size: inherit;
6+
line-height: inherit;
7+
margin-bottom: inherit;
8+
}
9+
}
10+
311
pre, code, tt {
412
font-family: $code-font;
513
font-size: 16px;

0 commit comments

Comments
 (0)