@@ -28,11 +28,12 @@ import {
2828 path ,
2929 stdin ,
3030 VERSION ,
31- } from './index.js'
32- import { installDeps , parseDeps } from './deps.js'
33- import { startRepl } from './repl.js'
34- import { randomId , bufToString } from './util.js'
35- import { createRequire , type minimist } from './vendor.js'
31+ } from './index.ts'
32+ import { installDeps , parseDeps } from './deps.ts'
33+ import { startRepl } from './repl.ts'
34+ import { randomId , bufToString } from './util.ts'
35+ import { transformMarkdown } from './md.ts'
36+ import { createRequire , type minimist } from './vendor.ts'
3637
3738const EXT = '.mjs'
3839const EXT_RE = / ^ \. [ m c ] ? [ j t ] s x ? $ /
@@ -206,86 +207,15 @@ async function readScriptFromHttp(remote: string): Promise<string> {
206207 return res . text ( )
207208}
208209
210+ export { transformMarkdown }
211+
209212export function injectGlobalRequire ( origin : string ) : void {
210213 const __filename = path . resolve ( origin )
211214 const __dirname = path . dirname ( __filename )
212215 const require = createRequire ( origin )
213216 Object . assign ( globalThis , { __filename, __dirname, require } )
214217}
215218
216- export function transformMarkdown ( buf : Buffer | string ) : string {
217- const output = [ ]
218- const tabRe = / ^ ( + | \t ) /
219- const codeBlockRe =
220- / ^ (?< 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 ) ) | .* ) $ /
221- let state = 'root'
222- let codeBlockEnd = ''
223- let prevLineIsEmpty = true
224- for ( const line of bufToString ( buf ) . split ( / \r ? \n / ) ) {
225- switch ( state ) {
226- case 'root' :
227- if ( tabRe . test ( line ) && prevLineIsEmpty ) {
228- output . push ( line )
229- state = 'tab'
230- continue
231- }
232- const { fence, js, bash } = line . match ( codeBlockRe ) ?. groups || { }
233- if ( ! fence ) {
234- prevLineIsEmpty = line === ''
235- output . push ( '// ' + line )
236- continue
237- }
238- codeBlockEnd = fence
239- if ( js ) {
240- state = 'js'
241- output . push ( '' )
242- } else if ( bash ) {
243- state = 'bash'
244- output . push ( 'await $`' )
245- } else {
246- state = 'other'
247- output . push ( '' )
248- }
249- break
250- case 'tab' :
251- if ( line === '' ) {
252- output . push ( '' )
253- } else if ( tabRe . test ( line ) ) {
254- output . push ( line )
255- } else {
256- output . push ( '// ' + line )
257- state = 'root'
258- }
259- break
260- case 'js' :
261- if ( line === codeBlockEnd ) {
262- output . push ( '' )
263- state = 'root'
264- } else {
265- output . push ( line )
266- }
267- break
268- case 'bash' :
269- if ( line === codeBlockEnd ) {
270- output . push ( '`' )
271- state = 'root'
272- } else {
273- output . push ( line )
274- }
275- break
276- case 'other' :
277- if ( line === codeBlockEnd ) {
278- output . push ( '' )
279- state = 'root'
280- } else {
281- output . push ( '// ' + line )
282- }
283- break
284- }
285- }
286- return output . join ( '\n' )
287- }
288-
289219export function isMain (
290220 metaurl : string = import . meta. url ,
291221 scriptpath : string = process . argv [ 1 ]
0 commit comments