@@ -6,53 +6,34 @@ const defaultOption = {
6
6
const imageFlowPlugin = ( md , opt ) => {
7
7
const options = opt || defaultOption ;
8
8
9
- const tokenize = ( state , silent ) => {
10
- let result = false ;
9
+ const tokenize = ( state , start ) => {
11
10
let token ;
11
+
12
12
const matchReg = / ^ < ( ( ! \[ ( [ ^ \] ] ) * \] \( ( [ ^ ) ] ) * \) ( , ? ) ( \s ) * ) * ) > / ;
13
+ const srcLine = state . src . slice ( state . bMarks [ start ] , state . eMarks [ start ] ) ;
13
14
14
- if ( silent ) {
15
- return result ;
16
- }
17
- if ( state . src . charCodeAt ( state . pos ) !== 0x3c /* < */ ) {
18
- return result ;
15
+ if ( srcLine . charCodeAt ( 0 ) !== 0x3c /* < */ ) {
16
+ return false ;
19
17
}
20
-
21
- const match = matchReg . exec ( state . src . substr ( state . pos ) ) ;
18
+ const match = matchReg . exec ( srcLine ) ;
22
19
23
20
if ( match ) {
24
- if ( options . limitless ) {
25
- result = true ;
26
- } else if ( match [ 1 ] . split ( / , | \s / ) . filter ( ( val ) => val ) . length < options . limit ) {
27
- result = true ;
28
- } else {
29
- return result ;
30
- }
31
-
32
- token = state . push ( "imageFlow_start" , "imageFlow" , 1 ) ;
33
- token = state . push ( "imageFlow_content" , "imageFlow" , 0 ) ;
34
- [ , token . content ] = match ;
35
- token = state . push ( "imageFlow_end" , "imageFlow" , - 1 ) ;
36
-
37
- // update position
38
- var newline = state . src . indexOf ( "\n" , state . pos ) ;
39
- if ( newline !== - 1 ) {
40
- state . pos = newline ;
41
- } else {
42
- state . pos = state . pos + state . posMax + 1 ;
21
+ if ( ! options . limitless && match [ 1 ] . split ( / , | \s / ) . filter ( ( val ) => val ) . length < options . limit ) {
22
+ token = state . push ( "imageFlow" , "" , 0 ) ;
23
+ [ , token . content ] = match ;
24
+ token . block = true ;
25
+
26
+ // update line
27
+ state . line ++ ;
28
+ return true ;
43
29
}
44
30
}
45
-
46
- return result ;
31
+ return false ;
47
32
} ;
48
33
49
- md . renderer . rules . imageFlow_start = ( ) => {
50
- return `<section class="imageflow-layer1"><section class="imageflow-layer2">` ;
51
- } ;
52
- md . renderer . rules . imageFlow_end = ( ) => {
53
- return `</section></section>` ;
54
- } ;
55
- md . renderer . rules . imageFlow_content = ( tokens , idx ) => {
34
+ md . renderer . rules . imageFlow = ( tokens , idx ) => {
35
+ const start = `<section class="imageflow-layer1"><section class="imageflow-layer2">` ;
36
+ const end = `</section></section>` ;
56
37
const contents = tokens [ idx ] . content . split ( / , | \s / ) . filter ( ( val ) => val ) ;
57
38
let wrapperContent = "" ;
58
39
let image ;
@@ -63,10 +44,10 @@ const imageFlowPlugin = (md, opt) => {
63
44
} class="imageflow-img" /></section>`;
64
45
} ) ;
65
46
66
- return wrapperContent ;
47
+ return start + wrapperContent + end ;
67
48
} ;
68
49
69
- md . inline . ruler . before ( "image " , "imageFlow" , tokenize ) ;
50
+ md . block . ruler . before ( "paragraph " , "imageFlow" , tokenize ) ;
70
51
} ;
71
52
72
53
export default imageFlowPlugin ;
0 commit comments