@@ -39,6 +39,7 @@ const SPAN_BREAK_LINE = |
39 | 39 | /[ \t>]*(?:\r?$|#{1,6}(?:[ \t]|\r?$)|(?:[-*+]|\d{1,9}[.)])[ \t]|`{3,}[^`\r\n]*\r?$|~{3,})/my; |
40 | 40 | const LINE_PREFIX = /([ \t]*)(>|(?:[-*+]|\d{1,9}[.)])[ \t]+)?/y; |
41 | 41 | const QUOTE_CONTINUATION = /([ \t]*)>/y; |
| 42 | +const LEADING_SPACE = /^[ \t]*/; |
42 | 43 | const THEMATIC_BREAK = /^(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})\r?$/; |
43 | 44 | const SETEXT_UNDERLINE = /^(?:-+|=+)[ \t]*\r?$/; |
44 | 45 | const FENCE_OPENER = /^(`{3,}(?=[^`\r\n]*\r?$)|~{3,})/; |
@@ -84,24 +85,32 @@ function columnWidth(text: string, start = 0): number { |
84 | 85 | function blockContinuation(line: string, block: ContainerBlock) { |
85 | 86 | let offset = 0; |
86 | 87 | let column = 0; |
| 88 | + let carriedIndent = 0; |
87 | 89 | for (let d = 0; d < block.depth; d++) { |
88 | 90 | QUOTE_CONTINUATION.lastIndex = offset; |
89 | 91 | const match = QUOTE_CONTINUATION.exec(line); |
90 | 92 | if (match === null) return null; |
91 | | - const indent = columnWidth(match[1]!, column); |
| 93 | + const prefixIndent = columnWidth(match[1]!, column); |
| 94 | + const indent = carriedIndent + prefixIndent; |
92 | 95 | const required = block.indents[d] ?? 0; |
93 | 96 | if (indent < required || indent - required > 3) return null; |
94 | 97 | offset = QUOTE_CONTINUATION.lastIndex; |
95 | | - column += indent + 1; |
| 98 | + column += prefixIndent + 1; |
| 99 | + carriedIndent = 0; |
96 | 100 | if (line[offset] === " " || line[offset] === "\t") { |
97 | | - column += columnWidth(line[offset]!, column); |
| 101 | + const spaceWidth = columnWidth(line[offset]!, column); |
| 102 | + column += spaceWidth; |
| 103 | + carriedIndent = spaceWidth - 1; |
98 | 104 | offset++; |
99 | 105 | } |
100 | 106 | } |
101 | 107 | const rest = line.slice(offset); |
102 | | - const text = rest.trimStart(); |
103 | | - const indent = columnWidth(rest.slice(0, rest.length - text.length), column); |
104 | | - return text === "" || indent >= (block.indents[block.depth] ?? 0) ? { rest, indent } : null; |
| 108 | + const leading = LEADING_SPACE.exec(rest)![0]; |
| 109 | + const text = rest.slice(leading.length); |
| 110 | + const indent = carriedIndent + columnWidth(leading, column); |
| 111 | + return text === "" || text === "\r" || indent >= (block.indents[block.depth] ?? 0) |
| 112 | + ? { rest, text, indent } |
| 113 | + : null; |
105 | 114 | } |
106 | 115 |
|
107 | 116 | function codeSpanLines(markdown: string) { |
@@ -119,7 +128,7 @@ function codeSpanLines(markdown: string) { |
119 | 128 | if (block !== undefined) { |
120 | 129 | const continuation = blockContinuation(line, block); |
121 | 130 | if (continuation !== null) { |
122 | | - const { rest, indent } = continuation; |
| 131 | + const { rest, text, indent } = continuation; |
123 | 132 | lines.push({ |
124 | 133 | start, |
125 | 134 | depth: block.depth, |
@@ -129,13 +138,12 @@ function codeSpanLines(markdown: string) { |
129 | 138 | if (block.kind === "html") { |
130 | 139 | if (block.closer.test(rest)) block = undefined; |
131 | 140 | } else { |
132 | | - const restText = rest.trimStart(); |
133 | 141 | const required = block.indents[block.depth] ?? 0; |
134 | 142 | const closer = block.char === "`" ? BACKTICK_FENCE_CLOSER : TILDE_FENCE_CLOSER; |
135 | 143 | if ( |
136 | 144 | indent - required <= 3 && |
137 | | - closer.test(restText) && |
138 | | - restText.startsWith(block.char.repeat(block.length)) |
| 145 | + closer.test(text) && |
| 146 | + text.startsWith(block.char.repeat(block.length)) |
139 | 147 | ) { |
140 | 148 | block = undefined; |
141 | 149 | } |
@@ -148,6 +156,7 @@ function codeSpanLines(markdown: string) { |
148 | 156 | } |
149 | 157 | let offset = 0; |
150 | 158 | let column = 0; |
| 159 | + let carriedIndent = 0; |
151 | 160 | let depth = 0; |
152 | 161 | let hasList = false; |
153 | 162 | let listOffset = 0; |
@@ -156,12 +165,13 @@ function codeSpanLines(markdown: string) { |
156 | 165 | LINE_PREFIX.lastIndex = offset; |
157 | 166 | const prefix = LINE_PREFIX.exec(line); |
158 | 167 | if (prefix === null) break; |
159 | | - const indent = columnWidth(prefix[1]!, column); |
| 168 | + const prefixIndent = columnWidth(prefix[1]!, column); |
| 169 | + const indent = carriedIndent + prefixIndent; |
160 | 170 | if ( |
161 | 171 | listOffset === 0 && |
162 | 172 | paragraphDepth === undefined && |
163 | 173 | indent < (listIndents[depth] ?? 0) && |
164 | | - line.trim() |
| 174 | + !/^[ \t]*\r?$/.test(line) |
165 | 175 | ) { |
166 | 176 | listIndents.length = depth; |
167 | 177 | } |
@@ -175,21 +185,27 @@ function codeSpanLines(markdown: string) { |
175 | 185 | depth++; |
176 | 186 | listOffset = 0; |
177 | 187 | listTailSpace = 0; |
| 188 | + carriedIndent = 0; |
178 | 189 | if (line[offset] === " " || line[offset] === "\t") { |
179 | | - column += columnWidth(line[offset]!, column); |
| 190 | + const spaceWidth = columnWidth(line[offset]!, column); |
| 191 | + column += spaceWidth; |
| 192 | + carriedIndent = spaceWidth - 1; |
180 | 193 | offset++; |
181 | 194 | } |
182 | 195 | } else { |
183 | | - listTailSpace = width - indent - prefix[2]!.trimEnd().length; |
184 | | - listOffset += listTailSpace > 4 ? width - listTailSpace + 1 : width; |
| 196 | + const markerWidth = carriedIndent + width; |
| 197 | + listTailSpace = width - prefixIndent - prefix[2]!.trimEnd().length; |
| 198 | + listOffset += listTailSpace > 4 ? markerWidth - listTailSpace + 1 : markerWidth; |
185 | 199 | listIndents[depth] = listOffset; |
186 | 200 | hasList = true; |
| 201 | + carriedIndent = 0; |
187 | 202 | if (listTailSpace > 4) break; |
188 | 203 | } |
189 | 204 | } |
190 | 205 | const content = line.slice(offset); |
191 | | - const text = content.trimStart(); |
192 | | - const indent = columnWidth(content.slice(0, content.length - text.length), column); |
| 206 | + const leading = LEADING_SPACE.exec(content)![0]; |
| 207 | + const text = content.slice(leading.length); |
| 208 | + const indent = carriedIndent + columnWidth(leading, column); |
193 | 209 | const containerIndent = hasList ? 0 : (listIndents[depth] ?? 0); |
194 | 210 | const lazy = indent < containerIndent || depth < (paragraphDepth ?? 0); |
195 | 211 | const nonCodeIndent = |
|