@@ -53,22 +53,16 @@ function* lexCommentString2(
53
53
end -- ;
54
54
}
55
55
56
- let lineStart = true ;
57
56
let expectingTag = false ;
58
57
59
58
for ( ; ; ) {
60
59
if ( pos >= end ) {
61
60
return ;
62
61
}
63
62
64
- if ( lineStart ) {
65
- lineStart = false ;
66
- }
67
-
68
63
switch ( file [ pos ] ) {
69
64
case "\n" :
70
65
yield makeToken ( TokenSyntaxKind . NewLine , 1 ) ;
71
- lineStart = true ;
72
66
expectingTag = false ;
73
67
break ;
74
68
@@ -84,17 +78,28 @@ function* lexCommentString2(
84
78
85
79
case "`" : {
86
80
// 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
89
83
// 3. Unmatched tick(s), not code, but part of some text.
90
84
// We don't quite handle #2 correctly yet. PR welcome!
91
85
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 ;
93
97
94
98
while ( lookahead + 1 < end && file [ lookahead + 1 ] === "`" ) {
95
99
tickCount ++ ;
96
100
lookahead ++ ;
97
101
}
102
+ const isCodeBlock = atNewline && tickCount >= 3 ;
98
103
let lookaheadStart = pos ;
99
104
const codeText : string [ ] = [ ] ;
100
105
@@ -105,13 +110,19 @@ function* lexCommentString2(
105
110
codeText . push (
106
111
file . substring ( lookaheadStart , lookahead ) ,
107
112
) ;
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
+ }
115
126
break ;
116
127
} else if ( file [ lookahead ] === "`" ) {
117
128
while ( lookahead < end && file [ lookahead ] === "`" ) {
@@ -136,7 +147,7 @@ function* lexCommentString2(
136
147
137
148
if ( lookahead >= end && pos !== lookahead ) {
138
149
if (
139
- tickCount === 3 &&
150
+ isCodeBlock &&
140
151
file . substring ( pos , end ) . includes ( "\n" )
141
152
) {
142
153
codeText . push ( file . substring ( lookaheadStart , end ) ) ;
0 commit comments