@@ -83,6 +83,8 @@ function fixLinkTokens2(tokens: MarkdownToken[]): MarkdownToken[] {
8383 return tokens
8484 let length = tokens . length
8585 let last = tokens [ length - 1 ]
86+ if ( ! last )
87+ return tokens
8688 if ( last . type !== 'link_close' ) {
8789 length --
8890 last = tokens [ length - 1 ]
@@ -130,6 +132,9 @@ function fixLinkToken3(tokens: MarkdownToken[]): MarkdownToken[] {
130132 const last = tokens [ tokens . length - 1 ]
131133 const preLast = tokens [ tokens . length - 2 ]
132134 const fixedTokens = [ ...tokens ]
135+ if ( ! last )
136+ return tokens
137+
133138 if ( last . type !== 'text' || ! ( last as unknown as { content ?: string } ) . content ?. startsWith ( ')' ) ) {
134139 return tokens
135140 }
@@ -160,11 +165,11 @@ function fixLinkToken4(tokens: MarkdownToken[]): MarkdownToken[] {
160165 const fixedTokens = [ ...tokens ]
161166 for ( let i = tokens . length - 1 ; i >= 3 ; i -- ) {
162167 const token = tokens [ i ]
163- if ( token . type === 'link_close' ) {
168+ if ( token && token . type === 'link_close' ) {
164169 if ( tokens [ i - 3 ] ?. content ?. endsWith ( '(' ) ) {
165170 const nextToken = tokens [ i + 1 ]
166- if ( nextToken ?. type === 'text' ) {
167- if ( tokens [ i - 1 ] . type === 'text' && tokens [ i - 3 ] . type === 'text' ) {
171+ if ( nextToken && nextToken ?. type === 'text' ) {
172+ if ( tokens [ i - 1 ] . type === 'text' && tokens [ i - 3 ] ? .type === 'text' ) {
168173 const nextTokenContent = String ( ( nextToken as unknown as { content ?: string } ) . content ?? '' )
169174 const a = tokensAny [ i - 3 ] as unknown as { content ?: string }
170175 const b = tokensAny [ i - 1 ] as unknown as { content ?: string }
@@ -178,7 +183,7 @@ function fixLinkToken4(tokens: MarkdownToken[]): MarkdownToken[] {
178183 }
179184 }
180185 else {
181- if ( tokens [ i - 1 ] . type === 'text' && tokens [ i - 3 ] . type === 'text' ) {
186+ if ( tokens [ i - 1 ] . type === 'text' && tokens [ i - 3 ] ? .type === 'text' ) {
182187 const a = tokensAny [ i - 3 ] as unknown as { content ?: string }
183188 const b = tokensAny [ i - 1 ] as unknown as { content ?: string }
184189 const content = String ( a . content ?? '' ) + String ( b . content ?? '' )
0 commit comments