prism.js美化代码块

程序员的博客里绝对不会缺少的东西一定是代码块,那么怎么把代码块打扮的美美的呢?

介于前两天有同学问我,我的代码块是怎么美化的,那么今天我就出个我美化代码块的教程,主要分为三个步骤,分割关键字、添加额外标签和自定义美化css

加载 js 分割代码关键字

使用任意代码块分割插件自动将代码关键字加上相应标签,我这是使用的是一款名叫 prism 的插件

1
<script src="//cdn.jsdelivr.net/gh/cetr/cdn@master/prism.min.js"></script>

动态添加代码块头部样式

用 js 动态对每篇文章里的代码块加上顶部三个彩色圆点,用于美化ui

1
2
3
4
5
6
7
8
9
10
11
12
13
<script>
if (!$('pre').hasClass('line-numbers')) {
$('pre').addClass('line-numbers').before($(
'<figcaption class="line-numbers-head">' +
'<div class="custom-carbon">' +
'<div class="custom-carbon-dot custom-carbon-dot--red"></div>' +
'<div class="custom-carbon-dot custom-carbon-dot--yellow"></div>' +
'<div class="custom-carbon-dot custom-carbon-dot--green"></div>' +
'</div>' +
'</figcaption>'
));
}
</script>

自定义 css 样式

自定义 css 给代码块及代码块里的文字添加样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
code[class*="language-"],
pre[class*="language-"] {
color: black;
background: none;
text-shadow: 0 1px white;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
font-size: 16px;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.5;

-moz-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;

-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}

pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
text-shadow: none;
}

pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
code[class*="language-"]::selection, code[class*="language-"] ::selection {
text-shadow: none;
}

@media print {
code[class*="language-"],
pre[class*="language-"] {
text-shadow: none;
}
}

/* Code blocks */
pre[class*="language-"] {
padding: 1em;
margin-bottom: 20px;
overflow: auto;
}

:not(pre) > code[class*="language-"],
pre[class*="language-"] {
background: #f5f2f0;
}

:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: #90a4ae;
}

.token.punctuation,
.token.attr-name {
color: #757575;
}

.token.namespace {
opacity: .7;
}

.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol,
.token.deleted {
color: #f76d47;
}

.token.selector,
.token.char,
.token.builtin,
.token.inserted {
color: #be4dbc;
}

.token.keyword {
color: #39adb5;
}

.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
color: #9a6e3a;
}

.token.atrule,
.token.string,
.token.attr-value {
color: #4dc14c;
}

.token.function,
.token.class-name {
color: #6182b8;
}

.token.regex,
.token.important,
.token.variable {
color: #eab700;
}

.token.parameter {
color: #ee8019;
}

.token.important,
.token.bold {
font-weight: bold;
}

.token.italic {
font-style: italic;
}

.token.entity {
cursor: help;
}

figcaption.line-numbers-head {
width: 100%;
color: #8d949e;
background-color: #f5f6f7;
zoom: 1;
border-top-left-radius: 0.25rem;
border-top-right-radius: 0.25rem;
}

figcaption.line-numbers-head .custom-carbon-dot {
display: inline-block;
margin: 0 4px;
border-radius: 50%;
width: 10px;
height: 10px;
}

figcaption.line-numbers-head .custom-carbon-dot--red {
background-color: #ff5f56;
}

figcaption.line-numbers-head .custom-carbon-dot--yellow {
background-color: #ffbd2e;
}

figcaption.line-numbers-head .custom-carbon-dot--green {
background-color: #27c93f;
}

figcaption.line-numbers-head .custom-carbon {
padding: 2px 0 0 9px;
}

pre.line-numbers {
margin-top: 0;
padding: 10px 1em 1em 1em;
background-color: #f5f6f7;
border-bottom-left-radius: 0.25rem;
border-bottom-right-radius: 0.25rem;
}

地址:https://coor.top/p/40.html


prism.js美化代码块
http://yoursite.com/2022/09/01/网页/code/
作者
雨停之後
发布于
2022年9月1日
许可协议