@@ -19,19 +19,39 @@ export const insertDocsHeaderInfo: PluginSimple = md => {
19
19
20
20
export const imgToImage : PluginSimple = md => {
21
21
const defaultRender = md . renderer . render ;
22
- md . renderer . rules . image = ( tokens , idx ) => {
23
- const token = tokens [ idx ] ;
24
- const srcIndex = token . attrIndex ( 'src' ) ;
25
- const altIndex = token . attrIndex ( 'alt' ) ;
22
+ let imgs : string [ ] = [ ] ;
26
23
27
- const src = token ?. attrs ?. [ srcIndex ] [ 1 ] ;
28
- const alt = altIndex >= 0 ? token ?. attrs ?. [ altIndex ] [ 1 ] : '' ;
24
+ // Add a rule to collect image src attributes
25
+ md . core . ruler . push ( 'collect_image_src' , state => {
26
+ imgs = [ ] ;
27
+ state . tokens . forEach ( token => {
28
+ if ( token . type === 'inline' && token . children ) {
29
+ token . children . forEach ( child => {
30
+ if ( child . type === 'image' ) {
31
+ const src = child . attrGet ( 'src' ) ;
32
+ if ( src ) {
33
+ imgs . push ( src ) ;
34
+ }
35
+ }
36
+ } ) ;
37
+ }
38
+ } ) ;
39
+ } ) ;
29
40
30
- return `<Image src="${ src } " alt="${ alt } " />` ;
41
+ md . renderer . rules . image = ( tokens , idx , _opt , env ) => {
42
+ const token = tokens [ idx ] ;
43
+ const src = token . attrGet ( 'src' ) ;
44
+ const alt = token . attrGet ( 'alt' ) ;
45
+ if ( ! env . imgs ) {
46
+ env . imgs = [ ] ;
47
+ }
48
+ env . imgs . push ( { src, index : env . imgs . length } ) ;
49
+ return `<Image src="${ src } " alt="${ alt } " index="${ env . imgs . length - 1 } "/>` ;
31
50
} ;
51
+
32
52
md . renderer . render = ( ...args ) => {
33
53
const content = defaultRender . apply ( md . renderer , args ) ;
34
- return `<ImageGroup>< ContentWrapper>${ content } </ContentWrapper></ImageGroup >` ;
54
+ return `<ContentWrapper imgsStr=" ${ imgs . join ( ',' ) } " >${ content } </ContentWrapper>` ;
35
55
} ;
36
56
} ;
37
57
0 commit comments