promptMentionLabel.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import type { MentionCategory } from "@/mentions/mentionTypes.js";
  2. function unescapePromptMentionMarkdownText(text: string): string {
  3. let result = "";
  4. for (let index = 0; index < text.length; index += 1) {
  5. if (text[index] === "\\" && index + 1 < text.length) {
  6. result += text[index + 1];
  7. index += 1;
  8. continue;
  9. }
  10. result += text[index];
  11. }
  12. return result;
  13. }
  14. function extractPromptMentionMarkdownLabel(text: string): string | null {
  15. const matched = /^\[((?:\\.|[^\\\]])*)\]\((?:<((?:\\.|[^>])*?)>|((?:\\.|[^)])*))\)$/.exec(
  16. text.trim(),
  17. );
  18. const label = matched?.[1];
  19. return label ? unescapePromptMentionMarkdownText(label) : null;
  20. }
  21. export function normalizePromptMentionDisplayLabel(
  22. category: MentionCategory,
  23. label: string,
  24. value: string,
  25. ): string {
  26. const markdownLabel = extractPromptMentionMarkdownLabel(label);
  27. let displayLabel = markdownLabel ?? label;
  28. if (category === "skills" && displayLabel.startsWith("$")) {
  29. displayLabel = displayLabel.slice(1);
  30. } else if (category === "sessions" && displayLabel.startsWith("#")) {
  31. displayLabel = displayLabel.slice(1);
  32. } else if (
  33. (category === "files" ||
  34. category === "subagents" ||
  35. category === "whiteboards" ||
  36. category === "plugins") &&
  37. displayLabel.startsWith("@")
  38. ) {
  39. displayLabel = displayLabel.slice(1);
  40. }
  41. // 旧草稿快照里可能把完整 markdown 链接写进 mention node 的 text 字段。
  42. // 这会让恢复后的 tag 图标还在,但文本显示成 `[$skill](path)` 原文。显示层只取人类可读 label,
  43. // markdown 字段继续保留完整原文用于发送。
  44. return displayLabel.trim() || value;
  45. }