@@ -62,75 +62,75 @@ var import_util2 = require("./util.cjs");
6262var import_util = require ( "./util.cjs" ) ;
6363function transformMarkdown ( buf ) {
6464 var _a2 ;
65- const output = [ ] ;
65+ const out = [ ] ;
6666 const tabRe = / ^ ( + | \t ) / ;
67- const codeBlockRe = new RegExp ( "^(?<fence>(`{3,20}|~{3,20}))(?:(?<js>( js|javascript|ts|typescript)) |(?<bash>( sh|shell|bash) )|.*)$" ) ;
67+ const fenceRe = new RegExp ( "^(?<indent> {0,3})(?< fence>(`{3,20}|~{3,20}))(?:(?<js>js|javascript|ts|typescript)|(?<bash>sh|shell|bash)|.*)$" ) ;
6868 let state = "root" ;
69- let codeBlockEnd = "" ;
70- let prevLineIsEmpty = true ;
69+ let prevEmpty = true ;
70+ let fenceChar = "" ;
71+ let stripRe = null ;
72+ let endRe = / ^ $ / ;
73+ let linePrefix = "" ;
74+ let closeOut = "" ;
75+ const isEnd = ( s ) => fenceChar !== "" && endRe . test ( s ) ;
7176 for ( const line of ( 0 , import_util . bufToString ) ( buf ) . split ( / \r ? \n / ) ) {
7277 switch ( state ) {
73- case "root" :
74- if ( tabRe . test ( line ) && prevLineIsEmpty ) {
75- output . push ( line ) ;
76- state = "tab" ;
77- continue ;
78+ case "root" : {
79+ const g = ( _a2 = line . match ( fenceRe ) ) == null ? void 0 : _a2 . groups ;
80+ if ( g == null ? void 0 : g . fence ) {
81+ fenceChar = g . fence [ 0 ] ;
82+ stripRe = g . indent ? new RegExp ( `^ {0,${ g . indent . length } }` ) : null ;
83+ endRe = new RegExp ( `^ {0,3}${ fenceChar } {${ g . fence . length } ,}[ \\t]*$` ) ;
84+ if ( g . js ) {
85+ out . push ( "" ) ;
86+ linePrefix = "" ;
87+ closeOut = "" ;
88+ } else if ( g . bash ) {
89+ out . push ( "await $`" ) ;
90+ linePrefix = "" ;
91+ closeOut = "`" ;
92+ } else {
93+ out . push ( "" ) ;
94+ linePrefix = "// " ;
95+ closeOut = "" ;
96+ }
97+ state = "fence" ;
98+ prevEmpty = false ;
99+ break ;
78100 }
79- const { fence, js, bash } = ( ( _a2 = line . match ( codeBlockRe ) ) == null ? void 0 : _a2 . groups ) || { } ;
80- if ( ! fence ) {
81- prevLineIsEmpty = line === "" ;
82- output . push ( "// " + line ) ;
101+ if ( prevEmpty && tabRe . test ( line ) ) {
102+ out . push ( line ) ;
103+ state = "tab" ;
83104 continue ;
84105 }
85- codeBlockEnd = fence ;
86- if ( js ) {
87- state = "js" ;
88- output . push ( "" ) ;
89- } else if ( bash ) {
90- state = "bash" ;
91- output . push ( "await $`" ) ;
92- } else {
93- state = "other" ;
94- output . push ( "" ) ;
95- }
96- break ;
106+ prevEmpty = line === "" ;
107+ out . push ( "// " + line ) ;
108+ continue ;
109+ }
97110 case "tab" :
98- if ( line === "" ) {
99- output . push ( "" ) ;
100- } else if ( tabRe . test ( line ) ) {
101- output . push ( line ) ;
102- } else {
103- output . push ( "// " + line ) ;
111+ if ( line === "" ) out . push ( "" ) ;
112+ else if ( tabRe . test ( line ) ) out . push ( line ) ;
113+ else {
114+ out . push ( "// " + line ) ;
104115 state = "root" ;
105116 }
117+ prevEmpty = line === "" ;
106118 break ;
107- case "js" :
108- if ( line === codeBlockEnd ) {
109- output . push ( "" ) ;
110- state = "root" ;
111- } else {
112- output . push ( line ) ;
113- }
114- break ;
115- case "bash" :
116- if ( line === codeBlockEnd ) {
117- output . push ( "`" ) ;
118- state = "root" ;
119- } else {
120- output . push ( line ) ;
121- }
122- break ;
123- case "other" :
124- if ( line === codeBlockEnd ) {
125- output . push ( "" ) ;
119+ case "fence" :
120+ if ( isEnd ( line ) ) {
121+ out . push ( closeOut ) ;
126122 state = "root" ;
123+ prevEmpty = true ;
124+ fenceChar = "" ;
127125 } else {
128- output . push ( "// " + line ) ;
126+ const s = stripRe ? line . replace ( stripRe , "" ) : line ;
127+ out . push ( linePrefix + s ) ;
128+ prevEmpty = false ;
129129 }
130130 break ;
131131 }
132132 }
133- return output . join ( "\n" ) ;
133+ return out . join ( "\n" ) ;
134134}
135135
136136// src/cli.ts
0 commit comments