73 lines
3.0 KiB
TypeScript
73 lines
3.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { splitMarkdownImages } from "../../lib/canvas/markdown-image-segments";
|
|
|
|
const image = "https://example.com/folder/test.jpg";
|
|
|
|
test("SKU output consumes the entire redundant linked-image wrapper", () => {
|
|
const text = `生成图\n[](${image})\n下载完成`;
|
|
assert.deepEqual(splitMarkdownImages(text), [
|
|
{ type: "text", value: "生成图\n" },
|
|
{ type: "image", alt: "第2行", src: image },
|
|
{ type: "text", value: "\n下载完成" },
|
|
]);
|
|
});
|
|
|
|
test("several linked and ordinary images do not leave broken wrapper text", () => {
|
|
const parts = splitMarkdownImages(`[](${image})\n`);
|
|
assert.deepEqual(parts, [
|
|
{ type: "image", alt: "a", src: image },
|
|
{ type: "text", value: "\n" },
|
|
{ type: "image", alt: "b", src: "https://example.com/b.png" },
|
|
]);
|
|
});
|
|
|
|
test("different outer links and ordinary links are preserved verbatim", () => {
|
|
const nested = `[](https://example.com/full.jpg)`;
|
|
const input = `[文档](https://example.com/doc) ${nested} `;
|
|
assert.deepEqual(splitMarkdownImages(input), [
|
|
{ type: "text", value: `[文档](https://example.com/doc) ${nested} ` },
|
|
{ type: "image", alt: "plain", src: "https://example.com/next.jpg" },
|
|
]);
|
|
});
|
|
|
|
test("supports balanced and escaped parentheses and nested/escaped alt text", () => {
|
|
const url = "https://example.com/a_(1)/x.jpg";
|
|
assert.deepEqual(splitMarkdownImages(`[![a [b] \\] c](${url})](${url})`), [
|
|
{ type: "image", alt: "a [b] ] c", src: url },
|
|
]);
|
|
assert.deepEqual(splitMarkdownImages(".jpg)"), [
|
|
{ type: "image", alt: "a", src: "https://example.com/a(1).jpg" },
|
|
]);
|
|
});
|
|
|
|
test("does not extract examples in inline and fenced code", () => {
|
|
const input = `\`\`\n\`\`\`md\n[](${image})\n\`\`\`\n~~~md\n\n~~~\n`;
|
|
const parts = splitMarkdownImages(input);
|
|
assert.equal(parts.filter((part) => part.type === "image").length, 1);
|
|
assert.equal(parts[0].type, "text");
|
|
assert.ok(parts[0].type === "text" && parts[0].value.includes("![code]"));
|
|
assert.deepEqual(parts[1], { type: "image", alt: "actual", src: image });
|
|
});
|
|
|
|
test("preserves unsafe, incomplete, and escaped markup for the existing renderer", () => {
|
|
for (const content of [
|
|
`[](javascript:alert(1))`,
|
|
`[](`,
|
|
`\\`,
|
|
"",
|
|
]) assert.deepEqual(splitMarkdownImages(content), [{ type: "text", value: content }]);
|
|
});
|
|
|
|
test("retains existing thumbnail metadata in alt text", () => {
|
|
for (const alt of [
|
|
"thumb|||https://example.com/original.jpg|||",
|
|
"thumb$$https://example.com/original.jpg$$",
|
|
"thumb|https://example.com/original.jpg|",
|
|
]) {
|
|
assert.deepEqual(splitMarkdownImages(``), [
|
|
{ type: "image", alt, src: image },
|
|
]);
|
|
}
|
|
});
|