Skip to content

Commit b135f84

Browse files
committed
Add Vue 3 compatibility
- Update docs - Update tests - Add Vue 2 to docs site - Add Vue 2 & 3 dependencies for tests
1 parent 9b794f5 commit b135f84

File tree

8 files changed

+319
-112
lines changed

8 files changed

+319
-112
lines changed

docs/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,6 @@
162162
}
163163
})();
164164
</script>
165+
<script src="//cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js"></script>
165166
</body>
166167
</html>

docs/vue.md

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
# Vue compatibility
22

3-
Docsify allows [Vue.js](https://vuejs.org) components to be added directly to you Markdown files. These components can greatly simplify working with data and adding reactivity to your content.
3+
Docsify allows Vue [v2.x](https://vuejs.org) and [v3.x](https://v3.vuejs.org) components to be added directly to you Markdown files. These components can greatly simplify working with data and adding reactivity to your content.
44

5-
To get started, load either the production (minified) or development (unminified) version of Vue in your `index.html`:
5+
To get started, load either the production or development version of Vue in your `index.html`:
6+
7+
#### Vue 2.x
68

79
```html
8-
<!-- Production (minified) -->
10+
<!-- Production -->
911
<script src="//cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js"></script>
1012

11-
<!-- Development (unminified, with debugging info via console) -->
13+
<!-- Development (debugging and Vue.js devtools support) -->
1214
<script src="//cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
1315
```
1416

17+
#### Vue 3.x
18+
19+
```html
20+
<!-- Production -->
21+
<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
22+
23+
<!-- Development (debugging and Vue.js devtools support) -->
24+
<script src="//cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
25+
```
26+
1527
## Basic rendering
1628

1729
Docsify will automatically render basic Vue content that does not require `data`, `methods`, or other instance features.
1830

1931
```markdown
20-
<button v-on:click.native="this.alert('Hello, World!')">Say Hello</button>
21-
2232
<ul>
2333
<li v-for="i in 3">{{ i }}</li>
2434
</ul>
2535
```
2636

2737
The HTML above will render the following:
2838

29-
<button v-on:click="this.alert('Hello, World!')">Say Hello</button>
30-
3139
<ul>
3240
<li v-for="i in 3">{{ i }}</li>
3341
</ul>
@@ -36,6 +44,8 @@ The HTML above will render the following:
3644

3745
Vue components and templates that require `data`, `methods`, computed properties, lifecycle hooks, etc. require manually creating a new `Vue()` instance within a `<script>` tag in your markdown.
3846

47+
<!-- prettier-ignore-start -->
48+
3949
```markdown
4050
<div id="example-1">
4151
<p>{{ message }}</p>
@@ -48,13 +58,19 @@ Vue components and templates that require `data`, `methods`, computed properties
4858
</div>
4959
```
5060

61+
<!-- prettier-ignore-end -->
62+
63+
#### Vue 2.x
64+
5165
```markdown
5266
<script>
5367
new Vue({
5468
el: "#example-1",
5569
data: function() {
56-
counter: 0,
57-
message: "Hello, World!"
70+
return {
71+
counter: 0,
72+
message: "Hello, World!"
73+
};
5874
},
5975
methods: {
6076
hello: function() {
@@ -65,8 +81,30 @@ Vue components and templates that require `data`, `methods`, computed properties
6581
</script>
6682
```
6783

84+
#### Vue 3.x
85+
86+
```markdown
87+
<script>
88+
Vue.createApp({
89+
data: function() {
90+
return {
91+
counter: 0,
92+
message: "Hello, World!"
93+
};
94+
},
95+
methods: {
96+
hello: function() {
97+
alert(this.message);
98+
}
99+
}
100+
}).mount("#example-1");
101+
</script>
102+
```
103+
68104
The HTML & JavaScript above will render the following:
69105

106+
<!-- prettier-ignore-start -->
107+
70108
<div id="example-1">
71109
<p>{{ message }}</p>
72110

@@ -77,64 +115,18 @@ The HTML & JavaScript above will render the following:
77115
<button v-on:click="counter += 1">+</button>
78116
</div>
79117

80-
!> Only the first `<script>` tag in a markdown file is executed. If you are working with multiple Vue components, all `Vue` instances must be created within this tag.
81-
82-
## Vuep playgrounds
83-
84-
[Vuep](https://github.com/QingWei-Li/vuep) is a Vue component that provides a live editor and preview for Vue content. See the [vuep documentation](https://qingwei-li.github.io/vuep/) for details.
85-
86-
Add Vuep CSS and JavaScript to your `index.html`:
87-
88-
```html
89-
<!-- Vuep CSS -->
90-
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/vuep/dist/vuep.css">
91-
92-
<!-- Vuep JavaScript -->
93-
<script src="//cdn.jsdelivr.net/npm/vuep/dist/vuep.min.js"></script>
94-
```
95-
96-
Add vuep markup to a markdown file (e.g. `README.md`):
97-
98-
```markdown
99-
<vuep template="#example-2"></vuep>
100-
101-
<script v-pre type="text/x-template" id="example-2">
102-
<template>
103-
<div>Hello, {{ name }}!</div>
104-
</template>
118+
<!-- prettier-ignore-end -->
105119

106-
<script>
107-
module.exports = {
108-
data: function() {
109-
return { name: 'Vue' }
110-
}
111-
}
112-
</script>
113-
</script>
114-
```
115-
116-
<vuep template="#example-2"></vuep>
117-
118-
<script v-pre type="text/x-template" id="example-2">
119-
<template>
120-
<div>Hello, {{ name }}!</div>
121-
</template>
122-
123-
<script>
124-
module.exports = {
125-
data: function() {
126-
return { name: 'World' }
127-
}
128-
}
129-
</script>
130-
</script>
120+
!> Only the first `<script>` tag in a markdown file is executed. If you are working with multiple Vue components, all `Vue` instances must be created within this tag.
131121

132122
<script>
133123
new Vue({
134124
el: "#example-1",
135-
data: {
136-
counter: 0,
137-
message: "Hello, World!"
125+
data: function() {
126+
return {
127+
counter: 0,
128+
message: "Hello, World!"
129+
};
138130
},
139131
methods: {
140132
hello: function() {

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { globals: serverGlobals } = require('./test/config/server.js');
44
const sharedConfig = {
55
errorOnDeprecated: true,
66
globals: {
7-
...serverGlobals, // BLANK_URL, DOCS_URL, LIB_URL, TEST_HOST
7+
...serverGlobals, // BLANK_URL, DOCS_URL, LIB_URL, NODE_MODULES_URL, TEST_HOST
88
DOCS_PATH: path.resolve(__dirname, 'docs'),
99
LIB_PATH: path.resolve(__dirname, 'lib'),
1010
SRC_PATH: path.resolve(__dirname, 'src'),

package-lock.json

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
"rollup-plugin-uglify": "^6.0.4",
104104
"serve-handler": "^6.1.2",
105105
"stylus": "^0.54.5",
106+
"vue2": "npm:vue@^2.6.12",
107+
"vue3": "npm:vue@^3.0.0",
106108
"xhr-mock": "^2.5.1"
107109
},
108110
"keywords": [

src/core/render/index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,38 @@ function renderMain(html) {
6262
if ('Vue' in window) {
6363
const mainElm = document.querySelector('#main') || {};
6464
const childElms = mainElm.children || [];
65+
const vueVersion =
66+
window.Vue.version && Number(window.Vue.version.charAt(0));
6567

6668
for (let i = 0, len = childElms.length; i < len; i++) {
6769
const elm = childElms[i];
6870
const isValid = elm.tagName !== 'SCRIPT';
69-
const isVue = Boolean(elm.__vue__ && elm.__vue__._isVue);
7071

71-
if (isValid && !isVue) {
72-
new window.Vue({
73-
mounted: function() {
74-
this.$destroy;
75-
},
76-
}).$mount(elm);
72+
if (!isValid) {
73+
continue;
74+
}
75+
76+
// Vue 3
77+
if (vueVersion === 3) {
78+
const isAlreadyVue = Boolean(elm._vnode && elm._vnode.__v_skip);
79+
80+
if (!isAlreadyVue) {
81+
const app = window.Vue.createApp({});
82+
83+
app.mount(elm);
84+
}
85+
}
86+
// Vue 2
87+
else if (vueVersion === 2) {
88+
const isAlreadyVue = Boolean(elm.__vue__ && elm.__vue__._isVue);
89+
90+
if (!isAlreadyVue) {
91+
new window.Vue({
92+
mounted: function() {
93+
this.$destroy();
94+
},
95+
}).$mount(elm);
96+
}
7797
}
7898
}
7999
}

0 commit comments

Comments
 (0)