The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | serializeCodeBlockToMarkdown, |
| 5 | serializeRenderedMarkdownFragment, |
| 6 | serializeTableElementToMarkdown, |
| 7 | } from "./markdown-clipboard"; |
| 8 | import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts"; |
| 9 | import { |
| 10 | collectAssistantCitations, |
| 11 | serializeAssistantCitation, |
| 12 | } from "@t3tools/shared/assistantCitations"; |
| 13 | import { markdownMathRanges, parseMarkdownWithMath } from "@t3tools/shared/markdownMath"; |
| 14 | |
| 15 | import { unified } from "unified"; |
| 16 | import remarkParse from "remark-parse"; |
| 17 | import { |
| 18 | CHAT_MARKDOWN_REMARK_PLUGINS, |
| 19 | CHAT_USER_MARKDOWN_REMARK_PLUGINS, |
| 20 | } from "@t3tools/shared/markdownPipeline"; |
| 21 | |
| 22 | const userMarkdown = unified() |
| 23 | .use(remarkParse) |
| 24 | .use(CHAT_MARKDOWN_REMARK_PLUGINS) |
| 25 | .use(CHAT_USER_MARKDOWN_REMARK_PLUGINS); |
| 26 | |
| 27 | const TEXT_NODE = 3; |
| 28 | const ELEMENT_NODE = 1; |
| 29 | |
| 30 | class FakeText { |
| 31 | readonly nodeType = TEXT_NODE; |
| 32 | readonly childNodes: ReadonlyArray<never> = []; |
| 33 | |
| 34 | constructor(readonly textContent: string) {} |
| 35 | } |
| 36 | |
| 37 | class FakeElement { |
| 38 | readonly nodeType = ELEMENT_NODE; |
| 39 | checked = false; |
| 40 | readonly childNodes: Array<FakeElement | FakeText> = []; |
| 41 | readonly classList = { |
| 42 | contains: (name: string) => this.classNames.includes(name), |
| 43 | }; |
| 44 | |
| 45 | constructor( |
| 46 | readonly tagName: string, |
| 47 | private readonly classNames: ReadonlyArray<string> = [], |
| 48 | private readonly attributes: Readonly<Record<string, string>> = {}, |
| 49 | ) {} |
| 50 | |
| 51 | get localName(): string { |
| 52 | return this.tagName.toLowerCase(); |
| 53 | } |
| 54 | |
| 55 | get textContent(): string { |
| 56 | return this.childNodes.map((child) => child.textContent).join(""); |
| 57 | } |
| 58 | |
| 59 | get children(): ReadonlyArray<FakeElement> { |
| 60 | return this.childNodes.filter((child): child is FakeElement => child instanceof FakeElement); |
| 61 | } |
| 62 | |
| 63 | append(...children: Array<FakeElement | FakeText>): this { |
| 64 | this.childNodes.push(...children); |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | getAttribute(name: string): string | null { |
| 69 | return this.attributes[name] ?? null; |
| 70 | } |
| 71 | |
| 72 | hasAttribute(name: string): boolean { |
| 73 | return Object.hasOwn(this.attributes, name); |
| 74 | } |
| 75 | |
| 76 | closest(): FakeElement | null { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | querySelector(selector: string): FakeElement | null { |
| 81 | if (selector.includes(", ")) { |
| 82 | for (const part of selector.split(", ")) { |
| 83 | const match = this.querySelector(part); |
| 84 | if (match) return match; |
| 85 | } |
| 86 | return null; |
| 87 | } |
| 88 | const childOnly = selector.startsWith(":scope > "); |
| 89 | const [target, ...rest] = (childOnly ? selector.slice(":scope > ".length) : selector).split( |
| 90 | " > ", |
| 91 | ); |
| 92 | const matches = (element: FakeElement): boolean => { |
| 93 | if (target === 'input[type="checkbox"]') { |
| 94 | return element.tagName === "INPUT" && element.getAttribute("type") === "checkbox"; |
| 95 | } |
| 96 | return element.tagName === target?.toUpperCase(); |
| 97 | }; |
| 98 | const search = (parent: FakeElement): FakeElement | null => { |
| 99 | for (const child of parent.childNodes) { |
| 100 | if (!(child instanceof FakeElement)) continue; |
| 101 | if (matches(child)) { |
| 102 | if (rest.length === 0) return child; |
| 103 | const nested = child.querySelector(`:scope > ${rest.join(" > ")}`); |
| 104 | if (nested) return nested; |
| 105 | } |
| 106 | if (!childOnly) { |
| 107 | const nested = search(child); |
| 108 | if (nested) return nested; |
| 109 | } |
| 110 | } |
| 111 | return null; |
| 112 | }; |
| 113 | return search(this); |
| 114 | } |
| 115 | |
| 116 | /** Supports the `:scope > a > b` child paths the table serializer queries. */ |
| 117 | querySelectorAll(selector: string): FakeElement[] { |
| 118 | return selector.split(", ").flatMap((part) => |
| 119 | part |
| 120 | .replace(/^:scope > /, "") |
| 121 | .split(" > ") |
| 122 | .reduce<FakeElement[]>( |
| 123 | (level, tag) => |
| 124 | level.flatMap((element) => |
| 125 | element.children.filter((child) => child.tagName === tag.toUpperCase()), |
| 126 | ), |
| 127 | [this], |
| 128 | ), |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function asNode(element: FakeElement): Node { |
| 134 | return element as unknown as Node; |
| 135 | } |
| 136 | |
| 137 | function shikiCodeLine(text: string): FakeElement { |
| 138 | const token = new FakeElement("SPAN").append(new FakeText(text)); |
| 139 | return new FakeElement("SPAN", ["line"]).append(token); |
| 140 | } |
| 141 | |
| 142 | function renderedCodeBlock(lines: ReadonlyArray<string>): FakeElement { |
| 143 | const code = new FakeElement("CODE"); |
| 144 | lines.forEach((line, index) => { |
| 145 | if (index > 0) code.append(new FakeText("\n")); |
| 146 | code.append(shikiCodeLine(line)); |
| 147 | }); |
| 148 | return new FakeElement("DIV", ["chat-markdown-codeblock"]).append( |
| 149 | new FakeElement("DIV", ["chat-markdown-codeblock-header", "select-none"]).append( |
| 150 | new FakeText("sh"), |
| 151 | ), |
| 152 | new FakeElement("DIV", ["chat-markdown-shiki"]).append(new FakeElement("PRE").append(code)), |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | describe("serializeRenderedMarkdownFragment", () => { |
| 157 | it("copies a popover context reference once, without its details or nested label", () => { |
| 158 | const reference = "[Review comment](t3-context://v1/review-comment/review-1)"; |
| 159 | const container = new FakeElement("DIV").append( |
| 160 | new FakeText("Fix "), |
| 161 | new FakeElement("BUTTON", [], { "data-markdown-copy": reference }).append( |
| 162 | new FakeElement("SPAN", [], { "data-markdown-copy": reference }).append( |
| 163 | new FakeText("Review comment"), |
| 164 | ), |
| 165 | ), |
| 166 | new FakeText(" please"), |
| 167 | ); |
| 168 | |
| 169 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(`Fix ${reference} please`); |
| 170 | }); |
| 171 | |
| 172 | beforeEach(() => { |
| 173 | vi.stubGlobal("Node", { TEXT_NODE, ELEMENT_NODE }); |
| 174 | }); |
| 175 | |
| 176 | afterEach(() => { |
| 177 | vi.unstubAllGlobals(); |
| 178 | }); |
| 179 | |
| 180 | it("copies the complete quote, source, and comment instead of the comment-only chip label", () => { |
| 181 | const citation = { |
| 182 | version: 1 as const, |
| 183 | environmentId: EnvironmentId.make("environment-one"), |
| 184 | threadId: ThreadId.make("thread-one"), |
| 185 | messageId: MessageId.make("assistant-one"), |
| 186 | text: "A complete quote, including the part hidden by the chip preview.", |
| 187 | comment: "What does this mean?\nPlease expand on the hidden part.", |
| 188 | start: 0, |
| 189 | end: 66, |
| 190 | prefix: "", |
| 191 | suffix: "", |
| 192 | }; |
| 193 | const anchor = new FakeElement("A", [], { |
| 194 | "data-markdown-copy": serializeAssistantCitation(citation), |
| 195 | href: "/environment-one/thread-one#citation", |
| 196 | }).append(new FakeText("What does this mean?…")); |
| 197 | const chip = new FakeElement("SPAN", [], { |
| 198 | "data-markdown-copy": serializeAssistantCitation(citation), |
| 199 | }).append(anchor, new FakeElement("BUTTON").append(new FakeText("Edit comment"))); |
| 200 | const container = new FakeElement("DIV").append(new FakeText("Explain "), chip); |
| 201 | const copied = serializeRenderedMarkdownFragment(asNode(container)); |
| 202 | expect(copied).toBe(`Explain ${serializeAssistantCitation(citation)}`); |
| 203 | expect(collectAssistantCitations(copied).map((entry) => entry.citation)).toEqual([citation]); |
| 204 | }); |
| 205 | |
| 206 | it("keeps a multi-line code selection plain instead of inline-wrapping it", () => { |
| 207 | const code = new FakeElement("CODE").append(new FakeText("first line\nsecond line")); |
| 208 | const container = new FakeElement("DIV").append(code); |
| 209 | |
| 210 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("first line\nsecond line"); |
| 211 | }); |
| 212 | |
| 213 | it.each([ |
| 214 | ["backtick edges", "`value`"], |
| 215 | ["space edges", " value "], |
| 216 | ["only spaces", " "], |
| 217 | ])("copies inline code that parses back unchanged: %s", (_, value) => { |
| 218 | const container = new FakeElement("DIV").append( |
| 219 | new FakeElement("P").append( |
| 220 | new FakeText("Use "), |
| 221 | new FakeElement("CODE").append(new FakeText(value)), |
| 222 | new FakeText(" here."), |
| 223 | ), |
| 224 | ); |
| 225 | const paragraph = parseMarkdownWithMath(serializeRenderedMarkdownFragment(asNode(container))) |
| 226 | .children[0]; |
| 227 | if (paragraph?.type !== "paragraph") throw new Error("Expected a paragraph"); |
| 228 | expect(paragraph.children.find((child) => child.type === "inlineCode")?.value).toBe(value); |
| 229 | }); |
| 230 | |
| 231 | it.each([ |
| 232 | String.raw`see C:\me\.t3\shot.png`, |
| 233 | String.raw`C:\work\_build`, |
| 234 | "C:/work/_build/out.txt", |
| 235 | String.raw`C:\work\a-_b.txt`, |
| 236 | "a*word*b and a_word_b; ~~literal~~ and ~single~", |
| 237 | String.raw`before\ after\ end`, |
| 238 | ])("copies prose that parses back unchanged: %s", (value) => { |
| 239 | const container = new FakeElement("DIV").append(new FakeText(value)); |
| 240 | const paragraph = userMarkdown.parse(serializeRenderedMarkdownFragment(asNode(container))) |
| 241 | .children[0]; |
| 242 | expect(paragraph).toMatchObject({ type: "paragraph", children: [{ type: "text", value }] }); |
| 243 | }); |
| 244 | |
| 245 | it.each([ |
| 246 | ["*", "EM", "word", "emphasis"], |
| 247 | ["\\", "CODE", "value", "inlineCode"], |
| 248 | ])("keeps a text edge %j beside %s", (edge, tag, value, type) => { |
| 249 | const container = new FakeElement("DIV").append( |
| 250 | new FakeText(edge), |
| 251 | new FakeElement(tag).append(new FakeText(value)), |
| 252 | ); |
| 253 | const paragraph = userMarkdown.parse(serializeRenderedMarkdownFragment(asNode(container))) |
| 254 | .children[0]; |
| 255 | expect(paragraph).toMatchObject({ |
| 256 | type: "paragraph", |
| 257 | children: [ |
| 258 | { type: "text", value: edge }, |
| 259 | type === "inlineCode" ? { type, value } : { type, children: [{ type: "text", value }] }, |
| 260 | ], |
| 261 | }); |
| 262 | }); |
| 263 | |
| 264 | it("copies email links with their address and drops in-app destinations", () => { |
| 265 | const container = new FakeElement("DIV").append( |
| 266 | new FakeElement("P").append( |
| 267 | new FakeText("Write to "), |
| 268 | new FakeElement("A", [], { href: "mailto:support@example.com" }).append( |
| 269 | new FakeText("support@example.com"), |
| 270 | ), |
| 271 | new FakeText(", "), |
| 272 | new FakeElement("A", [], { href: "mailto:support@example.com?subject=Help" }).append( |
| 273 | new FakeText("Support"), |
| 274 | ), |
| 275 | new FakeText(", "), |
| 276 | new FakeElement("A", [], { href: "mailto:support@example.com?subject=Help)" }).append( |
| 277 | new FakeText("Support"), |
| 278 | ), |
| 279 | new FakeText(" or "), |
| 280 | new FakeElement("A", [], { href: "/environment-one/thread-one" }).append( |
| 281 | new FakeText("the thread"), |
| 282 | ), |
| 283 | new FakeText("."), |
| 284 | ), |
| 285 | ); |
| 286 | const copied = serializeRenderedMarkdownFragment(asNode(container)); |
| 287 | expect(copied).toBe( |
| 288 | "Write to support@example.com, [Support](mailto:support@example.com?subject=Help), [Support](mailto:support@example.com?subject=Help\\)) or the thread.", |
| 289 | ); |
| 290 | const paragraph = parseMarkdownWithMath(copied).children[0]; |
| 291 | if (paragraph?.type !== "paragraph") throw new Error("Expected a paragraph"); |
| 292 | expect( |
| 293 | paragraph.children.filter((child) => child.type === "link").map((link) => link.url), |
| 294 | ).toEqual([ |
| 295 | "mailto:support@example.com", |
| 296 | "mailto:support@example.com?subject=Help", |
| 297 | "mailto:support@example.com?subject=Help)", |
| 298 | ]); |
| 299 | }); |
| 300 | |
| 301 | it.each([ |
| 302 | ["array[index]", "https://example.com"] as const, |
| 303 | ...[ |
| 304 | "support@example.com?subject=Help", |
| 305 | "foo", |
| 306 | "support@example.internal1", |
| 307 | "support@example.com_", |
| 308 | "support@example.com-", |
| 309 | "first_last@example.com", |
| 310 | ].map((label) => [label, `mailto:${label}`] as const), |
| 311 | ])("keeps the link label and destination: %s", (label, href) => { |
| 312 | const anchor = new FakeElement("A", [], { href }).append(new FakeText(label)); |
| 313 | const container = new FakeElement("DIV").append(anchor); |
| 314 | const paragraph = userMarkdown.parse(serializeRenderedMarkdownFragment(asNode(container))) |
| 315 | .children[0]; |
| 316 | if (paragraph?.type !== "paragraph") throw new Error("Expected a paragraph"); |
| 317 | expect(paragraph.children).toMatchObject([ |
| 318 | { type: "link", url: href, children: [{ type: "text", value: label }] }, |
| 319 | ]); |
| 320 | }); |
| 321 | |
| 322 | it("copies alert and insight callouts as their Markdown instead of the rendered label", () => { |
| 323 | // The shape ChatMarkdown renders for `> [!WARNING]\n> Back up first.`. |
| 324 | const callout = new FakeElement("DIV", [], { |
| 325 | role: "note", |
| 326 | "data-markdown-alert": "warning", |
| 327 | }).append( |
| 328 | new FakeElement("P", [], { |
| 329 | "data-thread-find-ignore": "true", |
| 330 | "data-markdown-alert-label": "", |
| 331 | }).append(new FakeElement("SVG", [], { "aria-hidden": "true" }), new FakeText("Warning")), |
| 332 | new FakeElement("P").append(new FakeText("Back up first.")), |
| 333 | ); |
| 334 | const container = new FakeElement("DIV").append(callout); |
| 335 | |
| 336 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe( |
| 337 | "> [!WARNING]\n> Back up first.", |
| 338 | ); |
| 339 | const insight = new FakeElement("DIV", [], { |
| 340 | role: "note", |
| 341 | "data-markdown-alert": "insight", |
| 342 | }).append( |
| 343 | new FakeElement("P", [], { |
| 344 | "data-thread-find-ignore": "true", |
| 345 | "data-markdown-alert-label": "", |
| 346 | }).append(new FakeText("Insight")), |
| 347 | new FakeElement("P").append(new FakeText("Caches are warm.")), |
| 348 | ); |
| 349 | expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(insight)))).toBe( |
| 350 | `★ Insight ${"─".repeat(37)}\nCaches are warm.\n${"─".repeat(49)}`, |
| 351 | ); |
| 352 | }); |
| 353 | |
| 354 | it("preserves explicit ordered-list values and continues from resets when copying", () => { |
| 355 | const list = new FakeElement("OL", [], { start: "0" }).append( |
| 356 | new FakeElement("LI").append(new FakeText("A")), |
| 357 | new FakeElement("LI", [], { value: "5" }).append(new FakeText("B")), |
| 358 | new FakeElement("LI").append(new FakeText("C")), |
| 359 | new FakeElement("LI", [], { value: "0" }).append(new FakeText("D")), |
| 360 | ); |
| 361 | expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe( |
| 362 | "0. A\n5. B\n6. C\n0. D", |
| 363 | ); |
| 364 | }); |
| 365 | |
| 366 | it("keeps a bare list item that sits alongside the code block", () => { |
| 367 | const container = new FakeElement("DIV").append( |
| 368 | new FakeElement("UL").append(new FakeElement("LI")), |
| 369 | renderedCodeBlock(["pnpm test"]), |
| 370 | ); |
| 371 | |
| 372 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("-\n\npnpm test"); |
| 373 | }); |
| 374 | |
| 375 | it("copies a command and the prose after it as shown, without code fences", () => { |
| 376 | const container = new FakeElement("DIV").append( |
| 377 | renderedCodeBlock(["printf 'hello\\n'"]), |
| 378 | new FakeElement("P").append(new FakeText("T")), |
| 379 | ); |
| 380 | |
| 381 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("printf 'hello\\n'\n\nT"); |
| 382 | }); |
| 383 | |
| 384 | it("keeps a diagram's source exactly as written when copied with prose", () => { |
| 385 | const source = 'flowchart TD\n A["first \n\n\nlast"]'; |
| 386 | const container = new FakeElement("DIV").append( |
| 387 | new FakeElement("DIV", [], { |
| 388 | "data-markdown-mermaid": "", |
| 389 | "data-markdown-copy": serializeCodeBlockToMarkdown(source, "mermaid"), |
| 390 | }), |
| 391 | new FakeElement("P").append(new FakeText("After")), |
| 392 | ); |
| 393 | |
| 394 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe( |
| 395 | `\`\`\`mermaid\n${source}\n\`\`\`\n\nAfter`, |
| 396 | ); |
| 397 | }); |
| 398 | |
| 399 | it("keeps blank lines inside a code block in a list item, quote, or longer fence", () => { |
| 400 | const code = ["import os", "", "", "def main():"]; |
| 401 | const list = new FakeElement("OL").append( |
| 402 | new FakeElement("LI").append(new FakeText("Create it:\n"), renderedCodeBlock(code)), |
| 403 | ); |
| 404 | const quote = new FakeElement("BLOCKQUOTE").append( |
| 405 | new FakeElement("P").append(new FakeText("Note:")), |
| 406 | renderedCodeBlock(code), |
| 407 | ); |
| 408 | const nestedFence = new FakeElement("DIV").append( |
| 409 | new FakeElement("P").append(new FakeText("Example:")), |
| 410 | renderedCodeBlock(["```js", "a()", "", "", "b() ", "```"]), |
| 411 | ); |
| 412 | |
| 413 | expect(serializeRenderedMarkdownFragment(asNode(nestedFence))).toBe( |
| 414 | "Example:\n\n```js\na()\n\n\nb() \n```", |
| 415 | ); |
| 416 | |
| 417 | expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe( |
| 418 | "1. Create it:\n import os\n\n\n def main():", |
| 419 | ); |
| 420 | expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(quote)))).toBe( |
| 421 | "> Note:\n>\n> import os\n>\n>\n> def main():", |
| 422 | ); |
| 423 | }); |
| 424 | |
| 425 | it("omits fences when a selection past the last line drags in the whole code block", () => { |
| 426 | const container = new FakeElement("DIV").append( |
| 427 | renderedCodeBlock(["printf '%s' 'TOKEN' | gh secret set CLOUDFLARE_API_TOKEN"]), |
| 428 | new FakeText("\n"), |
| 429 | new FakeElement("P").append(new FakeText("")), |
| 430 | ); |
| 431 | |
| 432 | expect(serializeRenderedMarkdownFragment(asNode(container))).toBe( |
| 433 | "printf '%s' 'TOKEN' | gh secret set CLOUDFLARE_API_TOKEN", |
| 434 | ); |
| 435 | }); |
| 436 | }); |
| 437 | |
| 438 | function renderedFormula(tex: string): FakeElement { |
| 439 | // MarkdownMath's wrapper; its children stand in for KaTeX's glyphs. |
| 440 | return new FakeElement("SPAN", [], { |
| 441 | "data-markdown-math": "inline", |
| 442 | "data-markdown-copy": `\\(${tex}\\)`, |
| 443 | }).append(new FakeText(tex)); |
| 444 | } |
| 445 | |
| 446 | function renderedTable(...rows: Array<Array<FakeElement | FakeText>>): FakeElement { |
| 447 | const row = (tag: string, cells: Array<FakeElement | FakeText>) => |
| 448 | new FakeElement("TR").append(...cells.map((cell) => new FakeElement(tag).append(cell))); |
| 449 | const [head = [], ...body] = rows; |
| 450 | return new FakeElement("TABLE").append( |
| 451 | new FakeElement("THEAD").append(row("TH", head)), |
| 452 | new FakeElement("TBODY").append(...body.map((cells) => row("TD", cells))), |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | describe("copying rendered Markdown tables", () => { |
| 457 | beforeEach(() => { |
| 458 | vi.stubGlobal("Node", { TEXT_NODE, ELEMENT_NODE }); |
| 459 | }); |
| 460 | |
| 461 | afterEach(() => { |
| 462 | vi.unstubAllGlobals(); |
| 463 | }); |
| 464 | |
| 465 | it("keeps formula pipes as authored while escaping prose pipes", () => { |
| 466 | const table = renderedTable( |
| 467 | [new FakeText("Formula"), new FakeText("Note")], |
| 468 | [renderedFormula("|x|"), new FakeText("*a* | ~~b~~")], |
| 469 | [renderedFormula("\\|x\\|"), new FakeElement("CODE").append(new FakeText("\\(c|d\\)"))], |
| 470 | [ |
| 471 | new FakeElement("A", [], { href: "https://example.com/?q=a|b" }).append( |
| 472 | new FakeText("norm "), |
| 473 | renderedFormula("|y|"), |
| 474 | ), |
| 475 | new FakeText("e"), |
| 476 | ], |
| 477 | [ |
| 478 | new FakeElement("UL").append( |
| 479 | new FakeElement("LI").append(renderedFormula("|z|"), new FakeText(" a | b")), |
| 480 | ), |
| 481 | new FakeElement("BLOCKQUOTE").append(new FakeElement("P").append(new FakeText("c | d"))), |
| 482 | ], |
| 483 | ); |
| 484 | const markdown = serializeTableElementToMarkdown(table as unknown as Element); |
| 485 | |
| 486 | const tree = parseMarkdownWithMath(markdown); |
| 487 | const parsed = tree.children[0]; |
| 488 | if (parsed?.type !== "table") throw new Error("Expected a table"); |
| 489 | expect(parsed.children.map((row) => row.children.length)).toEqual([2, 2, 2, 2, 2]); |
| 490 | expect(markdownMathRanges(markdown, tree).map((formula) => formula.tex)).toEqual([ |
| 491 | "|x|", |
| 492 | "\\|x\\|", |
| 493 | "|y|", |
| 494 | "|z|", |
| 495 | ]); |
| 496 | expect(parsed.children[1]?.children[1]?.children).toMatchObject([ |
| 497 | { type: "text", value: "*a* | ~~b~~" }, |
| 498 | ]); |
| 499 | expect(parsed.children[4]?.children[1]?.children).toMatchObject([ |
| 500 | { type: "text", value: "> c | d" }, |
| 501 | ]); |
| 502 | const [, , codeRow, linkRow] = parsed.children; |
| 503 | expect(codeRow?.children[1]?.children[0]).toMatchObject({ |
| 504 | type: "inlineCode", |
| 505 | value: "\\(c|d\\)", |
| 506 | }); |
| 507 | expect(linkRow?.children[0]?.children[0]).toMatchObject({ |
| 508 | type: "link", |
| 509 | url: "https://example.com/?q=a|b", |
| 510 | }); |
| 511 | }); |
| 512 | |
| 513 | it("copies code and diagrams in cells without markers or unescaped pipes", () => { |
| 514 | const code = new FakeElement("PRE").append( |
| 515 | new FakeElement("CODE").append(new FakeText("a b")), |
| 516 | ); |
| 517 | const diagram = new FakeElement("DIV", [], { |
| 518 | "data-markdown-mermaid": "", |
| 519 | "data-markdown-copy": serializeCodeBlockToMarkdown("A -->|hi| B", "mermaid"), |
| 520 | }); |
| 521 | expect( |
| 522 | serializeTableElementToMarkdown( |
| 523 | asNode( |
| 524 | renderedTable([new FakeText("Command"), new FakeText("Diagram")], [code, diagram]), |
| 525 | ) as Element, |
| 526 | ), |
| 527 | ).toBe("| Command | Diagram |\n| --- | --- |\n| a b | ```mermaid A -->\\|hi\\| B ``` |"); |
| 528 | }); |
| 529 | }); |