Skip to content

Commit 5491aba

Browse files
author
savoygu
committed
Add code segment support
1 parent 918e2ad commit 5491aba

File tree

4 files changed

+68
-26
lines changed

4 files changed

+68
-26
lines changed

example/src/App.vue

+21-3
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@
246246
通过 <code>slot="table"</code> 添加表格, 通过 <code>class="vd-demo__table"</code> 设置表格样式,还可以通过 <code>class="vd-demo__new"</code> 增加特殊标识。
247247
</template>
248248
</vue-demonstration>
249+
<vue-demonstration
250+
anchor="zhan-shi-code"
251+
:show-content="false"
252+
:code="sourcecodeK">
253+
<template slot="title">
254+
展示代码 <i class="vd-demo__new">(1.0.2 新增)</i>
255+
</template>
256+
</vue-demonstration>
249257
<vue-demonstration
250258
anchor="attributes"
251259
title="Attributes"
@@ -273,7 +281,15 @@
273281
<td>string</td>
274282
<td>—</td>
275283
<td>—</td>
276-
</tr><tr>
284+
</tr>
285+
<tr>
286+
<td>code<i class="vd-demo__new">(1.0.2 新增)</i></td>
287+
<td>要展示的代码片段</td>
288+
<td>string</td>
289+
<td>—</td>
290+
<td>—</td>
291+
</tr>
292+
<tr>
277293
<td>tag</td>
278294
<td>标题标签</td>
279295
<td>string</td>
@@ -369,7 +385,8 @@
369385
sourcecodeG,
370386
sourcecodeH,
371387
sourcecodeI,
372-
sourcecodeJ
388+
sourcecodeJ,
389+
sourcecodeK
373390
} from './template'
374391
375392
export default {
@@ -385,7 +402,8 @@
385402
sourcecodeG,
386403
sourcecodeH,
387404
sourcecodeI,
388-
sourcecodeJ
405+
sourcecodeJ,
406+
sourcecodeK
389407
}
390408
}
391409
}

example/src/template.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,6 @@ export const sourcecodeJ = `<vue-demonstration
261261
<template slot="explanation">
262262
通过 <code>slot="table"</code> 添加表格, 通过 <code>class="vd-demo__table"</code> 设置表格样式,还可以通过 <code>class="vd-demo__new"</code> 增加特殊标识。
263263
</template>
264-
</vue-demonstration>`
264+
</vue-demonstration>`
265+
266+
export const sourcecodeK = `import VueDemonstration from 'vue-demonstration'`

lib/vue-demonstration.js

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

src/components/Demo.vue

+43-21
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
:tag="tag"
77
:has-slot="!!$slots.title"></vd-demo-title>
88
<vd-demo-description
9-
:description="description"
10-
:has-slot="!!$slots.description">
9+
:description="description"
10+
:has-slot="!!$slots.description">
1111
</vd-demo-description>
1212
<slot name="table"></slot>
13+
<slot name="code">
14+
<div class="vd-demo__code" v-if="code">
15+
<pre v-highlightjs="code"><code class="html language-javascript"></code></pre>
16+
</div>
17+
</slot>
1318
<div class="vd-demo__content" v-if="showContent">
1419
<div class="vd-demo__source" :style="{padding: multiple ? '0' : '24px'}" ref="source">
1520
<slot name="source"></slot>
@@ -46,6 +51,7 @@
4651
props: {
4752
multiple: Boolean,
4853
highlight: String,
54+
code: String,
4955
tag: {
5056
type: String,
5157
default: 'h3'
@@ -81,9 +87,9 @@
8187
if (source) {
8288
let blocks = source.querySelectorAll('.vd-demo__block-1')
8389
if (
84-
blocks.length
85-
&& blocks.length % 2 === 1
86-
&& blocks.length - 2 > 0
90+
blocks.length
91+
&& blocks.length % 2 === 1
92+
&& blocks.length - 2 > 0
8793
) {
8894
blocks[blocks.length - 2].style.borderBottom = '1px solid #eff2f6'
8995
}
@@ -113,23 +119,23 @@
113119
},
114120
render (h) {
115121
return h(this.tag, {
116-
attrs: {
117-
id: this.anchor
118-
}
122+
attrs: {
123+
id: this.anchor
124+
}
125+
},
126+
[
127+
h('a', {
128+
attrs: {
129+
href: '#' + this.anchor,
130+
'aria-hidden': true,
131+
class: 'header-anchor'
119132
},
120-
[
121-
h('a', {
122-
attrs: {
123-
href: '#' + this.anchor,
124-
'aria-hidden': true,
125-
class: 'header-anchor'
126-
},
127-
domProps: {
128-
innerHTML: ''
129-
}
130-
}),
131-
this.title ? this.title : (this.hasSlot ? this.$parent.$slots.title : '' )
132-
])
133+
domProps: {
134+
innerHTML: ''
135+
}
136+
}),
137+
this.title ? this.title : (this.hasSlot ? this.$parent.$slots.title : '' )
138+
])
133139
}
134140
},
135141
'vd-demo-description': {
@@ -161,6 +167,7 @@
161167
</script>
162168
<style lang="less" rel="stylesheet/less">
163169
@import "../styles/less/style";
170+
164171
.vd-demo {
165172
width: 1000px;
166173
margin: 0 auto;
@@ -314,6 +321,21 @@
314321
line-height: 18px;
315322
}
316323
}
324+
&__code {
325+
pre {
326+
.hljs {
327+
line-height: 1.8;
328+
font-family: Menlo, Monaco, Consolas, Courier, monospace;
329+
font-size: 12px;
330+
padding: 18px 24px;
331+
background-color: #f9fafc;
332+
border: 1px solid #eaeefb;
333+
margin-bottom: 25px;
334+
border-radius: 4px;
335+
-webkit-font-smoothing: auto;
336+
}
337+
}
338+
}
317339
&__highlight {
318340
width: 60%;
319341
border-right: 1px solid #eaeefb;

0 commit comments

Comments
 (0)