-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCodeBlock.vue
149 lines (134 loc) · 3.13 KB
/
CodeBlock.vue
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
<template>
<div class="code-block-wrapper">
<div :class="className">
<pre
:class="className"
><code :class="className" v-html="highlightCode"></code></pre>
<div class="line-numbers-wrapper">
<template v-for="i in getLineNumbers" :key="i">
<span class="line-numbers">{{ i }}</span>
<br />
</template>
</div>
</div>
</div>
</template>
<script>
import { highlight, languages } from 'prismjs'
import 'prismjs/components/prism-json'
import 'prismjs/components/prism-python'
import 'prismjs/components/prism-markdown'
import 'prismjs/themes/prism-tomorrow.css'
import { computed, toRefs } from 'vue'
export default {
props: {
lang: {
type: String,
default: 'py'
},
code: {
type: String,
required: true
}
},
setup(props) {
const { lang, code } = toRefs(props)
// computed properties
const className = computed(() => {
if (props.lang === 'txt') {
return 'language-markup'
}
return `language-${lang.value}`
})
const highlightCode = computed(() => {
if (lang.value === 'txt') {
return highlight(code.value, languages['markup'], 'markup')
}
return highlight(code.value, languages[lang.value], lang.value)
})
const getLineNumbers = computed(() => {
return code.value.split('\n').length
})
return { className, highlightCode, getLineNumbers }
}
}
</script>
<style scoped>
.code-block-wrapper {
overflow: auto;
height: 100vh;
}
div[class*='language-'] {
position: relative;
}
div[class*='language-']::before {
position: absolute;
color: var(--c-white-dark);
font-size: 0.75rem;
z-index: 3;
right: 1em;
top: 0.5em;
}
[class*='language-'] pre {
position: relative;
margin: 0;
padding: 1.25rem 1.5rem;
padding-left: 4.5rem;
vertical-align: middle;
overflow: auto;
border-radius: 0 3px 3px 0;
font-size: var(--font-size) !important;
}
/* font-size is pre's font-size * code's font-size */
[class*='language-'] code {
padding: 0;
border-radius: 0;
line-height: var(--code-line-height);
}
.line-numbers-wrapper {
position: absolute;
top: 0;
bottom: 0;
z-index: 3;
border-right: 1px solid rgba(0, 0, 0, 0.5);
padding: 1.25rem 0;
width: 3.5rem;
text-align: center;
font-family: var(--code-font-family);
font-size: var(--font-size);
line-height: var(--code-line-height);
color: var(--c-white-dark);
background-color: #2d2d2d;
}
/* font-size is .line-numbers-wrapper's font-size * .line-numbers's font-size */
.line-numbers {
position: relative;
font-size: var(--code-font-size);
z-index: 4;
user-select: none;
}
div[class~='language-md']::before,
div[class~='language-markdown']::before {
content: 'md';
}
div[class~='language-json']::before {
content: 'json';
}
div[class~='language-py']::before,
div[class~='language-python']::before {
content: 'py';
}
div[class~='language-sh']::before,
div[class~='language-bash']::before {
content: 'sh';
}
div[class~='language-yaml']::before {
content: 'yaml';
}
/* media queries */
@media (max-width: 768px) {
.code-block-wrapper {
height: 100%;
}
}
</style>