Skip to content

Commit 5a4d1c7

Browse files
committed
Added PrismJS for syntax highlighting
1 parent 135b2a6 commit 5a4d1c7

File tree

6 files changed

+340
-8
lines changed

6 files changed

+340
-8
lines changed

.babelrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
{
2-
"presets": ["@babel/preset-env", "@babel/preset-react"]
2+
"presets": ["@babel/preset-env", "@babel/preset-react"],
3+
"plugins": [
4+
[
5+
"prismjs",
6+
{
7+
"languages": ["javascript"],
8+
"theme": "twilight"
9+
}
10+
]
11+
]
312
}

css/prism.css

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/* PrismJS 1.15.0
2+
https://prismjs.com/download.html#themes=prism-okaidia&languages=clike+javascript */
3+
/**
4+
* okaidia theme for JavaScript, CSS and HTML
5+
* Loosely based on Monokai textmate theme by http://www.monokai.nl/
6+
* @author ocodia
7+
*/
8+
9+
code[class*='language-'],
10+
pre[class*='language-'] {
11+
color: #f8f8f2;
12+
background: none;
13+
text-shadow: 0 1px rgba(0, 0, 0, 0.3);
14+
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
15+
text-align: left;
16+
white-space: pre;
17+
word-spacing: normal;
18+
word-break: normal;
19+
word-wrap: normal;
20+
line-height: 1.5;
21+
22+
-moz-tab-size: 4;
23+
-o-tab-size: 4;
24+
tab-size: 4;
25+
26+
-webkit-hyphens: none;
27+
-moz-hyphens: none;
28+
-ms-hyphens: none;
29+
hyphens: none;
30+
}
31+
32+
/* Code blocks */
33+
pre[class*='language-'] {
34+
padding: 1em;
35+
margin: 0.5em 0;
36+
overflow: auto;
37+
border-radius: 0.3em;
38+
}
39+
40+
:not(pre) > code[class*='language-'],
41+
pre[class*='language-'] {
42+
background: #272822;
43+
}
44+
45+
/* Inline code */
46+
:not(pre) > code[class*='language-'] {
47+
padding: 0.1em;
48+
border-radius: 0.3em;
49+
white-space: normal;
50+
}
51+
52+
.token.comment,
53+
.token.prolog,
54+
.token.doctype,
55+
.token.cdata {
56+
color: slategray;
57+
}
58+
59+
.token.punctuation {
60+
color: #f8f8f2;
61+
}
62+
63+
.namespace {
64+
opacity: 0.7;
65+
}
66+
67+
.token.property,
68+
.token.tag,
69+
.token.constant,
70+
.token.symbol,
71+
.token.deleted {
72+
color: #f92672;
73+
}
74+
75+
.token.boolean,
76+
.token.number {
77+
color: #ae81ff;
78+
}
79+
80+
.token.selector,
81+
.token.attr-name,
82+
.token.string,
83+
.token.char,
84+
.token.builtin,
85+
.token.inserted {
86+
color: #a6e22e;
87+
}
88+
89+
.token.operator,
90+
.token.entity,
91+
.token.url,
92+
.language-css .token.string,
93+
.style .token.string,
94+
.token.variable {
95+
color: #f8f8f2;
96+
}
97+
98+
.token.atrule,
99+
.token.attr-value,
100+
.token.function,
101+
.token.class-name {
102+
color: #e6db74;
103+
}
104+
105+
.token.keyword {
106+
color: #66d9ef;
107+
}
108+
109+
.token.regex,
110+
.token.important {
111+
color: #fd971f;
112+
}
113+
114+
.token.important,
115+
.token.bold {
116+
font-weight: bold;
117+
}
118+
.token.italic {
119+
font-style: italic;
120+
}
121+
122+
.token.entity {
123+
cursor: help;
124+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"prismjs": "^1.15.0",
67
"react": "^16.7.0",
78
"react-dom": "^16.7.0",
89
"styled-components": "^4.1.3"
@@ -19,12 +20,15 @@
1920
"@babel/preset-react": "^7.0.0",
2021
"babel-eslint": "^10.0.1",
2122
"babel-loader": "^8.0.5",
23+
"babel-plugin-prismjs": "^1.0.2",
24+
"css-loader": "^2.1.0",
2225
"eslint": "^5.12.1",
2326
"eslint-config-react": "^1.1.7",
2427
"eslint-loader": "^2.1.1",
2528
"eslint-plugin-react": "^7.12.4",
2629
"html-webpack-plugin": "^3.2.0",
2730
"prettier": "1.16.2",
31+
"style-loader": "^0.23.1",
2832
"webpack": "^4.29.0",
2933
"webpack-bundle-analyzer": "^3.0.3",
3034
"webpack-cli": "^3.2.1",

src/index.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import styled from 'styled-components';
4+
import Prism from 'prismjs';
5+
import '../css/prism.css';
46

57
const Title = styled.h1`
68
font-family: sans-serif;
7-
padding: 2em;
9+
padding: 1.5em;
810
background: papayawhip;
9-
font-size: 4em;
11+
font-size: 2em;
1012
text-align: center;
1113
color: palevioletred;
1214
margin: 0;
1315
`;
1416

1517
class Welcome extends React.Component {
18+
componentDidMount() {
19+
Prism.highlightAll();
20+
}
21+
1622
render() {
17-
return <Title>Hello World</Title>;
23+
return (
24+
<React.Fragment>
25+
<Title>JavaScript Patterns</Title>
26+
27+
<pre>
28+
<code className="language-javascript">
29+
{`
30+
onSubmit(e) {
31+
e.preventDefault();
32+
const job = {
33+
title: 'Developer',
34+
company: 'Facebook'
35+
};
36+
}
37+
`}
38+
</code>
39+
</pre>
40+
</React.Fragment>
41+
);
1842
}
1943
}
2044

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ module.exports = {
2424
test: /\.(js|jsx)$/,
2525
exclude: /node_modules/,
2626
use: ['babel-loader', 'eslint-loader']
27+
},
28+
{
29+
test: /\.css$/,
30+
use: ['style-loader', 'css-loader']
2731
}
2832
]
2933
},

0 commit comments

Comments
 (0)