@@ -219,39 +219,42 @@ export function injectGlobalRequire(origin: string) {
219
219
export function transformMarkdown ( buf : Buffer | string ) : string {
220
220
const source = buf . toString ( )
221
221
const output = [ ]
222
+ const tabRe = / ^ ( + | \t ) /
223
+ const codeBlockRe =
224
+ / ^ (?< fence > ( ` { 3 , 20 } | ~ { 3 , 20 } ) ) (?: (?< js > ( j s | j a v a s c r i p t | t s | t y p e s c r i p t ) ) | (?< bash > ( s h | s h e l l | b a s h ) ) | .* ) $ /
222
225
let state = 'root'
223
226
let codeBlockEnd = ''
224
227
let prevLineIsEmpty = true
225
- const jsCodeBlock = / ^ ( ` ` ` { 1 , 20 } | ~ ~ ~ { 1 , 20 } ) ( j s | j a v a s c r i p t | t s | t y p e s c r i p t ) $ /
226
- const shCodeBlock = / ^ ( ` ` ` { 1 , 20 } | ~ ~ ~ { 1 , 20 } ) ( s h | s h e l l | b a s h ) $ /
227
- const otherCodeBlock = / ^ ( ` ` ` { 1 , 20 } | ~ ~ ~ { 1 , 20 } ) ( .* ) $ /
228
228
for ( const line of source . split ( / \r ? \n / ) ) {
229
229
switch ( state ) {
230
230
case 'root' :
231
- if ( / ^ ( { 4 } | \t ) / . test ( line ) && prevLineIsEmpty ) {
231
+ if ( tabRe . test ( line ) && prevLineIsEmpty ) {
232
232
output . push ( line )
233
233
state = 'tab'
234
- } else if ( jsCodeBlock . test ( line ) ) {
235
- output . push ( '' )
234
+ continue
235
+ }
236
+ const { fence, js, bash } = line . match ( codeBlockRe ) ?. groups || { }
237
+ if ( ! fence ) {
238
+ prevLineIsEmpty = line === ''
239
+ output . push ( '// ' + line )
240
+ continue
241
+ }
242
+ codeBlockEnd = fence
243
+ if ( js ) {
236
244
state = 'js'
237
- codeBlockEnd = line . match ( jsCodeBlock ) ! [ 1 ]
238
- } else if ( shCodeBlock . test ( line ) ) {
239
- output . push ( 'await $`' )
240
- state = 'bash'
241
- codeBlockEnd = line . match ( shCodeBlock ) ! [ 1 ]
242
- } else if ( otherCodeBlock . test ( line ) ) {
243
245
output . push ( '' )
244
- state = 'other'
245
- codeBlockEnd = line . match ( otherCodeBlock ) ! [ 1 ]
246
+ } else if ( bash ) {
247
+ state = 'bash'
248
+ output . push ( 'await $`' )
246
249
} else {
247
- prevLineIsEmpty = line === ' '
248
- output . push ( '// ' + line )
250
+ state = 'other '
251
+ output . push ( '' )
249
252
}
250
253
break
251
254
case 'tab' :
252
255
if ( line === '' ) {
253
256
output . push ( '' )
254
- } else if ( / ^ ( + | \t ) / . test ( line ) ) {
257
+ } else if ( tabRe . test ( line ) ) {
255
258
output . push ( line )
256
259
} else {
257
260
output . push ( '// ' + line )
0 commit comments