Skip to content

Commit 3a41102

Browse files
committed
Add code highlight.
1 parent 1c6985d commit 3a41102

File tree

13 files changed

+112
-19
lines changed

13 files changed

+112
-19
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
.hljs {
3+
.selector-tag {
4+
color: #22863a;
5+
}
6+
.selector-class {
7+
color: #6f42c1;
8+
}
9+
.selector-pseudo {
10+
font-weight: bold;
11+
}
12+
.attribute {
13+
color: #005cc5;
14+
}
15+
.number {
16+
color: #005cc5;
17+
}
18+
}

src/components/PreviewMarkdown/default.less

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ pre code {
1313
pre {
1414
margin-bottom: 18px;
1515
max-height: 35em;
16+
font-size: 85%;
17+
line-height: 1.45;
1618
position: relative;
1719
overflow: auto;
18-
background-color: #F0F0F0;
20+
background-color: #f6f8fa;
1921
border-radius: 3px;
2022
}
2123
del {
2224
color: #888;
2325
}
2426
pre code {
2527
background: none;
26-
font-size: 1em;
2728
overflow-wrap: normal;
2829
white-space: inherit;
2930
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
.hljs {
3+
.comment {
4+
color: #6a737d;
5+
}
6+
.keyword {
7+
color: #d73a49;
8+
}
9+
.attr {
10+
color: #032f62;
11+
}
12+
.string {
13+
color: #032f62;
14+
}
15+
.class {
16+
.keyword {
17+
color: #d73a49;
18+
}
19+
.title {
20+
color: #6f42c1;
21+
}
22+
.title {
23+
color: #6f42c1;
24+
}
25+
}
26+
.tag {
27+
.name {
28+
color: #22863a;
29+
}
30+
.attr {
31+
color: #6f42c1;
32+
}
33+
}
34+
}

src/components/PreviewMarkdown/index.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
&-markdown {
2020
padding: 20px;
2121
@import "./default.less";
22+
@import "./hljs.less";
23+
@import "./css.less";
2224
}
2325
}
2426
}

src/components/PreviewMarkdown/index.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import classnames from 'classnames'
2+
import hljs from 'highlight.js';
23
import React from 'react';
34
import ReactMarkdown, { MarkdownAbstractSyntaxTree } from 'react-markdown';
45
import { IProps } from '../../common/props';
56
import './index.less';
67

8+
hljs.configure({
9+
classPrefix: '', // don't append class prefix
10+
tabReplace: ' ', // 2 spaces
11+
});
12+
713
export interface IPreviewMarkdown extends IProps {
814
visble: boolean;
915
value?: string;
@@ -20,21 +26,39 @@ export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, I
2026
prefixCls: 'md-editor',
2127
visble: true,
2228
}
29+
public node!: HTMLDivElement;
2330
constructor(props: IPreviewMarkdown) {
2431
super(props);
2532
this.state = {
2633
value: props.value,
2734
}
2835
}
2936

37+
public componentDidMount() {
38+
this.highlight();
39+
}
40+
41+
public highlight() {
42+
const code = this.node.getElementsByTagName('code') as unknown as HTMLElement[];
43+
for (const value of code) {
44+
const tag = value.parentNode as HTMLElement;
45+
if (tag && tag.tagName === 'PRE') {
46+
hljs.highlightBlock(value);
47+
}
48+
}
49+
}
50+
3051
public updateSource(value: string) {
31-
this.setState({ value });
52+
this.setState({ value }, () => {
53+
this.highlight();
54+
});
3255
}
3356

3457
public render() {
3558
const { prefixCls, visble, value, ...elementProps } = this.props;
3659
return (
3760
<div
61+
ref={(node: HTMLDivElement) => this.node = node}
3862
className={classnames(`${prefixCls}-preview`, {
3963
[`${prefixCls}-visble`]: visble
4064
})}

src/index.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
box-shadow: 0 0 0 1px rgba(16, 22, 26, 0.1), 0 0 0 rgba(16, 22, 26, 0), 0 1px 1px rgba(16, 22, 26, 0.2);
66
border-radius: 3px;
77
position: relative;
8+
background-color: #fff;
89
&-content {
910
position: relative;
1011
}

website/App.less

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
body {
2+
background: #343a40;
3+
}
4+
15
.warpper {
26
color: #333;
37
}
@@ -9,7 +13,9 @@
913
}
1014

1115
.doc {
12-
padding: 20px 0 0 0;
16+
width: 980px;
17+
padding: 20px 10px 0 10px;
18+
margin: 0 auto;
1319
}
1420

1521
.logo {
@@ -19,4 +25,4 @@
1925
margin: 0 auto;
2026
display: block;
2127
}
22-
}
28+
}

website/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class App extends React.Component {
2727
/>
2828
</div>
2929
<ReactMarkdown source={DocumentStrSource} className={styles.doc} />
30-
<Footer name="Kenny Wong" href="" year={2019}/>
30+
<Footer name="Kenny Wong" href="https://github.com/uiwjs/react-markdown-editor" github="https://github.com/uiwjs/react-markdown-editor" year={2019}/>
3131
</div>
3232
);
3333
}

website/Logo.tsx

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

website/components/Footer.less

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
12
.footer {
2-
text-align: center;
3-
padding: 15px 0 100px 0;
4-
font-size: 12px;
5-
line-height: 20px;
3+
text-align: center;
4+
padding: 15px 0 100px 0;
5+
font-size: 12px;
6+
line-height: 20px;
7+
color: #fff;
8+
a {
9+
color: #03A9F4;
10+
}
611
}

0 commit comments

Comments
 (0)