GeezerVison is showing github.com — fix(shared): thread links in fence-looking indented code stay literal… · pingdotgg/t3code@609178f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/threadLinks.test.ts +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -184,6 +184,49 @@ describe("thread links", () => { | 184 | 184 | }, | 185 | 185 | ); | 186 | 186 |
| | 187 | + it.each([ | | 188 | + ["4 spaces", " ```md\n <t3-thread://v1/abc>\n ```", ""], | | 189 | + ["tab", "\t```md\n\t<t3-thread://v1/abc>\n\t```", ""], | | 190 | + ["list + 5 spaces", "- ```md\n <t3-thread://v1/abc>\n ```", ""], | | 191 | + ["nested list", "- a\n - ```md\n <t3-thread://v1/abc>\n ```", ""], | | 192 | + ["nested list with tab", "-\t- ~~~\n <t3-thread://v1/abc>", ""], | | 193 | + ["quote + 5 spaces", "> ```md\n> <t3-thread://v1/abc>\n> ```", "> "], | | 194 | + [ | | 195 | + "spaced nested quote + 5 spaces", | | 196 | + "> > ```md\n> > <t3-thread://v1/abc>", | | 197 | + "> > ", | | 198 | + ], | | 199 | + ["quote + list + 5 spaces", "> - ```md\n> <t3-thread://v1/abc>\n> ```", "> "], | | 200 | + ["quote + space + tab + list", "> \t- ```md\n> <t3-thread://v1/abc>", "> "], | | 201 | + [ | | 202 | + "quote + list after para", | | 203 | + "> para\n> - ```md\n> <t3-thread://v1/abc>\n> ```", | | 204 | + "> ", | | 205 | + ], | | 206 | + ["1. after paragraph", "Intro\n1. ```md\n <t3-thread://v1/abc>\n ```", ""], | | 207 | + ["ordered list >= 2 after blank", "Intro\n\n2. ```md\n <t3-thread://v1/abc>", ""], | | 208 | + [ | | 209 | + "quote after blank line past closer", | | 210 | + "Example\n\n> ~~~\n> ~~~\n> <t3-thread://v1/abc>", | | 211 | + "> ", | | 212 | + ], | | 213 | + ["quote after autolink", "<https://example.com>\n> ~~~\n> <t3-thread://v1/abc>", "> "], | | 214 | + ["blank line inside indented code", " ```md\n\n <t3-thread://v1/abc>", ""], | | 215 | + ["quote after indented code", " ```md\n> ```\n> <t3-thread://v1/abc>", "> "], | | 216 | + ["4 spaces after closer", " ```md\n a\n ```\n <t3-thread://v1/abc>", ""], | | 217 | + ["4 spaces with U+2028", " ```\u2028<t3-thread://v1/t1>```", ""], | | 218 | + ["hosted link", " ```\n https://app.t3.codes/threads/env/thr\n ```", ""], | | 219 | + ])( | | 220 | + "keeps links inside fence-looking indented code blocks literal (%s)", | | 221 | + (_name, block, prose) => { | | 222 | + expect(replaceThreadLinks(block, () => "linked")).toBe(block); | | 223 | + const after = `${block}\n${prose.trimEnd()}\n${prose}See `; | | 224 | + expect(replaceThreadLinks(`${after}<t3-thread://v2/local/t>`, () => "linked")).toBe( | | 225 | + `${after}linked`, | | 226 | + ); | | 227 | + }, | | 228 | + ); | | 229 | + | 187 | 230 | it("leaves links inside code spans and fences as written", () => { | 188 | 231 | const markdown = [ | 189 | 232 | "Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.", | @@ -210,6 +253,8 @@ describe("thread links", () => { | 210 | 253 | expect(relabelThreadLinks(nested.join("\n"), local, () => "New")).toBe( | 211 | 254 | [...nested.slice(0, 3), "> > [New](t3-thread://v2/local/t1)"].join("\n"), | 212 | 255 | ); | | 256 | + const deepNested = "> > > > > ```\n> > > > > <t3-thread://v2/local/t1> ```"; | | 257 | + expect(relabelThreadLinks(deepNested, local, () => "New")).toBe(deepNested); | 213 | 258 | const labels: Array<string | undefined> = []; | 214 | 259 | const separated = "[Label\u2028> > example](t3-thread://v1/t1)"; | 215 | 260 | expect(replaceThreadLinks(separated, (_link, label) => void labels.push(label))).toBe( | @@ -235,6 +280,30 @@ describe("thread links", () => { | 235 | 280 | ["1. Run:", "", " ```bash", ' echo "```"', ` ${hidden}`, " ```", "", after], | 236 | 281 | ["````md", "```", hidden, "````", after], | 237 | 282 | ["Use ~~~ to mark it.", after], | | 283 | + ["> para", "> ~~~", `> ${after}`], | | 284 | + ["> para", `> ~~~ ${after}`], | | 285 | + ["- > para", " > ```md", ` > ${after}`], | | 286 | + [`Intro\u2028- ~~~ ${after}`], | | 287 | + ["Intro", `2. ~~~ ${after}`], | | 288 | + ["Intro", `2. - ~~~ ${after}`], | | 289 | + [">\t- ```md", `> ${after}`], | | 290 | + ["> para", "lazy", "> ~~~", `> ${after}`], | | 291 | + ["> para", "> 2. >", `> 2. ~~~ ${after}`], | | 292 | + ["> para\r", "> ~~~\r", `> ${after}`], | | 293 | + [`> para\r\r> ~~~\r> ${hidden}\r> ${after}`], | | 294 | + ["para", " - ```", ` ${after}`], | | 295 | + ["para", " - ```", ` ${after}`], | | 296 | + [" ~~~", `- ${after}`], | | 297 | + [" ~~~md", " ~~~", " ~~~", after], | | 298 | + ["- item", " > ~~~", ` > ${hidden}`, ` ${after}`], | | 299 | + ["10. > ```md", ` > ${hidden} \`\`\``, "", after], | | 300 | + [" - > ```md", ` > ${hidden} \`\`\``, "", after], | | 301 | + ["> - Run this:", ">", "> ```bash", "> echo hi", ">", "> ```", `> ${after}`], | | 302 | + ["Example", "", "> ~~~", ">\t\t~~~", `> ${hidden}`, ">\t\t~~~", `> ${after}`], | | 303 | + [after, "> ~~~", `> ${hidden}`], | | 304 | + [`1. \`\`\`md ${hidden}`, ` \t${after}`], | | 305 | + ["> ```md", `> \t${after}`], | | 306 | + ["> ```md", `>\t ${after}`], | 238 | 307 | ["```" + hidden + "``` then", after], | 239 | 308 | ["Use ```example", hidden, "end``` then", after], | 240 | 309 | ["Stray ` tick", "", after, "and `code`"], | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/threadLinks.ts +77Lines changed: 77 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,8 @@ const AFTER_QUOTE = String.raw`(?:(?<=>)[ \t]*)?`; | 31 | 31 | // start of the text, after a list marker, or after a quote's optional space. | 32 | 32 | const NOT_INDENTED_CODE = String.raw`(?!(?<![\s\S])(?: {0,3}\t| {4}))`; | 33 | 33 | const OPENER_LIST_MARKERS = String.raw`(?:(?:[-*+]|\d{1,9}[.)])[ \t]{1,4}(?![ \t]))*`; | | 34 | +// Fence-looking lines declined by `fencedCode` may still open an indented code block. | | 35 | +const INDENTED_FENCE = String.raw`(?<![^\r\n])(?<indentedFence>[ \t>*+\-\d.)]*?(?:\`{3,}[^\`\r\n]*|~{3,}[^\r\n]*))(?=[\r\n]|(?![\s\S]))`; | 34 | 36 | const fencedCode = (char: "`" | "~", name: string) => { | 35 | 37 | const bq = `${name}Bq`; | 36 | 38 | const info = char === "`" ? "[^`\\r\\n]*" : "[^\\r\\n]*"; | @@ -51,6 +53,7 @@ const THREAD_LINK_OUTSIDE_CODE = new RegExp( | 51 | 53 | String.raw`\\[!-/:-@[-\`{-~]`, | 52 | 54 | fencedCode("`", "bt"), | 53 | 55 | fencedCode("~", "tl"), | | 56 | + INDENTED_FENCE, | 54 | 57 | String.raw`(?<spanTicks>\`+)`, | 55 | 58 | // A labeled link's destination may be angle-bracketed, as CommonMark allows: [label](<href>). | 56 | 59 | String.raw`\[(?<label>${LABEL})\]\((?:<(?<angleHref>${THREAD_HREF}[^\s<>]+|${HOSTED_LINK})>|(?<href>${THREAD_HREF}[^\s>)]+|${HOSTED_LINK}))\)`, | @@ -290,6 +293,74 @@ export function threadLinkRefs( | 290 | 293 | return [...refs.values()]; | 291 | 294 | } | 292 | 295 |
| | 296 | +const NEXT_LINE = /(?:\r\n?|\n)([^\r\n]*)/y; | | 297 | +const LIST_MARKER_AT = /([-*+]|\d{1,9}[.)])(?=[ \t])/y; | | 298 | + | | 299 | +function walkContainer(line: string, allowList: boolean) { | | 300 | + let i = 0; | | 301 | + let col = 0; | | 302 | + let contentCol = 0; | | 303 | + let quoteCol = 0; | | 304 | + let quotes = 0; | | 305 | + let list = 0; | | 306 | + let quoteAfterList = false; | | 307 | + while (i < line.length) { | | 308 | + while (line[i] === " " || line[i] === "\t") col += line[i++] === "\t" ? 4 - (col % 4) : 1; | | 309 | + if (i >= line.length || col - contentCol > 3) break; | | 310 | + if (line[i] === ">") { | | 311 | + quoteAfterList ||= list !== 0; | | 312 | + quotes++; | | 313 | + quoteCol = contentCol = ++col + (line[++i] === " " || line[i] === "\t" ? 1 : 0); | | 314 | + if (contentCol > col) col += line[i++] === "\t" ? 4 - (col % 4) : 1; | | 315 | + continue; | | 316 | + } | | 317 | + if (!allowList) break; | | 318 | + LIST_MARKER_AT.lastIndex = i; | | 319 | + const marker = LIST_MARKER_AT.exec(line)?.[1]; | | 320 | + if (marker === undefined) break; | | 321 | + i += marker.length; | | 322 | + const afterMarker = (col += marker.length); | | 323 | + while (line[i] === " " || line[i] === "\t") col += line[i++] === "\t" ? 4 - (col % 4) : 1; | | 324 | + list = list < 0 || !/^(?:[-*+]|1[.)])$/.test(marker) ? -1 : 1; | | 325 | + contentCol = col - afterMarker >= 5 ? afterMarker + 1 : col; | | 326 | + } | | 327 | + return { | | 328 | + rest: line.slice(i), | | 329 | + quotes, | | 330 | + quoteIndent: col - quoteCol, | | 331 | + base: contentCol - quoteCol + 4, | | 332 | + list, | | 333 | + quoteAfterList, | | 334 | + blank: !list && i === line.length, | | 335 | + }; | | 336 | +} | | 337 | + | | 338 | +function findIndentedCodeEnd(markdown: string, openIndex: number, openerLength: number): number { | | 339 | + const opener = walkContainer(markdown.slice(openIndex, openIndex + openerLength), true); | | 340 | + if ( | | 341 | + opener.quoteAfterList || | | 342 | + opener.quoteIndent < opener.base || | | 343 | + !/^(?:`{3,}|~{3,})/.test(opener.rest) | | 344 | + ) | | 345 | + return -1; | | 346 | + if (openIndex > 0 && opener.list <= 0) { | | 347 | + const prevEnd = | | 348 | + openIndex - (markdown[openIndex - 1] === "\n" && markdown[openIndex - 2] === "\r" ? 2 : 1); | | 349 | + let prevStart = prevEnd; | | 350 | + while (prevStart > 0 && !/[\r\n]/.test(markdown[prevStart - 1]!)) prevStart--; | | 351 | + const prev = walkContainer(markdown.slice(prevStart, prevEnd), true); | | 352 | + if (opener.list ? !prev.blank : prev.quotes > 0 || (!prev.blank && prevStart > 0)) return -1; | | 353 | + } | | 354 | + let blockEnd = openIndex + openerLength; | | 355 | + NEXT_LINE.lastIndex = blockEnd; | | 356 | + for (let next = NEXT_LINE.exec(markdown); next !== null; next = NEXT_LINE.exec(markdown)) { | | 357 | + const cont = walkContainer(next[1]!, false); | | 358 | + if (cont.quotes !== opener.quotes || (cont.rest && cont.quoteIndent < opener.base)) break; | | 359 | + blockEnd = NEXT_LINE.lastIndex; | | 360 | + } | | 361 | + return blockEnd; | | 362 | +} | | 363 | + | 293 | 364 | /** Rewrites each link outside code `replace` answers for. `label` is a Markdown link's own text. */ | 294 | 365 | export function replaceThreadLinks( | 295 | 366 | markdown: string, | @@ -307,6 +378,12 @@ export function replaceThreadLinks( | 307 | 378 | let lines: ReturnType<typeof codeSpanLines> | undefined; | 308 | 379 | let lineIndex = 0; | 309 | 380 | for (let match = pattern.exec(source); match !== null; match = pattern.exec(source)) { | | 381 | + const indentedFence = match.groups?.indentedFence; | | 382 | + if (indentedFence !== undefined) { | | 383 | + const codeEnd = findIndentedCodeEnd(markdown, match.index, indentedFence.length); | | 384 | + pattern.lastIndex = codeEnd === -1 ? match.index + 1 : codeEnd; | | 385 | + continue; | | 386 | + } | 310 | 387 | const spanTicks = match.groups?.spanTicks; | 311 | 388 | if (spanTicks !== undefined) { | 312 | 389 | lines ??= codeSpanLines(markdown); | |
|