@@ -53,22 +53,16 @@ function* lexCommentString2(
5353 end -- ;
5454 }
5555
56- let lineStart = true ;
5756 let expectingTag = false ;
5857
5958 for ( ; ; ) {
6059 if ( pos >= end ) {
6160 return ;
6261 }
6362
64- if ( lineStart ) {
65- lineStart = false ;
66- }
67-
6863 switch ( file [ pos ] ) {
6964 case "\n" :
7065 yield makeToken ( TokenSyntaxKind . NewLine , 1 ) ;
71- lineStart = true ;
7266 expectingTag = false ;
7367 break ;
7468
@@ -84,17 +78,28 @@ function* lexCommentString2(
8478
8579 case "`" : {
8680 // Markdown's code rules are a royal pain. This could be one of several things.
87- // 1. Inline code: <1-n ticks><text><same number of ticks>
88- // 2. Code block: <3 ticks><language, no ticks>\n<text>\n<3 ticks>\n
81+ // 1. Inline code: <1-n ticks><text without multiple consecutive newlines or ticks at start of line ><same number of ticks>
82+ // 2. Code block: <newline><3+ ticks><language, no ticks>\n<text>\n<3 ticks>\n
8983 // 3. Unmatched tick(s), not code, but part of some text.
9084 // We don't quite handle #2 correctly yet. PR welcome!
9185 let tickCount = 1 ;
92- let lookahead = pos ;
86+
87+ let lookahead = pos - 1 ;
88+ let atNewline = true ;
89+ while ( lookahead > 0 && file [ lookahead ] !== "\n" ) {
90+ if ( / \S / . test ( file [ lookahead ] ) ) {
91+ atNewline = false ;
92+ break ;
93+ }
94+ -- lookahead ;
95+ }
96+ lookahead = pos ;
9397
9498 while ( lookahead + 1 < end && file [ lookahead + 1 ] === "`" ) {
9599 tickCount ++ ;
96100 lookahead ++ ;
97101 }
102+ const isCodeBlock = atNewline && tickCount >= 3 ;
98103 let lookaheadStart = pos ;
99104 const codeText : string [ ] = [ ] ;
100105
@@ -105,13 +110,19 @@ function* lexCommentString2(
105110 codeText . push (
106111 file . substring ( lookaheadStart , lookahead ) ,
107112 ) ;
108- yield {
109- kind : TokenSyntaxKind . Code ,
110- text : codeText . join ( "" ) ,
111- pos,
112- } ;
113- expectingTag = false ;
114- pos = lookahead ;
113+ const codeTextStr = codeText . join ( "" ) ;
114+ if ( isCodeBlock || ! / \n \s * \n / . test ( codeTextStr ) ) {
115+ yield {
116+ kind : TokenSyntaxKind . Code ,
117+ text : codeTextStr ,
118+ pos,
119+ } ;
120+ expectingTag = false ;
121+ pos = lookahead ;
122+ } else {
123+ yield makeToken ( TokenSyntaxKind . Text , tickCount ) ;
124+ expectingTag = false ;
125+ }
115126 break ;
116127 } else if ( file [ lookahead ] === "`" ) {
117128 while ( lookahead < end && file [ lookahead ] === "`" ) {
@@ -136,7 +147,7 @@ function* lexCommentString2(
136147
137148 if ( lookahead >= end && pos !== lookahead ) {
138149 if (
139- tickCount === 3 &&
150+ isCodeBlock &&
140151 file . substring ( pos , end ) . includes ( "\n" )
141152 ) {
142153 codeText . push ( file . substring ( lookaheadStart , end ) ) ;
0 commit comments