Skip to content

Commit 1813bc4

Browse files
committed
docs: improve Codepen examples by transpile all scripts and styles
1 parent 27e9e7d commit 1813bc4

File tree

10 files changed

+193
-117
lines changed

10 files changed

+193
-117
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ before_script:
2323
- chmod +x ./cc-test-reporter
2424
- ./cc-test-reporter before-build
2525
script:
26+
- npm rebuild node-sass
2627
- yarn lint
2728
- yarn coverage
2829
- yarn build

build/loaders/component-example-loader.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1+
const sass = require('node-sass')
2+
const pretty = require('pretty')
3+
const prettier = require('prettier')
14
const path = require('path')
25
const compiler = require('vue-template-compiler')
36
const transpile = require('vue-template-es2015-compiler')
7+
const { resolvePath } = require('../config')
8+
const { getIndentedSource } = require('../../docs/app/mixins/codeSource')
49

510
function camelCaseToDash (str) {
611
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
712
}
813

14+
function getComponentScript (script) {
15+
return getIndentedSource(`
16+
Vue.use(VueMaterial.default)
17+
18+
${script}
19+
20+
const app = new Vue(example)
21+
22+
app.$mount('#app')
23+
`)
24+
}
25+
26+
function getComponentTemplate (template) {
27+
return pretty(`
28+
<div id="app">
29+
${template}
30+
</div>
31+
`)
32+
}
33+
934
module.exports = function (source) {
1035
this.cacheable && this.cacheable()
1136

@@ -31,8 +56,36 @@ module.exports = function (source) {
3156

3257
let { script, template, style } = parsedTags
3358

59+
if (style) {
60+
style = style.replace(/~vue-material/g, '.')
61+
62+
const { css } = sass.renderSync({
63+
data: style,
64+
includePaths: [resolvePath('src')],
65+
outputStyle: 'expanded'
66+
})
67+
68+
style = css.toString('utf8')
69+
}
70+
71+
if (template) {
72+
template = template.replace(/src="\/assets/g, 'src="https://vuematerial.io/assets')
73+
template = getComponentTemplate(template)
74+
}
75+
3476
if (script) {
35-
script = script.replace('export default ', '')
77+
let newScript = null
78+
79+
script = script.replace('export default ', 'const example = ')
80+
script = getComponentScript(script)
81+
82+
try {
83+
newScript = transpile(script)
84+
} catch (e) {
85+
newScript = script
86+
}
87+
88+
script = prettier.format(newScript, { semi: false })
3689
}
3790

3891
const code = `

docs/app/components/CodeBlock.vue

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,65 @@
1010
</div>
1111
</template>
1212

13+
<script>
14+
import highlight from 'highlight.js/lib/highlight.js'
15+
import highlightSCSS from 'highlight.js/lib/languages/scss'
16+
import highlightXML from 'highlight.js/lib/languages/xml'
17+
import highlightJavascript from 'highlight.js/lib/languages/javascript'
18+
import highlightShell from 'highlight.js/lib/languages/shell'
19+
import Clipboard from 'clipboard'
20+
import codeSource from 'docs-mixins/codeSource'
21+
22+
highlight.registerLanguage('scss', highlightSCSS)
23+
highlight.registerLanguage('xml', highlightXML)
24+
highlight.registerLanguage('javascript', highlightJavascript)
25+
highlight.registerLanguage('shell', highlightShell)
26+
27+
export default {
28+
name: 'CodeBlock',
29+
mixins: [codeSource],
30+
props: {
31+
lang: String,
32+
label: String,
33+
height: {
34+
type: [Number, String],
35+
default: '450px'
36+
}
37+
},
38+
data: () => ({
39+
showMessage: false
40+
}),
41+
methods: {
42+
enableCopy () {
43+
if (this.$refs.copy) {
44+
const clipboard = new Clipboard(this.$refs.copy.$el, {
45+
target: () => this.$refs.block
46+
})
47+
let timer = null
48+
49+
clipboard.on('success', (event) => {
50+
event.clearSelection()
51+
this.showMessage = true
52+
53+
window.clearTimeout(timer)
54+
timer = window.setTimeout(() => {
55+
this.showMessage = false
56+
}, 2000)
57+
})
58+
}
59+
}
60+
},
61+
async mounted () {
62+
await this.$nextTick()
63+
64+
this.reindentSource()
65+
this.enableCopy()
66+
67+
highlight.highlightBlock(this.$refs.block)
68+
}
69+
}
70+
</script>
71+
1372
<style lang="scss" scoped>
1473
@import "~vue-material/components/MdAnimation/variables";
1574
@import "~vue-material/theme/engine";
@@ -193,81 +252,3 @@
193252
color: #bbdf88;
194253
}
195254
</style>
196-
197-
<script>
198-
import highlight from 'highlight.js/lib/highlight.js'
199-
import highlightSCSS from 'highlight.js/lib/languages/scss'
200-
import highlightXML from 'highlight.js/lib/languages/xml'
201-
import highlightJavascript from 'highlight.js/lib/languages/javascript'
202-
import highlightShell from 'highlight.js/lib/languages/shell'
203-
import Clipboard from 'clipboard'
204-
205-
highlight.registerLanguage('scss', highlightSCSS)
206-
highlight.registerLanguage('xml', highlightXML)
207-
highlight.registerLanguage('javascript', highlightJavascript)
208-
highlight.registerLanguage('shell', highlightShell)
209-
210-
export default {
211-
name: 'CodeBlock',
212-
props: {
213-
lang: String,
214-
label: String,
215-
height: {
216-
type: [Number, String],
217-
default: '450px'
218-
}
219-
},
220-
data: () => ({
221-
showMessage: false
222-
}),
223-
methods: {
224-
enableCopy () {
225-
if (this.$refs.copy) {
226-
const clipboard = new Clipboard(this.$refs.copy.$el, {
227-
target: () => this.$refs.block
228-
})
229-
let timer = null
230-
231-
clipboard.on('success', (event) => {
232-
event.clearSelection()
233-
this.showMessage = true
234-
235-
window.clearTimeout(timer)
236-
timer = window.setTimeout(() => {
237-
this.showMessage = false
238-
}, 2000)
239-
})
240-
}
241-
},
242-
reindentSource () {
243-
const block = this.$refs.block
244-
let lines = block.textContent.split('\n')
245-
let matches
246-
247-
if (lines[0] === '') {
248-
lines.shift()
249-
}
250-
251-
let indentation = (matches = (/^[\s\t]+/).exec(lines[0])) !== null ? matches[0] : null
252-
253-
if (indentation) {
254-
lines = lines.map(line => {
255-
line = line.replace(indentation, '')
256-
257-
return line.replace(/\t/g, ' ')
258-
})
259-
260-
block.textContent = lines.join('\n').trim()
261-
}
262-
}
263-
},
264-
async mounted () {
265-
await this.$nextTick()
266-
267-
this.reindentSource()
268-
this.enableCopy()
269-
270-
highlight.highlightBlock(this.$refs.block)
271-
}
272-
}
273-
</script>

docs/app/components/CodepenEdit.vue

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,27 @@
2222
component: Object
2323
},
2424
computed: {
25-
/* eslint-disable */
2625
styles () {
2726
return this.component.style ? this.component.style : ''
2827
},
2928
html () {
30-
return `
31-
<div id="app">
32-
${this.component.template}
33-
</div>
34-
`
29+
return this.component.template
3530
},
3631
script () {
37-
return `
38-
Vue.use(VueMaterial.default)
39-
40-
const example = ${this.component.script}
41-
42-
new Vue({
43-
name: 'root',
44-
el: '#app',
45-
...example
46-
})
47-
`
32+
return this.component.script
4833
},
4934
externalScripts () {
5035
return [
51-
'https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser.js',
52-
'https://unpkg.com/vue',
36+
'https://unpkg.com/babel-standalone/babel.min.js',
37+
'https://unpkg.com/vue/dist/vue.min.js',
5338
`${vueMaterialUrl}`
5439
].join(';')
5540
},
5641
externalStyles () {
5742
return [
58-
`https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic`,
59-
`https://fonts.googleapis.com/icon?family=Material+Icons`,
43+
'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Material+Icons',
6044
`${vueMaterialUrl}/dist/vue-material.min.css`,
61-
'https://unpkg.com/vue-material@beta/dist/theme/default.css'
45+
`${vueMaterialUrl}/dist/theme/default.css`
6246
].join(';')
6347
},
6448
apiJson () {
@@ -72,11 +56,9 @@ new Vue({
7256
html: `${this.html}`,
7357
html_pre_processor: 'none',
7458
css: `${this.styles}`,
75-
css_pre_processor: 'scss',
7659
css_starter: 'neither',
7760
css_prefix: 'autoprefixer',
7861
js: `${this.script}`,
79-
js_pre_processor: 'babel',
8062
html_classes: '',
8163
head: '<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">',
8264
css_external: `${this.externalStyles}`,
@@ -87,7 +69,6 @@ new Vue({
8769
apiContent () {
8870
return JSON.stringify(this.apiJson).replace(/"/g, '&​quot;').replace(/'/g, '&apos;');
8971
}
90-
/* eslint-enable */
9172
}
9273
}
9374
</script>

docs/app/mixins/codeSource.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const getIndentedSource = source => {
2+
let lines = source.split('\n')
3+
let matches
4+
5+
if (lines[0] === '') {
6+
lines.shift()
7+
}
8+
9+
let indentation = (matches = (/^[\s\t]+/).exec(lines[0])) !== null ? matches[0] : null
10+
11+
if (indentation) {
12+
lines = lines.map(line => {
13+
line = line.replace(indentation, '')
14+
15+
return line.replace(/\t/g, ' ')
16+
})
17+
18+
return lines.join('\n').trim()
19+
}
20+
21+
return source
22+
}
23+
24+
export default {
25+
getIndentedSource,
26+
reindentSource () {
27+
const block = this.$refs.block
28+
29+
block.textContent = getIndentedSource(block.textContent)
30+
}
31+
}

docs/app/pages/Components/Button/examples/RegularButtons.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</style>
3434

3535
<script>
36-
export default {
37-
name: 'RegularButtons'
38-
}
36+
export default {
37+
name: 'RegularButtons'
38+
}
3939
</script>

docs/app/pages/Components/Datepicker/examples/DisabledDatesDatepicker.vue

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
</template>
66

77
<script>
8-
function isWeekend (date) {
9-
const day = date.getDay()
10-
11-
return day === 6 || day === 0
12-
}
13-
148
export default {
159
name: 'DisabledDatesDatepicker',
1610
data: () => ({
1711
selectedDate: null,
18-
disabledDates: date => isWeekend(date)
12+
disabledDates: date => {
13+
const day = date.getDay()
14+
15+
return day === 6 || day === 0
16+
}
1917
})
2018
}
2119
</script>

docs/app/pages/Components/Form/examples/FormValidation.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
</template>
7272

7373
<script>
74-
import Vue from 'vue'
7574
import { validationMixin } from 'vuelidate'
7675
import {
7776
required,

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
"postcss": "^6.0.14",
101101
"preload-webpack-plugin": "^2.0.0",
102102
"prerender-spa-plugin": "^2.1.0",
103+
"prettier": "^1.8.2",
104+
"pretty": "^2.0.0",
103105
"sass-loader": "^6.0.6",
104106
"sinon": "^4.1.1",
105107
"url-loader": "^0.6.2",

0 commit comments

Comments
 (0)