| ... | ... | @@ -50,10 +50,8 @@ function parseTail(tail: string): TailState { |
| 50 | 50 | pendingDelim: null, |
| 51 | 51 | pendingHtml: null, |
| 52 | 52 | }; |
| 53 | | |
| 54 | 53 | for (let i = 0; i < tail.length; i++) { |
| 55 | 54 | const char = tail[i]!; |
| 56 | | |
| 57 | 55 | if (char === "\n" && tail[i - 1] === "\n") { |
| 58 | 56 | resetParagraphState(state); |
| 59 | 57 | if (!state.exclusive) state.commitIndex = i + 1; |
| ... | ... | @@ -65,31 +63,25 @@ function parseTail(tail: string): TailState { |
| 65 | 63 | i = exclusiveEnd - 1; |
| 66 | 64 | continue; |
| 67 | 65 | } |
| 68 | | |
| 69 | 66 | if (state.exclusive) continue; |
| 70 | 67 | |
| 71 | 68 | if (inLinkUrlMode(state)) { |
| 72 | 69 | updateLink(state, i, char); |
| 73 | 70 | continue; |
| 74 | 71 | } |
| 75 | | |
| 76 | 72 | const htmlEnd = consumeHtml(state, tail, i); |
| 77 | 73 | if (htmlEnd > i) { |
| 78 | 74 | i = htmlEnd - 1; |
| 79 | 75 | continue; |
| 80 | 76 | } |
| 81 | | |
| 82 | 77 | const delimEnd = consumeDelim(state, tail, i); |
| 83 | 78 | if (delimEnd > i) { |
| 84 | 79 | i = delimEnd - 1; |
| 85 | 80 | continue; |
| 86 | 81 | } |
| 87 | | |
| 88 | 82 | if (isEscapedAt(tail, i)) continue; |
| 89 | | |
| 90 | 83 | updateLink(state, i, char); |
| 91 | 84 | } |
| 92 | | |
| 93 | 85 | return state; |
| 94 | 86 | } |
| 95 | 87 | |
| ... | ... | @@ -105,7 +97,6 @@ function consumeBackticks(state: TailState, tail: string, start: number) { |
| 105 | 97 | const length = runLengthAt(tail, start); |
| 106 | 98 | const token = "`".repeat(length); |
| 107 | 99 | const exclusive = state.exclusive; |
| 108 | | |
| 109 | 100 | if (exclusive?.kind === "fence") { |
| 110 | 101 | if ( |
| 111 | 102 | exclusive.token[0] === "`" && |
| ... | ... | @@ -117,22 +108,18 @@ function consumeBackticks(state: TailState, tail: string, start: number) { |
| 117 | 108 | } |
| 118 | 109 | return start + length; |
| 119 | 110 | } |
| 120 | | |
| 121 | 111 | if (exclusive?.kind === "code") { |
| 122 | 112 | if (!isEscapedAt(tail, start) && length >= exclusive.token.length) { |
| 123 | 113 | state.exclusive = null; |
| 124 | 114 | } |
| 125 | 115 | return start + length; |
| 126 | 116 | } |
| 127 | | |
| 128 | 117 | if (exclusive) return start + length; |
| 129 | 118 | if (isEscapedAt(tail, start)) return start + length; |
| 130 | | |
| 131 | 119 | if (length >= 3 && isLineStart(tail, start)) { |
| 132 | 120 | state.exclusive = { kind: "fence", start, token }; |
| 133 | 121 | return start + length; |
| 134 | 122 | } |
| 135 | | |
| 136 | 123 | state.exclusive = { kind: "code", start, token }; |
| 137 | 124 | return start + length; |
| 138 | 125 | } |
| ... | ... | @@ -140,17 +127,14 @@ function consumeBackticks(state: TailState, tail: string, start: number) { |
| 140 | 127 | function consumeMath(state: TailState, tail: string, start: number) { |
| 141 | 128 | const length = runLengthAt(tail, start); |
| 142 | 129 | const exclusive = state.exclusive; |
| 143 | | |
| 144 | 130 | if (exclusive?.kind === "math") { |
| 145 | 131 | if (!isEscapedAt(tail, start) && length >= exclusive.token.length) { |
| 146 | 132 | state.exclusive = null; |
| 147 | 133 | } |
| 148 | 134 | return start + length; |
| 149 | 135 | } |
| 150 | | |
| 151 | 136 | if (exclusive) return start + length; |
| 152 | 137 | if (isEscapedAt(tail, start)) return start + length; |
| 153 | | |
| 154 | 138 | state.exclusive = { |
| 155 | 139 | kind: "math", |
| 156 | 140 | start, |
| ... | ... | @@ -162,27 +146,22 @@ function consumeMath(state: TailState, tail: string, start: number) { |
| 162 | 146 | function consumeTildes(state: TailState, tail: string, start: number) { |
| 163 | 147 | const length = runLengthAt(tail, start); |
| 164 | 148 | const exclusive = state.exclusive; |
| 165 | | |
| 166 | 149 | if (exclusive?.kind === "fence" && exclusive.token[0] === "~") { |
| 167 | 150 | if (isLineStart(tail, start) && !isEscapedAt(tail, start) && length >= exclusive.token.length) { |
| 168 | 151 | state.exclusive = null; |
| 169 | 152 | } |
| 170 | 153 | return start + length; |
| 171 | 154 | } |
| 172 | | |
| 173 | 155 | if (exclusive) return start; |
| 174 | 156 | if (length < 3 || !isLineStart(tail, start) || isEscapedAt(tail, start)) return start; |
| 175 | | |
| 176 | 157 | state.exclusive = { kind: "fence", start, token: "~".repeat(length) }; |
| 177 | 158 | return start + length; |
| 178 | 159 | } |
| 179 | 160 | |
| 180 | 161 | function consumeHtml(state: TailState, tail: string, start: number) { |
| 181 | 162 | if (tail[start] !== "<" || isEscapedAt(tail, start)) return start; |
| 182 | | |
| 183 | 163 | const next = tail[start + 1]; |
| 184 | 164 | if (next !== undefined && !isAsciiLetter(next)) return start; |
| 185 | | |
| 186 | 165 | state.pendingHtml = start; |
| 187 | 166 | for (let i = start + 1; i < tail.length; i++) { |
| 188 | 167 | if (tail[i] === ">" || tail[i] === "\n") { |
| ... | ... | @@ -190,47 +169,38 @@ function consumeHtml(state: TailState, tail: string, start: number) { |
| 190 | 169 | return i + 1; |
| 191 | 170 | } |
| 192 | 171 | } |
| 193 | | |
| 194 | 172 | return tail.length; |
| 195 | 173 | } |
| 196 | 174 | |
| 197 | 175 | function consumeDelim(state: TailState, tail: string, start: number) { |
| 198 | 176 | const token = matchDelim(tail, start); |
| 199 | 177 | if (!token) return start; |
| 200 | | |
| 201 | 178 | if (isEscapedAt(tail, start)) return start + token.length; |
| 202 | | |
| 203 | 179 | const existingIndex = state.delims.findLastIndex((delim) => delim.token === token); |
| 204 | 180 | if (existingIndex !== -1) { |
| 205 | 181 | state.delims.splice(existingIndex, 1); |
| 206 | 182 | return start + token.length; |
| 207 | 183 | } |
| 208 | | |
| 209 | 184 | if (start + token.length === tail.length) { |
| 210 | 185 | state.pendingDelim = { start, token }; |
| 211 | 186 | return start + token.length; |
| 212 | 187 | } |
| 213 | | |
| 214 | 188 | if (!canOpenDelim(tail, start, token)) return start + token.length; |
| 215 | | |
| 216 | 189 | state.delims.push({ start, token }); |
| 217 | 190 | return start + token.length; |
| 218 | 191 | } |
| 219 | 192 | |
| 220 | 193 | function matchDelim(tail: string, start: number): DelimToken | undefined { |
| 221 | 194 | const char = tail[start]; |
| 222 | | |
| 223 | 195 | if (char === "*") { |
| 224 | 196 | if (tail.startsWith("***", start)) return "***"; |
| 225 | 197 | if (tail.startsWith("**", start)) return "**"; |
| 226 | 198 | return "*"; |
| 227 | 199 | } |
| 228 | | |
| 229 | 200 | if (char === "_") { |
| 230 | 201 | if (tail.startsWith("__", start)) return "__"; |
| 231 | 202 | return "_"; |
| 232 | 203 | } |
| 233 | | |
| 234 | 204 | if (char === "~" && tail.startsWith("~~", start)) { |
| 235 | 205 | return "~~"; |
| 236 | 206 | } |
| ... | ... | @@ -239,19 +209,16 @@ function matchDelim(tail: string, start: number): DelimToken | undefined { |
| 239 | 209 | function canOpenDelim(tail: string, start: number, token: DelimToken) { |
| 240 | 210 | const next = tail[start + token.length]; |
| 241 | 211 | if (!next || /\s/.test(next)) return false; |
| 242 | | |
| 243 | 212 | const prev = tail[start - 1]; |
| 244 | 213 | return !isWordChar(prev) || !isWordChar(next); |
| 245 | 214 | } |
| 246 | 215 | |
| 247 | 216 | function updateLink(state: TailState, index: number, char: string) { |
| 248 | 217 | const top = state.links.at(-1); |
| 249 | | |
| 250 | 218 | if (char === "[") { |
| 251 | 219 | state.links.push({ phase: "text", start: index }); |
| 252 | 220 | return; |
| 253 | 221 | } |
| 254 | | |
| 255 | 222 | if (char === "]" && top?.phase === "text") { |
| 256 | 223 | state.links[state.links.length - 1] = { |
| 257 | 224 | phase: "url_wait", |
| ... | ... | @@ -260,7 +227,6 @@ function updateLink(state: TailState, index: number, char: string) { |
| 260 | 227 | }; |
| 261 | 228 | return; |
| 262 | 229 | } |
| 263 | | |
| 264 | 230 | if (char === "(" && top?.phase === "url_wait") { |
| 265 | 231 | state.links[state.links.length - 1] = { |
| 266 | 232 | phase: "url", |
| ... | ... | @@ -270,18 +236,15 @@ function updateLink(state: TailState, index: number, char: string) { |
| 270 | 236 | }; |
| 271 | 237 | return; |
| 272 | 238 | } |
| 273 | | |
| 274 | 239 | if (char === "(" && top?.phase === "url") { |
| 275 | 240 | top.parenDepth += 1; |
| 276 | 241 | return; |
| 277 | 242 | } |
| 278 | | |
| 279 | 243 | if (char === ")" && top?.phase === "url") { |
| 280 | 244 | if (top.parenDepth > 0) { |
| 281 | 245 | top.parenDepth -= 1; |
| 282 | 246 | return; |
| 283 | 247 | } |
| 284 | | |
| 285 | 248 | state.links.pop(); |
| 286 | 249 | } |
| 287 | 250 | } |
| ... | ... | @@ -289,10 +252,8 @@ function updateLink(state: TailState, index: number, char: string) { |
| 289 | 252 | function resetParagraphState(state: TailState) { |
| 290 | 253 | state.delims.length = 0; |
| 291 | 254 | state.links.length = 0; |
| 292 | | |
| 293 | 255 | if (state.exclusive?.kind === "fence") return; |
| 294 | 256 | if (state.exclusive?.kind === "math" && state.exclusive.token === "$$") return; |
| 295 | | |
| 296 | 257 | state.exclusive = null; |
| 297 | 258 | } |
| 298 | 259 | |
| ... | ... | @@ -302,22 +263,18 @@ function inLinkUrlMode(state: TailState) { |
| 302 | 263 | } |
| 303 | 264 | |
| 304 | 265 | function renderTail(tail: string, state: TailState) { |
| 305 | | if (state.pendingHtml !== null) tail = tail.slice(0, state.pendingHtml); |
| 306 | | |
| 266 | if (state.pendingHtml !== null) |
| 267 | tail = tail.slice(0, findPendingHtmlCutoff(tail, state.pendingHtml)); |
| 307 | 268 | const link = state.links.at(-1); |
| 308 | 269 | if (link) return renderOpenLink(tail, link); |
| 309 | | |
| 310 | 270 | const nestedClosers = renderNestedClosers(state); |
| 311 | 271 | if (nestedClosers) return appendCloser(tail, nestedClosers); |
| 312 | | |
| 313 | 272 | const open = findLastOpen(state); |
| 314 | 273 | if (!open) return state.pendingDelim ? tail.slice(0, state.pendingDelim.start) : tail; |
| 315 | | |
| 316 | 274 | if (open.kind === "delim") { |
| 317 | 275 | if (!hasContentAfter(tail, open.start, open.token.length)) return tail.slice(0, open.start); |
| 318 | 276 | return appendCloserBeforeTrailingInlineWhitespace(tail, open.token); |
| 319 | 277 | } |
| 320 | | |
| 321 | 278 | if (!hasContentAfter(tail, open.start, open.token.length)) { |
| 322 | 279 | return open.kind === "fence" ? tail : tail.slice(0, open.start); |
| 323 | 280 | } |
| ... | ... | @@ -330,12 +287,10 @@ function renderTail(tail: string, state: TailState) { |
| 330 | 287 | |
| 331 | 288 | function renderOpenLink(tail: string, state: LinkState) { |
| 332 | 289 | const before = tail.slice(0, state.start); |
| 333 | | |
| 334 | 290 | if (state.phase === "text") { |
| 335 | 291 | if (!hasContentAfter(tail, state.start, 1)) return before; |
| 336 | 292 | return before + tail.slice(state.start + 1); |
| 337 | 293 | } |
| 338 | | |
| 339 | 294 | const text = tail.slice(state.start + 1, state.textEnd); |
| 340 | 295 | if (state.phase === "url_wait") return before + text + tail.slice(state.textEnd + 1); |
| 341 | 296 | return before + text; |
| ... | ... | @@ -343,22 +298,46 @@ function renderOpenLink(tail: string, state: LinkState) { |
| 343 | 298 | |
| 344 | 299 | function renderNestedClosers(state: TailState) { |
| 345 | 300 | const closers: string[] = []; |
| 346 | | |
| 347 | 301 | if (state.exclusive) { |
| 348 | 302 | if (state.exclusive.kind !== "code") return undefined; |
| 349 | 303 | closers.push(state.exclusive.token); |
| 350 | 304 | } |
| 351 | | |
| 352 | 305 | for (let i = state.delims.length - 1; i >= 0; i--) { |
| 353 | 306 | const token = state.delims[i]!.token; |
| 354 | 307 | if (!NESTABLE_DELIMS.has(token)) return undefined; |
| 355 | 308 | closers.push(token); |
| 356 | 309 | } |
| 357 | | |
| 358 | 310 | if (closers.length < 2) return undefined; |
| 359 | 311 | return closers.join(""); |
| 360 | 312 | } |
| 361 | 313 | |
| 314 | function findPendingHtmlCutoff(tail: string, pendingStart: number) { |
| 315 | let cutoff = pendingStart; |
| 316 | let cursor = pendingStart; |
| 317 | |
| 318 | while (cursor > 0) { |
| 319 | const candidateStart = tail.lastIndexOf("<", cursor - 1); |
| 320 | if (candidateStart === -1) break; |
| 321 | if (!isCompleteHtmlTag(tail, candidateStart, cursor)) break; |
| 322 | cutoff = candidateStart; |
| 323 | cursor = candidateStart; |
| 324 | } |
| 325 | |
| 326 | return cutoff; |
| 327 | } |
| 328 | |
| 329 | function isCompleteHtmlTag(tail: string, start: number, end: number) { |
| 330 | if (tail[end - 1] !== ">") return false; |
| 331 | if (isEscapedAt(tail, start)) return false; |
| 332 | const next = tail[start + 1]; |
| 333 | if (next !== undefined && !isAsciiLetter(next)) return false; |
| 334 | for (let i = start + 1; i < end - 1; i++) { |
| 335 | const char = tail[i]; |
| 336 | if (char === ">" || char === "\n") return false; |
| 337 | } |
| 338 | return true; |
| 339 | } |
| 340 | |
| 362 | 341 | function appendCloser(tail: string, closer: string) { |
| 363 | 342 | return tail + closer.slice(overlapLength(tail, closer)); |
| 364 | 343 | } |
| ... | ... | @@ -366,7 +345,6 @@ function appendCloser(tail: string, closer: string) { |
| 366 | 345 | function appendCloserBeforeTrailingInlineWhitespace(tail: string, closer: string) { |
| 367 | 346 | const trailingWhitespace = tail.match(/[^\S\n]+$/)?.[0]; |
| 368 | 347 | if (!trailingWhitespace) return appendCloser(tail, closer); |
| 369 | | |
| 370 | 348 | const body = tail.slice(0, -trailingWhitespace.length); |
| 371 | 349 | return appendCloser(body, closer) + trailingWhitespace; |
| 372 | 350 | } |
| ... | ... | @@ -401,12 +379,10 @@ function isLineStart(text: string, index: number) { |
| 401 | 379 | |
| 402 | 380 | function isEscapedAt(text: string, index: number) { |
| 403 | 381 | let count = 0; |
| 404 | | |
| 405 | 382 | for (let i = index - 1; i >= 0; i--) { |
| 406 | 383 | if (text[i] !== "\\") break; |
| 407 | 384 | count += 1; |
| 408 | 385 | } |
| 409 | | |
| 410 | 386 | return count % 2 === 1; |
| 411 | 387 | } |
| 412 | 388 | |