Skip to content

Commit dba1557

Browse files
committed
feat: Support autolink headings.
1 parent e6e8462 commit dba1557

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
"@mapbox/rehype-prism": "0.6.0",
4949
"rehype-raw": "5.1.0",
5050
"remark-gfm": "1.0.0",
51+
"remark-slug": "6.0.0",
52+
"remark-autolink-headings": "6.0.1",
53+
"rehype-rewrite": "1.0.2",
5154
"react-markdown": "6.0.2"
5255
},
5356
"devDependencies": {

src/index.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
import React, { useImperativeHandle } from 'react';
22
import ReactMarkdown from 'react-markdown';
33
import gfm from 'remark-gfm';
4+
// @ts-ignore
5+
import slug from 'remark-slug';
6+
// @ts-ignore
7+
import headings from 'remark-autolink-headings'
48
import rehypeRaw from 'rehype-raw';
59
import rehypePrism from '@mapbox/rehype-prism';
10+
import rehypeRewrite from 'rehype-rewrite';
611
import './styles/markdown.less';
712
import './styles/markdowncolor.less';
813

14+
const rehypeRewriteHandle = (node: any, index: number, parent: any) => {
15+
if (node.type === 'element' && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName) && index !== 0) {
16+
const child = node.children && node.children[0] ? node.children[0] : null;
17+
if (child && child.properties && child.properties.ariaHidden === 'true') {
18+
child.properties = { class: 'anchor', ...child.properties };
19+
child.children = [
20+
{
21+
type: 'element',
22+
tagName: 'svg',
23+
properties: {
24+
class: 'octicon octicon-link',
25+
viewBox: '0 0 16 16',
26+
version: '1.1',
27+
width: '16',
28+
height: '16',
29+
ariaHidden: 'true',
30+
},
31+
children: [
32+
{
33+
type: 'element',
34+
tagName: 'path',
35+
properties: {
36+
fillRule: 'evenodd',
37+
d: 'M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z',
38+
}
39+
}
40+
]
41+
}
42+
];
43+
}
44+
}
45+
}
46+
947
export type MarkdownPreviewProps = {
1048
className?: string;
1149
source?: string;
@@ -29,8 +67,9 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
2967
<div ref={mdp} onScroll={onScroll} onMouseOver={onMouseOver} {...warpperElement} className={cls} style={style}>
3068
<ReactMarkdown
3169
{...other}
32-
plugins={[gfm, ...(other.plugins || [])]}
33-
rehypePlugins={[[rehypePrism, { ignoreMissing: true }], rehypeRaw, ...(other.rehypePlugins || [])]}
70+
plugins={[gfm, ...(other.plugins || [])]}
71+
rehypePlugins={[[rehypePrism, { ignoreMissing: true }], [rehypeRewrite, rehypeRewriteHandle], rehypeRaw, ...(other.rehypePlugins || [])]}
72+
remarkPlugins={[ slug, headings, ...(other.remarkPlugins || []) ]}
3473
children={source || ''}
3574
/>
3675
</div>

src/styles/markdown.less

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868
line-height: 1.25;
6969
margin-bottom: 16px;
7070
margin-top: 24px;
71+
.anchor {
72+
float: left;
73+
padding-right: 4px;
74+
margin-left: -20px;
75+
line-height: 1;
76+
}
77+
.octicon-link {
78+
visibility: hidden;
79+
vertical-align: middle;
80+
}
81+
&:hover .octicon-link {
82+
visibility: visible;
83+
}
7184
}
7285
h1 {
7386
font-size: 2em;

0 commit comments

Comments
 (0)