@@ -2,7 +2,9 @@ const MarkdownIt = require('markdown-it');
2
2
const cheerio = require ( 'cheerio' ) ;
3
3
const path = require ( 'path' ) ;
4
4
const postcss = require ( 'postcss' ) ;
5
+ const vue = require ( 'vue' ) ;
5
6
7
+ const isVue3 = vue . version && vue . version [ 0 ] > 2 ;
6
8
const defaultOptions = {
7
9
cheerioLoadOptions : {
8
10
decodeEntities : false ,
@@ -62,9 +64,7 @@ function normalizeComponent(script, mixin) {
62
64
component = {};
63
65
}
64
66
65
- component.mixins = (component.mixins || []).concat([${ mixin } ]);
66
-
67
- return component;
67
+ return Object.assign(component, ${ mixin } );
68
68
}())` ;
69
69
}
70
70
@@ -110,7 +110,7 @@ module.exports = function markdownToVueLoader(source, map) {
110
110
}
111
111
112
112
if ( commentOption !== 'no-vue-component' ) {
113
- $pre . children ( 'code' ) . each ( ( i , code ) => {
113
+ $pre . children ( 'code' ) . each ( ( idx , code ) => {
114
114
const $code = $ ( code ) ;
115
115
const language = $code . attr ( 'class' ) . replace ( REGEXP_LANGUAGE_PREFIXES , '' ) ;
116
116
@@ -119,28 +119,30 @@ module.exports = function markdownToVueLoader(source, map) {
119
119
}
120
120
121
121
const mixin = [ ] ;
122
+ let template = '' ;
122
123
let component ;
123
124
let scoped ;
124
125
let style ;
125
- let template ;
126
126
127
127
switch ( language ) {
128
128
case 'vue' : {
129
- const $html = cheerio . load ( $code . text ( ) , cheerioLoadOptions ) ;
130
- const $style = $html ( 'style' ) ;
129
+ const $$ = cheerio . load ( $code . text ( ) , cheerioLoadOptions ) ;
130
+ const $style = $$ ( 'style' ) ;
131
131
132
- component = $html ( 'script' ) . html ( ) || 'export default {};' ;
132
+ component = $$ ( 'script' ) . html ( ) || 'export default {};' ;
133
133
scoped = $style . attr ( 'scoped' ) ;
134
134
style = $style . html ( ) ;
135
- template = $html ( 'template' ) . html ( ) ;
135
+ $$ ( 'template' ) . each ( ( i , element ) => {
136
+ template += $ ( element ) . html ( ) ;
137
+ } ) ;
136
138
break ;
137
139
}
138
140
139
141
case 'html' : {
140
- const $html = cheerio . load ( $code . text ( ) , cheerioLoadOptions ) ;
141
- const $body = $html ( 'body' ) ;
142
- const $script = $html ( 'script' ) ;
143
- const $style = $html ( 'style' ) ;
142
+ const $$ = cheerio . load ( $code . text ( ) , cheerioLoadOptions ) ;
143
+ const $body = $$ ( 'body' ) ;
144
+ const $script = $$ ( 'script' ) ;
145
+ const $style = $$ ( 'style' ) ;
144
146
145
147
component = $script . html ( ) || 'export default {};' ;
146
148
scoped = $style . attr ( 'scoped' ) ;
@@ -149,7 +151,12 @@ module.exports = function markdownToVueLoader(source, map) {
149
151
$style . remove ( ) ;
150
152
151
153
// Move <template> from <head> to <body>
152
- $body . append ( $html ( 'head template' ) ) ;
154
+ $body . append ( $$ ( 'head template' ) ) ;
155
+ $body . find ( 'template' ) . each ( ( i , element ) => {
156
+ const $element = $ ( element ) ;
157
+
158
+ $element . replaceWith ( $element . html ( ) ) ;
159
+ } ) ;
153
160
template = $body . html ( ) ;
154
161
break ;
155
162
}
@@ -189,7 +196,7 @@ module.exports = function markdownToVueLoader(source, map) {
189
196
document.head.appendChild(style);
190
197
this.$styleInjectedByMarkdownToVueLoader = style;
191
198
}` ) ;
192
- mixin . push ( `beforeDestroy : function () {
199
+ mixin . push ( `before ${ isVue3 ? 'Unmount' : 'Destroyed' } : function () {
193
200
var $style = this.$styleInjectedByMarkdownToVueLoader;
194
201
$style.parentNode.removeChild($style);
195
202
}` ) ;
@@ -237,18 +244,18 @@ module.exports = function markdownToVueLoader(source, map) {
237
244
}
238
245
} ) ;
239
246
240
- const $html = cheerio . load ( '<template></template>' ) ;
241
- const $body = $html ( 'body' ) ;
247
+ const $$ = cheerio . load ( '<template></template>' ) ;
248
+ const $body = $$ ( 'body' ) ;
242
249
243
- $body . append ( $html ( 'head template' ) ) ;
250
+ $body . append ( $$ ( 'head template' ) ) ;
244
251
245
- $ ( 'style' ) . each ( ( i , style ) => {
252
+ $ ( 'style' ) . each ( ( index , style ) => {
246
253
const $style = $ ( style ) ;
247
254
248
255
$body . append ( $style ) ;
249
256
} ) ;
250
257
251
- $html ( 'template' ) . append ( `<div class="${ options . componentNamespace } -${ normalizedResourceName } ">${ $ ( 'body' ) . html ( ) } </div>` ) ;
258
+ $$ ( 'template' ) . append ( `<div class="${ options . componentNamespace } -${ normalizedResourceName } ">${ $ ( 'body' ) . html ( ) } </div>` ) ;
252
259
253
260
if ( options . exportSource || components . length > 0 ) {
254
261
$body . append ( `<script>
@@ -259,5 +266,5 @@ module.exports = function markdownToVueLoader(source, map) {
259
266
</script>` ) ;
260
267
}
261
268
262
- this . callback ( null , $html ( 'body' ) . html ( ) , map ) ;
269
+ this . callback ( null , $$ ( 'body' ) . html ( ) , map ) ;
263
270
} ;
0 commit comments