GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(shared): thread links in fence-looking indented code stay literal… · pingdotgg/t3code@609178f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/shared/src
    • threadLinks.test.ts
    • threadLinks.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/shared/src/threadLinks.test.ts‎

Copy file name to clipboardExpand all lines: packages/shared/src/threadLinks.test.ts
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,49 @@ describe("thread links", () => {
184184
},
185185
);
186186

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+
187230
it("leaves links inside code spans and fences as written", () => {
188231
const markdown = [
189232
"Live [old](t3-thread://v1/t1), literal `[old](t3-thread://v1/t1)`.",
@@ -210,6 +253,8 @@ describe("thread links", () => {
210253
expect(relabelThreadLinks(nested.join("\n"), local, () => "New")).toBe(
211254
[...nested.slice(0, 3), "> > [New](t3-thread://v2/local/t1)"].join("\n"),
212255
);
256+
const deepNested = "> > > > > ```\n> > > > > <t3-thread://v2/local/t1> ```";
257+
expect(relabelThreadLinks(deepNested, local, () => "New")).toBe(deepNested);
213258
const labels: Array<string | undefined> = [];
214259
const separated = "[Label\u2028> > example](t3-thread://v1/t1)";
215260
expect(replaceThreadLinks(separated, (_link, label) => void labels.push(label))).toBe(
@@ -235,6 +280,30 @@ describe("thread links", () => {
235280
["1. Run:", "", " ```bash", ' echo "```"', ` ${hidden}`, " ```", "", after],
236281
["````md", "```", hidden, "````", after],
237282
["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}`],
238307
["```" + hidden + "``` then", after],
239308
["Use ```example", hidden, "end``` then", after],
240309
["Stray ` tick", "", after, "and `code`"],
Collapse file

‎packages/shared/src/threadLinks.ts‎

Copy file name to clipboardExpand all lines: packages/shared/src/threadLinks.ts
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const AFTER_QUOTE = String.raw`(?:(?<=>)[ \t]*)?`;
3131
// start of the text, after a list marker, or after a quote's optional space.
3232
const NOT_INDENTED_CODE = String.raw`(?!(?<![\s\S])(?: {0,3}\t| {4}))`;
3333
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]))`;
3436
const fencedCode = (char: "`" | "~", name: string) => {
3537
const bq = `${name}Bq`;
3638
const info = char === "`" ? "[^`\\r\\n]*" : "[^\\r\\n]*";
@@ -51,6 +53,7 @@ const THREAD_LINK_OUTSIDE_CODE = new RegExp(
5153
String.raw`\\[!-/:-@[-\`{-~]`,
5254
fencedCode("`", "bt"),
5355
fencedCode("~", "tl"),
56+
INDENTED_FENCE,
5457
String.raw`(?<spanTicks>\`+)`,
5558
// A labeled link's destination may be angle-bracketed, as CommonMark allows: [label](<href>).
5659
String.raw`\[(?<label>${LABEL})\]\((?:<(?<angleHref>${THREAD_HREF}[^\s<>]+|${HOSTED_LINK})>|(?<href>${THREAD_HREF}[^\s>)]+|${HOSTED_LINK}))\)`,
@@ -290,6 +293,74 @@ export function threadLinkRefs(
290293
return [...refs.values()];
291294
}
292295

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+
293364
/** Rewrites each link outside code `replace` answers for. `label` is a Markdown link's own text. */
294365
export function replaceThreadLinks(
295366
markdown: string,
@@ -307,6 +378,12 @@ export function replaceThreadLinks(
307378
let lines: ReturnType<typeof codeSpanLines> | undefined;
308379
let lineIndex = 0;
309380
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+
}
310387
const spanTicks = match.groups?.spanTicks;
311388
if (spanTicks !== undefined) {
312389
lines ??= codeSpanLines(markdown);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.