Skip to content

Commit fc42206

Browse files
committed
详情页渲染
1 parent c18cd0d commit fc42206

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dayjs": "^1.8.21",
3232
"express": "^4.16.4",
3333
"github-markdown-css": "^4.0.0",
34+
"highlight.js": "^9.18.1",
3435
"nuxt": "^2.0.0",
3536
"vant": "^2.5.3"
3637
},

pages/detail/_id.vue

+154-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
<template>
22
<div class="content">
33
<div class="detail-box">
4+
<h2 class="detail-title van-ellipsis">
5+
<van-tag type="primary" class="tag">{{ articleType }}</van-tag
6+
>{{ info.articleTitle }}
7+
</h2>
8+
<div class="flex row">
9+
<span>{{ time }}</span>
10+
<nuxt-link class="theme-color" :to="`/about/${authorInfo.authorId}`">
11+
{{ authorInfo.authorName }}
12+
</nuxt-link>
13+
</div>
14+
<div class="flex row">
15+
<span>views: {{ info.articleView }}</span>
16+
<van-button plain type="info" size="small" @click="addStar"
17+
>star: {{ info.articleStart }}</van-button
18+
>
19+
</div>
20+
<div class="toc-btn flex" @click="tocShow = !tocShow">
21+
<van-icon v-if="!tocShow" name="arrow-left" />
22+
<van-icon v-else name="arrow" />
23+
<span>目录</span>
24+
</div>
425
<div
26+
:class="{ 'no-toc': !tocShow }"
527
class="detail-article markdown-body"
6-
v-html="detailInfo.info.articleContentHtml"
28+
v-html="info.articleContentHtml"
729
></div>
830
</div>
931
</div>
1032
</template>
1133

1234
<script>
35+
import dayjs from 'dayjs'
36+
import 'highlight.js/scss/default.scss'
1337
import 'github-markdown-css/github-markdown.css'
38+
import 'highlight.js/styles/vs2015.css'
1439
1540
export default {
1641
name: 'Detail',
@@ -26,26 +51,151 @@ export default {
2651
detailInfo = result.data
2752
}
2853
return {
29-
detailInfo
54+
...detailInfo
3055
}
3156
},
3257
data() {
33-
return {}
58+
return {
59+
tocShow: false
60+
}
61+
},
62+
computed: {
63+
articleType() {
64+
let result
65+
switch (this.info.articleNature) {
66+
case 0:
67+
result = '原创'
68+
break
69+
case 1:
70+
result = '转载'
71+
break
72+
case 2:
73+
result = '翻译'
74+
break
75+
default:
76+
result = '原创'
77+
break
78+
}
79+
return result
80+
},
81+
time() {
82+
return dayjs(this.info.articleUpdateTime).format('YYYY-MM-DD HH:mm:ss')
83+
}
84+
},
85+
methods: {
86+
async addStar() {
87+
const result = await this.$axios.$post('/api/vue-blog/addStar', {
88+
articleId: this.$route.params.id * 1
89+
})
90+
if (result.isok) {
91+
this.$toast.success('Add star success')
92+
this.info.articleStart = result.data.articleStart
93+
}
94+
}
3495
}
3596
}
3697
</script>
3798

3899
<style lang="scss" scoped>
39100
.detail-box {
40101
margin: 15px 0;
102+
padding: 0 15px;
41103
background-color: #fff;
104+
.detail-title {
105+
padding: 15px 0 10px;
106+
font-size: 18px;
107+
.tag {
108+
margin-right: 3px;
109+
vertical-align: top;
110+
margin-top: 5px;
111+
}
112+
}
113+
.row {
114+
justify-content: space-between;
115+
font-size: 16px;
116+
line-height: 30px;
117+
}
118+
.toc-btn {
119+
position: fixed;
120+
top: 68%;
121+
right: 0;
122+
z-index: 1000;
123+
height: 30px;
124+
font-size: 14px;
125+
padding: 0 8px;
126+
background-color: #fff;
127+
border: 1px solid #dedede;
128+
border-right: 0;
129+
border-radius: 15px 0 0 15px;
130+
}
42131
.detail-article {
43-
padding: 15px;
132+
position: relative;
133+
padding: 0 0 15px;
134+
font-size: 16px;
44135
&.markdown-body {
45136
/deep/ {
46137
ol {
47138
list-style: decimal;
48139
}
140+
ul {
141+
list-style: disc;
142+
}
143+
.markdownIt-TOC {
144+
position: fixed;
145+
right: 15px;
146+
z-index: 10;
147+
background-color: #fff;
148+
box-shadow: -2px 2px 4px #ccc;
149+
&.markdownIt-TOC::before {
150+
content: '文章目录:';
151+
font-weight: 700;
152+
color: currentColor;
153+
margin-left: -1.5em;
154+
line-height: 26px;
155+
}
156+
}
157+
pre {
158+
background: #1e1e1e !important;
159+
}
160+
pre.hljs {
161+
padding: 14px 2px;
162+
border-radius: 5px;
163+
position: relative;
164+
ol {
165+
list-style: decimal;
166+
margin: 0;
167+
margin-left: 40px;
168+
padding: 0;
169+
li {
170+
list-style: decimal-leading-zero;
171+
position: relative;
172+
padding-left: 10px;
173+
.line-num {
174+
position: absolute;
175+
left: -40px;
176+
top: 0;
177+
width: 40px;
178+
height: 100%;
179+
border-right: 1px solid rgba(0, 0, 0, 0.66);
180+
}
181+
}
182+
}
183+
b.name {
184+
position: absolute;
185+
top: 2px;
186+
right: 12px;
187+
z-index: 10;
188+
color: #999;
189+
pointer-events: none;
190+
}
191+
}
192+
}
193+
&.no-toc {
194+
/deep/ {
195+
.markdownIt-TOC {
196+
display: none;
197+
}
198+
}
49199
}
50200
}
51201
}

0 commit comments

Comments
 (0)