@@ -10,7 +10,7 @@ const scriptLangTsRE = /<\s*script[^>]*\blang=['"]ts['"][^>]*/;
10
10
const scriptSetupRE = / < \s * s c r i p t [ ^ > ] * \b s e t u p \b [ ^ > ] * / ;
11
11
const scriptClientRE = / < \s * s c r i p t [ ^ > ] * \b c l i e n t \b [ ^ > ] * / ;
12
12
let index = 1 ;
13
- function getDemoComponent ( md : any , env , { title, desc, path, code, ...props } : any ) {
13
+ function getDemoComponent ( md : any , env : any , { title, desc, path, code, enableIntersectionObserver , ...props } : any ) {
14
14
const componentName = `DemoComponent${ index ++ } ` ;
15
15
path = normalizePath ( path ) ;
16
16
injectImportStatement ( env , componentName , path ) ;
@@ -30,6 +30,7 @@ function getDemoComponent(md: any, env, { title, desc, path, code, ...props }: a
30
30
src="${ path } "
31
31
title="${ title ?? '' } "
32
32
desc="${ desc ?? '' } "
33
+ enableIntersectionObserver="${ enableIntersectionObserver ?? 'true' } "
33
34
importMap="${ encodeURIComponent ( importMap ) } "
34
35
>
35
36
<${ componentName } ></${ componentName } >
@@ -58,39 +59,34 @@ function genDemoByCode(md: any, env: any, path: string, code: string) {
58
59
}
59
60
60
61
function injectImportStatement ( env : any , componentName : string , path : string ) {
61
- const componentRegistStatement =
62
- `import ${ componentName } from '${ path } '` . trim ( )
62
+ const componentRegistStatement = `import ${ componentName } from '${ path } '` . trim ( ) ;
63
63
64
64
if ( ! env ?. sfcBlocks ?. scripts ) {
65
- env . sfcBlocks . scripts = [ ]
65
+ env . sfcBlocks . scripts = [ ] ;
66
66
}
67
- const tags = env . sfcBlocks . scripts as { content : string } [ ]
67
+ const tags = env . sfcBlocks . scripts as { content : string } [ ] ;
68
68
69
- const isUsingTS = tags . findIndex ( tag => scriptLangTsRE . test ( tag . content ) ) > - 1
70
- const existingSetupScriptIndex = tags ?. findIndex ( tag => {
71
- return (
72
- scriptRE . test ( tag . content ) &&
73
- scriptSetupRE . test ( tag . content ) &&
74
- ! scriptClientRE . test ( tag . content )
75
- )
76
- } )
69
+ const isUsingTS = tags . findIndex ( ( tag ) => scriptLangTsRE . test ( tag . content ) ) > - 1 ;
70
+ const existingSetupScriptIndex = tags ?. findIndex (
71
+ ( tag ) => scriptRE . test ( tag . content ) && scriptSetupRE . test ( tag . content ) && ! scriptClientRE . test ( tag . content ) ,
72
+ ) ;
77
73
78
74
if ( existingSetupScriptIndex > - 1 ) {
79
- const tagSrc = tags [ existingSetupScriptIndex ]
75
+ const tagSrc = tags [ existingSetupScriptIndex ] ;
80
76
tags [ existingSetupScriptIndex ] . content = tagSrc . content . replace (
81
77
scriptRE ,
82
78
`${ componentRegistStatement }
83
79
84
80
</script>` ,
85
- )
81
+ ) ;
86
82
} else {
87
83
tags . unshift ( {
88
84
content : `
89
85
<script ${ isUsingTS ? 'lang="ts"' : '' } setup >
90
86
${ componentRegistStatement }
91
87
</script>
92
88
` . trim ( ) ,
93
- } )
89
+ } ) ;
94
90
}
95
91
}
96
92
@@ -102,7 +98,7 @@ function demoBlockPlugin(md: any) {
102
98
const addRenderRule = ( type : string ) => {
103
99
const defaultRender = md . renderer . rules [ type ] ;
104
100
105
- md . renderer . rules [ type ] = ( tokens : any , idx : string , options , env , ...args : any [ ] ) => {
101
+ md . renderer . rules [ type ] = ( tokens : any , idx : string , options : any , env : any , ...args : any [ ] ) => {
106
102
const token = tokens [ idx ] ;
107
103
const content = token . content . trim ( ) ;
108
104
if ( ! content . startsWith ( `<${ DemoTag } ` ) ) {
@@ -117,7 +113,7 @@ function demoBlockPlugin(md: any) {
117
113
return defaultRender ( tokens , idx , options , env , ...args ) ;
118
114
}
119
115
120
- const frontmatter = env . frontmatter
116
+ const frontmatter = env . frontmatter ;
121
117
122
118
const mdDir = dirname ( frontmatter . realPath ?? path ) ;
123
119
const srcPath = resolve ( mdDir , props . src ) ;
@@ -127,13 +123,14 @@ function demoBlockPlugin(md: any) {
127
123
title : props . title ,
128
124
desc : props . desc ,
129
125
path : srcPath ,
126
+ enableIntersectionObserver : props . enableIntersectionObserver ,
130
127
code,
131
128
} ) ;
132
129
} ;
133
- }
130
+ } ;
134
131
135
- addRenderRule ( 'html_block' )
136
- addRenderRule ( 'html_inline' )
132
+ addRenderRule ( 'html_block' ) ;
133
+ addRenderRule ( 'html_inline' ) ;
137
134
}
138
135
139
136
function getPropsMap ( attrs : any ) {
@@ -147,14 +144,14 @@ function getPropsMap(attrs: any) {
147
144
148
145
export function parseProps ( content : string ) {
149
146
const ast = baseParse ( content ) ;
150
- const demoElement = ast . children [ 0 ] ;
147
+ const demoElement = ast . children [ 0 ] as any ;
151
148
152
149
return getPropsMap ( demoElement . props as any [ ] ) ;
153
150
}
154
151
155
152
function fencePlugin ( md : any ) {
156
153
const defaultRender = md . renderer . rules . fence ;
157
- md . renderer . rules . fence = ( tokens : any , idx : number , options , env , ...args : any [ ] ) => {
154
+ md . renderer . rules . fence = ( tokens : any , idx : number , options : any , env : any , ...args : any [ ] ) => {
158
155
const token = tokens [ idx ] ;
159
156
if ( token . info . trim ( ) !== FenceDemoTag ) {
160
157
return defaultRender ( tokens , idx , options , env , ...args ) ;
0 commit comments