windowsFileLinkEscapeRemarkPlugin.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import remarkParse from "remark-parse";
  2. import { unified } from "unified";
  3. import { describe, expect, it } from "vitest";
  4. import { windowsFileLinkEscapeRemarkPlugin } from "../src/lib/windowsFileLinkEscapeRemarkPlugin.js";
  5. interface ProbeNode {
  6. children?: ProbeNode[];
  7. type: string;
  8. url?: string;
  9. }
  10. function collectUrls(markdown: string): string[] {
  11. const processor = unified().use(remarkParse).use(windowsFileLinkEscapeRemarkPlugin);
  12. const file = processor.parse(markdown);
  13. const tree = processor.runSync(file, markdown) as ProbeNode;
  14. const urls: string[] = [];
  15. const visit = (node: ProbeNode): void => {
  16. if (
  17. (node.type === "link" || node.type === "image" || node.type === "definition") &&
  18. typeof node.url === "string"
  19. ) {
  20. urls.push(node.url);
  21. }
  22. node.children?.forEach(visit);
  23. };
  24. visit(tree);
  25. return urls;
  26. }
  27. describe("windowsFileLinkEscapeRemarkPlugin", () => {
  28. it("restores the backslash CommonMark ate before a dot-prefixed directory", () => {
  29. // 现场:PowerShell 兜底截图落在 .zcode 下,`\.` 被当作标点转义吃掉,
  30. // 预览请求 C:/Users/dev.zcode/... 直接 ENOENT。
  31. expect(
  32. collectUrls(
  33. String.raw`[screenshot.png](C:\Users\dev\.zcode\workspace\default\screenshot.png)`,
  34. ),
  35. ).toEqual([String.raw`C:\Users\dev\.zcode\workspace\default\screenshot.png`]);
  36. });
  37. it("restores image destinations the same way as links", () => {
  38. expect(
  39. collectUrls(String.raw`![shot](C:\Users\dev\.zcode\workspace\default\shot.png)`),
  40. ).toEqual([String.raw`C:\Users\dev\.zcode\workspace\default\shot.png`]);
  41. });
  42. it("restores UNC destinations whose leading pair collapsed to one backslash", () => {
  43. // 源码里的 `\\host` 中 `\\` 自身就是一次标点转义,解析后只剩一个反斜杠。
  44. expect(collectUrls(String.raw`[log](\\build01\share\.logs\run.txt)`)).toEqual([
  45. String.raw`\\build01\share\.logs\run.txt`,
  46. ]);
  47. });
  48. it("leaves Windows paths that lost nothing untouched", () => {
  49. const markdown = String.raw`[app](C:\Users\dev\z-code\src\app.ts)`;
  50. expect(collectUrls(markdown)).toEqual([String.raw`C:\Users\dev\z-code\src\app.ts`]);
  51. });
  52. it("restores forward-slash drive paths that still contain an escaped punctuation", () => {
  53. expect(collectUrls(String.raw`[shot](C:/Users/dev/\.zcode/shot.png)`)).toEqual([
  54. String.raw`C:/Users/dev/\.zcode/shot.png`,
  55. ]);
  56. });
  57. it("ignores non-Windows destinations", () => {
  58. expect(
  59. collectUrls(
  60. ["[docs](https://example.com/a.b)", "[rel](./src/app.ts)", "[abs](/tmp/.cache/x)"].join(
  61. "\n\n",
  62. ),
  63. ),
  64. ).toEqual(["https://example.com/a.b", "./src/app.ts", "/tmp/.cache/x"]);
  65. });
  66. it("skips destinations carrying a title rather than guessing where it starts", () => {
  67. // fail closed:title 语法一旦参与切片就可能把引号算进路径,宁可不还原。
  68. expect(collectUrls(String.raw`[shot](C:\Users\dev\.zcode\a.png "标题")`)).toEqual([
  69. String.raw`C:\Users\dev.zcode\a.png`,
  70. ]);
  71. });
  72. it("skips angle-bracket destinations", () => {
  73. expect(collectUrls(String.raw`[shot](<C:\Users\dev\.zcode\a.png>)`)).toEqual([
  74. String.raw`C:\Users\dev.zcode\a.png`,
  75. ]);
  76. });
  77. it("restores reference-style definitions that back a link reference", () => {
  78. // 引用式链接的 URL 由 definition 提供,同一处转义丢失在这条路径上同样成立。
  79. const markdown = [
  80. String.raw`[shot][ref]`,
  81. "",
  82. String.raw`[ref]: C:\Users\dev\.zcode\a.png`,
  83. ].join("\n");
  84. expect(collectUrls(markdown)).toEqual([String.raw`C:\Users\dev\.zcode\a.png`]);
  85. });
  86. it("keeps the parsed url when the raw slice does not round-trip", () => {
  87. // 实体引用会在解析期被解开,反转义等式不成立 → 放弃还原。
  88. expect(collectUrls(String.raw`[shot](C:\Users\a&amp;b\.zcode\a.png)`)).toEqual([
  89. String.raw`C:\Users\a&b.zcode\a.png`,
  90. ]);
  91. });
  92. });