@real1ty-obsidian-plugins/utils 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ import { normalizePath } from "obsidian";
2
+
3
+ import { formatWikiLink, parsePropertyLinks } from "./link-parser";
4
+
5
+ /**
6
+ * Adds a link to a property, avoiding duplicates using normalized path comparison.
7
+ * Prevents cycles and duplicate relationships by comparing normalized paths.
8
+ *
9
+ * **Important**: linkPath should be WITHOUT .md extension (wikilink format).
10
+ *
11
+ * @param currentValue - The current property value (can be string, string[], or undefined)
12
+ * @param linkPath - The file path to add (without .md extension, e.g., "folder/file")
13
+ * @returns New array with link added, or same array if link already exists
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * addLinkToProperty(undefined, "MyNote") // ["[[MyNote]]"]
18
+ * addLinkToProperty("[[Note1]]", "Note2") // ["[[Note1]]", "[[Note2]]"]
19
+ * addLinkToProperty(["[[Note1]]"], "Note2") // ["[[Note1]]", "[[Note2]]"]
20
+ * addLinkToProperty(["[[Note1]]"], "Note1") // ["[[Note1]]"] (no change - duplicate prevented)
21
+ * addLinkToProperty(["[[Folder/Note]]"], "folder/note") // ["[[Folder/Note]]", "[[folder/note|note]]"] (case-sensitive, different entry)
22
+ * ```
23
+ */
24
+ export function addLinkToProperty(
25
+ currentValue: string | string[] | undefined,
26
+ linkPath: string
27
+ ): string[] {
28
+ // Handle undefined or null
29
+ if (currentValue === undefined || currentValue === null) {
30
+ return [formatWikiLink(linkPath)];
31
+ }
32
+
33
+ // Normalize to array
34
+ const currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];
35
+
36
+ const existingPaths = parsePropertyLinks(currentArray);
37
+
38
+ // Normalize paths for comparison to prevent duplicates with different casing or separators
39
+ const normalizedLinkPath = normalizePath(linkPath);
40
+
41
+ const normalizedExistingPaths = existingPaths.map((p) => normalizePath(p));
42
+
43
+ // Only add if not already present (using normalized path comparison)
44
+ if (!normalizedExistingPaths.includes(normalizedLinkPath)) {
45
+ return [...currentArray, formatWikiLink(linkPath)];
46
+ }
47
+
48
+ return currentArray;
49
+ }
50
+
51
+ /**
52
+ * Removes a link from a property using normalized path comparison.
53
+ *
54
+ * @param currentValue - The current property value (can be string, string[], or undefined)
55
+ * @param linkPath - The file path to remove (without .md extension)
56
+ * @returns New array with link removed (can be empty)
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * removeLinkFromProperty(["[[Note1]]", "[[Note2]]"], "Note1") // ["[[Note2]]"]
61
+ * removeLinkFromProperty(["[[Note1]]"], "Note1") // []
62
+ * removeLinkFromProperty("[[Note1]]", "Note1") // []
63
+ * removeLinkFromProperty(undefined, "Note1") // []
64
+ * removeLinkFromProperty(["[[Folder/Note]]"], "Folder/Note") // [] (case-sensitive removal)
65
+ * ```
66
+ */
67
+ export function removeLinkFromProperty(
68
+ currentValue: string | string[] | undefined,
69
+ linkPath: string
70
+ ): string[] {
71
+ if (currentValue === undefined || currentValue === null) {
72
+ return [];
73
+ }
74
+
75
+ // Normalize to array
76
+ const currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];
77
+
78
+ const normalizedLinkPath = normalizePath(linkPath);
79
+
80
+ return currentArray.filter((item) => {
81
+ const parsed = parsePropertyLinks([item])[0];
82
+
83
+ if (!parsed) return true; // Keep invalid entries
84
+
85
+ return normalizePath(parsed) !== normalizedLinkPath;
86
+ });
87
+ }
88
+
89
+ /**
90
+ * Checks if a link exists in a property using normalized path comparison.
91
+ *
92
+ * @param currentValue - The current property value (can be string, string[], or undefined)
93
+ * @param linkPath - The file path to check (without .md extension)
94
+ * @returns True if the link exists
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * hasLinkInProperty(["[[Note1]]", "[[Note2]]"], "Note1") // true
99
+ * hasLinkInProperty("[[Note1]]", "Note1") // true
100
+ * hasLinkInProperty([], "Note1") // false
101
+ * hasLinkInProperty(undefined, "Note1") // false
102
+ * hasLinkInProperty(["[[Folder/Note]]"], "Folder/Note") // true (case-sensitive match)
103
+ * ```
104
+ */
105
+ export function hasLinkInProperty(
106
+ currentValue: string | string[] | undefined,
107
+ linkPath: string
108
+ ): boolean {
109
+ const existingPaths = parsePropertyLinks(currentValue);
110
+
111
+ const normalizedLinkPath = normalizePath(linkPath);
112
+
113
+ return existingPaths.some((path) => normalizePath(path) === normalizedLinkPath);
114
+ }