Files
multica/packages/views/editor/utils/escape-markdown-label.test.ts
LinYushen d013a31db9 fix: escape special chars in image alt and file-card filename (MUL-2899) (#3644)
* fix: escape special chars in image alt and file-card filename during Markdown serialization

Filenames containing Markdown label characters ([, ], \, (, )) broke
the ![alt](url) and !file[name](url) syntax, causing raw Markdown to
render instead of the image/file card.

- Add shared escapeMarkdownLabel utility
- Apply escaping in file-card renderMarkdown
- Add renderMarkdown to ImageExtension for alt text escaping
- Add regression tests

Closes #3616

Co-authored-by: multica-agent <github@multica.ai>

* fix: address review — fix tokenizer regex, unescape labels, add regression tests

- Remove unused tokenizeFn (TS6133)
- Change file-card regex to (?:\\.|[^\]])* to handle escaped brackets
- Unescape labels in tokenize() and preprocessFileCards()
- Export ImageExtension for testability
- Rewrite tests: 3 describe blocks covering ImageExtension.renderMarkdown,
  file-card tokenizer round-trip, and preprocessFileCards (6 tests total)
- typecheck and vitest both pass

Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: multica-agent <github@multica.ai>
2026-06-02 14:33:45 +08:00

27 lines
784 B
TypeScript

import { describe, it, expect } from "vitest";
import { escapeMarkdownLabel } from "./escape-markdown-label";
describe("escapeMarkdownLabel", () => {
it("escapes [ and ]", () => {
expect(escapeMarkdownLabel("photo[1].png")).toBe("photo\\[1\\].png");
});
it("escapes backslash", () => {
expect(escapeMarkdownLabel("a\\b")).toBe("a\\\\b");
});
it("escapes ( and )", () => {
expect(escapeMarkdownLabel("file(1).txt")).toBe("file\\(1\\).txt");
});
it("escapes all special chars together", () => {
expect(escapeMarkdownLabel("6P4N\\`X[A~Z(S@XO}WE0FT_P.jpg")).toBe(
"6P4N\\\\`X\\[A~Z\\(S@XO}WE0FT_P.jpg",
);
});
it("leaves normal text unchanged", () => {
expect(escapeMarkdownLabel("hello world.png")).toBe("hello world.png");
});
});