一、修改js
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
highlightCodeBlocks() {
if (typeof hljs !== 'undefined') {
const blocks = this.editorDiv.querySelectorAll('pre code');
blocks.forEach((block, idx) => {
block.removeAttribute('data-highlighted');
hljs.highlightElement(block);
const pre = block.parentNode;
if (!pre || pre.classList.contains('hljs-line-numbers-added')) return;
const codeText = block.innerText;
const lines = codeText.split('\n').length;
if (lines <= 1) return;
const lineNumbersDiv = document.createElement('div');
lineNumbersDiv.className = 'hljs-line-numbers';
lineNumbersDiv.setAttribute('aria-hidden', 'true');
lineNumbersDiv.style.cssText = `
float: left;
text-align: right;
padding-right: 10px;
user-select: none;
color: #888;
background: inherit;
border-right: 1px solid #444;
margin-right: 10px;
font-family: monospace;
font-size: inherit;
line-height: inherit;
`;
let numbers = '';
for (let i = 1; i <= lines; i++) numbers += i + '\n';
lineNumbersDiv.innerText = numbers;
pre.style.position = 'relative';
pre.style.overflow = 'auto';
pre.style.display = 'flex';
pre.style.flexDirection = 'row';
pre.insertBefore(lineNumbersDiv, block);
block.style.display = 'block';
block.style.flex = '1';
block.style.whiteSpace = 'pre';
pre.classList.add('hljs-line-numbers-added');
});
}
}
二、修改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
pre.hljs-line-numbers-added {
display: flex !important;
flex-direction: row !important;
align-items: stretch !important;
background: #2c3e50;
color: #f8f9fa;
padding: 12px !important;
}
.hljs-line-numbers {
float: none;
text-align: right;
padding-right: 15px;
user-select: none;
color: #8e9eaf;
border-right: 1px solid #4a5b6e;
background: inherit;
font-family: monospace;
font-size: 14px;
line-height: 1.45;
white-space: pre;
}
pre code {
display: block;
flex: 1;
overflow-x: auto;
white-space: pre;
font-family: monospace;
font-size: 14px;
line-height: 1.45;
}
@media (max-width: 768px) {
.hljs-line-numbers {
padding-right: 8px;
font-size: 12px;
}
pre code {
font-size: 12px;
}
}
发表评论 (0)
留下你的足迹