|
1 | 1 | import { describe, expect, it } from "vite-plus/test"; |
2 | 2 |
|
3 | 3 | import { splitMarkdownDetails } from "./markdownDetails"; |
| 4 | +import { |
| 5 | + markdownForNativeParsing, |
| 6 | + markdownReferenceDefinitions, |
| 7 | + withReferenceDefinitions, |
| 8 | +} from "./markdownDocument"; |
| 9 | +import { parseMarkdown } from "react-native-enriched-markdown/src/web/parseMarkdown"; |
| 10 | + |
| 11 | +type NativeNode = Awaited<ReturnType<typeof parseMarkdown>>; |
| 12 | + |
| 13 | +/** Where the native parser sends each link and image in one rendered fragment of a reply. */ |
| 14 | +async function referenceDestinations(reply: string, pick: number) { |
| 15 | + const segment = splitMarkdownDetails(reply)[pick]; |
| 16 | + const fragment = segment?.kind === "details" ? segment.body : (segment?.markdown ?? ""); |
| 17 | + const urls: Array<string | undefined> = []; |
| 18 | + const visit = (node: NativeNode) => { |
| 19 | + if (node.type === "Link" || node.type === "Image") urls.push(node.attributes?.url); |
| 20 | + node.children?.forEach(visit); |
| 21 | + }; |
| 22 | + visit( |
| 23 | + await parseMarkdown( |
| 24 | + markdownForNativeParsing( |
| 25 | + withReferenceDefinitions(fragment, markdownReferenceDefinitions(reply)), |
| 26 | + ), |
| 27 | + ), |
| 28 | + ); |
| 29 | + return urls; |
| 30 | +} |
4 | 31 |
|
5 | 32 | describe("splitMarkdownDetails", () => { |
6 | 33 | // regression: #28059 mobile chat showed every <details> fold open |
@@ -78,3 +105,57 @@ describe("splitMarkdownDetails", () => { |
78 | 105 | ).toMatchObject([{ kind: "details", open: false }]); |
79 | 106 | }); |
80 | 107 | }); |
| 108 | + |
| 109 | +describe("withReferenceDefinitions", () => { |
| 110 | + it("resolves a fragment's references from the whole reply, its own definitions first", async () => { |
| 111 | + const fold = "\n\n<details><summary>A</summary>a</details>\n\n"; |
| 112 | + for (const [reply, pick, expected] of [ |
| 113 | + // Into a fold, with an escaped label and a Windows destination kept as written. |
| 114 | + [ |
| 115 | + "<details>\n<summary>A</summary>\n\n[config][c] ![B][a\\]b]\n\n</details>\n\n[c]: C:\\work\\.t3\\settings.json\n[a\\]b]: https://example.com/a", |
| 116 | + 0, |
| 117 | + ["C:\\work\\.t3\\settings.json", "https://example.com/a"], |
| 118 | + ], |
| 119 | + // Out of a fold. |
| 120 | + [ |
| 121 | + "See [d].\n\n<details>\n<summary>A</summary>\n\n[d]: https://example.com\n\n</details>", |
| 122 | + 0, |
| 123 | + ["https://example.com"], |
| 124 | + ], |
| 125 | + // A quoted definition that wraps onto continuation lines. |
| 126 | + [`See [d].${fold}> [d]:\n> https://example.com\n> "Title"`, 0, ["https://example.com"]], |
| 127 | + // A definition the compact HTML block hides from the reply's parse, with a label that wraps. |
| 128 | + [ |
| 129 | + "See [the\n doc].\n\n<details><summary>A</summary>a</details>\n[The Doc]: https://example.com", |
| 130 | + 0, |
| 131 | + ["https://example.com"], |
| 132 | + ], |
| 133 | + // A literal ">" in a wrapped label is part of the label, not a quote marker. |
| 134 | + [ |
| 135 | + `[part rest] and [part > rest]${fold}[part\n > rest]: https://example.com/a\n\n[part rest]: https://example.com/b`, |
| 136 | + 0, |
| 137 | + ["https://example.com/b", "https://example.com/a"], |
| 138 | + ], |
| 139 | + // A fragment's own definition wins, however the parser equates the two labels. |
| 140 | + [ |
| 141 | + "[ x ]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [x].\n\n[X]: https://inside.example\n\n</details>", |
| 142 | + 1, |
| 143 | + ["https://inside.example"], |
| 144 | + ], |
| 145 | + [ |
| 146 | + "[SS]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [SS].\n\n[\u00df]: https://inside.example\n\n</details>", |
| 147 | + 1, |
| 148 | + ["https://inside.example"], |
| 149 | + ], |
| 150 | + ] as const) { |
| 151 | + expect(await referenceDestinations(reply, pick)).toEqual(expected); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + it("leaves a fragment with nothing to resolve as written", () => { |
| 156 | + expect(withReferenceDefinitions("See [d].", [])).toBe("See [d]."); |
| 157 | + expect(withReferenceDefinitions("Plain text.", ["[d]: https://example.com"])).toBe( |
| 158 | + "Plain text.", |
| 159 | + ); |
| 160 | + }); |
| 161 | +}); |
0 commit comments