@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.
@@ -3,5 +3,6 @@ export * from "./file";
3
3
  export * from "./file-operations";
4
4
  export * from "./frontmatter";
5
5
  export * from "./link-parser";
6
+ export * from "./property-utils";
6
7
  export * from "./templater";
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
@@ -3,5 +3,6 @@ export * from "./file";
3
3
  export * from "./file-operations";
4
4
  export * from "./frontmatter";
5
5
  export * from "./link-parser";
6
+ export * from "./property-utils";
6
7
  export * from "./templater";
7
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC","sourcesContent":["export * from \"./child-reference\";\nexport * from \"./file\";\nexport * from \"./file-operations\";\nexport * from \"./frontmatter\";\nexport * from \"./link-parser\";\nexport * from \"./templater\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC","sourcesContent":["export * from \"./child-reference\";\nexport * from \"./file\";\nexport * from \"./file-operations\";\nexport * from \"./frontmatter\";\nexport * from \"./link-parser\";\nexport * from \"./property-utils\";\nexport * from \"./templater\";\n"]}
@@ -6,4 +6,30 @@
6
6
  * Normalizes paths and handles malformed links gracefully.
7
7
  */
8
8
  export declare const extractFilePathFromLink: (link: string) => string | null;
9
+ /**
10
+ * Parses an Obsidian wiki link to extract the file path.
11
+ * Handles formats like:
12
+ * - [[path/to/file]]
13
+ * - [[path/to/file|Display Name]]
14
+ *
15
+ * @param link - The wiki link string
16
+ * @returns The file path without the [[ ]] brackets, or null if invalid
17
+ */
18
+ export declare function parseWikiLink(link: string): string | null;
19
+ /**
20
+ * Parses a property value that can be a single link or an array of links.
21
+ * Extracts file paths from all valid wiki links.
22
+ *
23
+ * @param value - The property value (string, string[], or undefined)
24
+ * @returns Array of file paths, empty if no valid links found
25
+ */
26
+ export declare function parsePropertyLinks(value: string | string[] | undefined): string[];
27
+ /**
28
+ * Formats a file path as an Obsidian wiki link with display name.
29
+ * Example: "Projects/MyProject" -> "[[Projects/MyProject|MyProject]]"
30
+ *
31
+ * @param filePath - The file path to format
32
+ * @returns The formatted wiki link with display name alias
33
+ */
34
+ export declare function formatWikiLink(filePath: string): string;
9
35
  //# sourceMappingURL=link-parser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"link-parser.d.ts","sourceRoot":"","sources":["../../src/file/link-parser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,GAAI,MAAM,MAAM,KAAG,MAAM,GAAG,IAU/D,CAAC"}
1
+ {"version":3,"file":"link-parser.d.ts","sourceRoot":"","sources":["../../src/file/link-parser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,GAAI,MAAM,MAAM,KAAG,MAAM,GAAG,IAU/D,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAezD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,GAAG,MAAM,EAAE,CAQjF;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAqBvD"}
@@ -16,4 +16,63 @@ export const extractFilePathFromLink = (link) => {
16
16
  return null;
17
17
  return filePath.endsWith(".md") ? filePath : `${filePath}.md`;
18
18
  };
19
+ /**
20
+ * Parses an Obsidian wiki link to extract the file path.
21
+ * Handles formats like:
22
+ * - [[path/to/file]]
23
+ * - [[path/to/file|Display Name]]
24
+ *
25
+ * @param link - The wiki link string
26
+ * @returns The file path without the [[ ]] brackets, or null if invalid
27
+ */
28
+ export function parseWikiLink(link) {
29
+ if (!link || typeof link !== "string") {
30
+ return null;
31
+ }
32
+ const trimmed = link.trim();
33
+ // Match [[path/to/file]] or [[path/to/file|Display Name]]
34
+ const match = trimmed.match(/^\[\[([^\]|]+)(?:\|[^\]]+)?\]\]$/);
35
+ if (!match) {
36
+ return null;
37
+ }
38
+ return match[1].trim();
39
+ }
40
+ /**
41
+ * Parses a property value that can be a single link or an array of links.
42
+ * Extracts file paths from all valid wiki links.
43
+ *
44
+ * @param value - The property value (string, string[], or undefined)
45
+ * @returns Array of file paths, empty if no valid links found
46
+ */
47
+ export function parsePropertyLinks(value) {
48
+ if (!value) {
49
+ return [];
50
+ }
51
+ const links = Array.isArray(value) ? value : [value];
52
+ return links.map((link) => parseWikiLink(link)).filter((path) => path !== null);
53
+ }
54
+ /**
55
+ * Formats a file path as an Obsidian wiki link with display name.
56
+ * Example: "Projects/MyProject" -> "[[Projects/MyProject|MyProject]]"
57
+ *
58
+ * @param filePath - The file path to format
59
+ * @returns The formatted wiki link with display name alias
60
+ */
61
+ export function formatWikiLink(filePath) {
62
+ if (!filePath || typeof filePath !== "string") {
63
+ return "";
64
+ }
65
+ const trimmed = filePath.trim();
66
+ if (!trimmed) {
67
+ return "";
68
+ }
69
+ // Extract the filename (last segment after the last /)
70
+ const segments = trimmed.split("/");
71
+ const displayName = segments[segments.length - 1];
72
+ // If there's no path separator, just return simple link
73
+ if (segments.length === 1) {
74
+ return `[[${trimmed}]]`;
75
+ }
76
+ return `[[${trimmed}|${displayName}]]`;
77
+ }
19
78
  //# sourceMappingURL=link-parser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"link-parser.js","sourceRoot":"","sources":["../../src/file/link-parser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAiB,EAAE;IACtE,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,8BAA8B,CAAC,CAAC;IACzD,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IAExB,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,CAAC,QAAQ;QAAE,OAAO,IAAI,CAAC;IAE3B,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAEpE,OAAO,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,KAAK,CAAC;AAC/D,CAAC,CAAC","sourcesContent":["/**\n * Handles different link formats and edge cases:\n * [[FileName]] -> FileName.md\n * [[Folder/FileName]] -> Folder/FileName.md\n * [[Folder/FileName|DisplayName]] -> Folder/FileName.md\n * Normalizes paths and handles malformed links gracefully.\n */\nexport const extractFilePathFromLink = (link: string): string | null => {\n\tconst match = link.match(/\\[\\[([^|\\]]+?)(?:\\|.*?)?\\]\\]/);\n\tif (!match) return null;\n\n\tconst filePath = match[1].trim();\n\tif (!filePath) return null;\n\n\tif (filePath.includes(\"[[\") || filePath.includes(\"]]\")) return null;\n\n\treturn filePath.endsWith(\".md\") ? filePath : `${filePath}.md`;\n};\n"]}
1
+ {"version":3,"file":"link-parser.js","sourceRoot":"","sources":["../../src/file/link-parser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAiB,EAAE;IACtE,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,8BAA8B,CAAC,CAAC;IACzD,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IAExB,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,CAAC,QAAQ;QAAE,OAAO,IAAI,CAAC;IAE3B,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAEpE,OAAO,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,KAAK,CAAC;AAC/D,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACzC,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;IA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AED,OAAO,KAAK,OAAO,IAAI,WAAW,IAAI,CAAC;AACxC,CAAC","sourcesContent":["/**\n * Handles different link formats and edge cases:\n * [[FileName]] -> FileName.md\n * [[Folder/FileName]] -> Folder/FileName.md\n * [[Folder/FileName|DisplayName]] -> Folder/FileName.md\n * Normalizes paths and handles malformed links gracefully.\n */\nexport const extractFilePathFromLink = (link: string): string | null => {\n\tconst match = link.match(/\\[\\[([^|\\]]+?)(?:\\|.*?)?\\]\\]/);\n\tif (!match) return null;\n\n\tconst filePath = match[1].trim();\n\tif (!filePath) return null;\n\n\tif (filePath.includes(\"[[\") || filePath.includes(\"]]\")) return null;\n\n\treturn filePath.endsWith(\".md\") ? filePath : `${filePath}.md`;\n};\n\n/**\n * Parses an Obsidian wiki link to extract the file path.\n * Handles formats like:\n * - [[path/to/file]]\n * - [[path/to/file|Display Name]]\n *\n * @param link - The wiki link string\n * @returns The file path without the [[ ]] brackets, or null if invalid\n */\nexport function parseWikiLink(link: string): string | null {\n\tif (!link || typeof link !== \"string\") {\n\t\treturn null;\n\t}\n\n\tconst trimmed = link.trim();\n\n\t// Match [[path/to/file]] or [[path/to/file|Display Name]]\n\tconst match = trimmed.match(/^\\[\\[([^\\]|]+)(?:\\|[^\\]]+)?\\]\\]$/);\n\n\tif (!match) {\n\t\treturn null;\n\t}\n\n\treturn match[1].trim();\n}\n\n/**\n * Parses a property value that can be a single link or an array of links.\n * Extracts file paths from all valid wiki links.\n *\n * @param value - The property value (string, string[], or undefined)\n * @returns Array of file paths, empty if no valid links found\n */\nexport function parsePropertyLinks(value: string | string[] | undefined): string[] {\n\tif (!value) {\n\t\treturn [];\n\t}\n\n\tconst links = Array.isArray(value) ? value : [value];\n\n\treturn links.map((link) => parseWikiLink(link)).filter((path): path is string => path !== null);\n}\n\n/**\n * Formats a file path as an Obsidian wiki link with display name.\n * Example: \"Projects/MyProject\" -> \"[[Projects/MyProject|MyProject]]\"\n *\n * @param filePath - The file path to format\n * @returns The formatted wiki link with display name alias\n */\nexport function formatWikiLink(filePath: string): string {\n\tif (!filePath || typeof filePath !== \"string\") {\n\t\treturn \"\";\n\t}\n\n\tconst trimmed = filePath.trim();\n\n\tif (!trimmed) {\n\t\treturn \"\";\n\t}\n\n\t// Extract the filename (last segment after the last /)\n\tconst segments = trimmed.split(\"/\");\n\tconst displayName = segments[segments.length - 1];\n\n\t// If there's no path separator, just return simple link\n\tif (segments.length === 1) {\n\t\treturn `[[${trimmed}]]`;\n\t}\n\n\treturn `[[${trimmed}|${displayName}]]`;\n}\n"]}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Adds a link to a property, avoiding duplicates using normalized path comparison.
3
+ * Prevents cycles and duplicate relationships by comparing normalized paths.
4
+ *
5
+ * **Important**: linkPath should be WITHOUT .md extension (wikilink format).
6
+ *
7
+ * @param currentValue - The current property value (can be string, string[], or undefined)
8
+ * @param linkPath - The file path to add (without .md extension, e.g., "folder/file")
9
+ * @returns New array with link added, or same array if link already exists
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * addLinkToProperty(undefined, "MyNote") // ["[[MyNote]]"]
14
+ * addLinkToProperty("[[Note1]]", "Note2") // ["[[Note1]]", "[[Note2]]"]
15
+ * addLinkToProperty(["[[Note1]]"], "Note2") // ["[[Note1]]", "[[Note2]]"]
16
+ * addLinkToProperty(["[[Note1]]"], "Note1") // ["[[Note1]]"] (no change - duplicate prevented)
17
+ * addLinkToProperty(["[[Folder/Note]]"], "folder/note") // ["[[Folder/Note]]", "[[folder/note|note]]"] (case-sensitive, different entry)
18
+ * ```
19
+ */
20
+ export declare function addLinkToProperty(currentValue: string | string[] | undefined, linkPath: string): string[];
21
+ /**
22
+ * Removes a link from a property using normalized path comparison.
23
+ *
24
+ * @param currentValue - The current property value (can be string, string[], or undefined)
25
+ * @param linkPath - The file path to remove (without .md extension)
26
+ * @returns New array with link removed (can be empty)
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * removeLinkFromProperty(["[[Note1]]", "[[Note2]]"], "Note1") // ["[[Note2]]"]
31
+ * removeLinkFromProperty(["[[Note1]]"], "Note1") // []
32
+ * removeLinkFromProperty("[[Note1]]", "Note1") // []
33
+ * removeLinkFromProperty(undefined, "Note1") // []
34
+ * removeLinkFromProperty(["[[Folder/Note]]"], "Folder/Note") // [] (case-sensitive removal)
35
+ * ```
36
+ */
37
+ export declare function removeLinkFromProperty(currentValue: string | string[] | undefined, linkPath: string): string[];
38
+ /**
39
+ * Checks if a link exists in a property using normalized path comparison.
40
+ *
41
+ * @param currentValue - The current property value (can be string, string[], or undefined)
42
+ * @param linkPath - The file path to check (without .md extension)
43
+ * @returns True if the link exists
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * hasLinkInProperty(["[[Note1]]", "[[Note2]]"], "Note1") // true
48
+ * hasLinkInProperty("[[Note1]]", "Note1") // true
49
+ * hasLinkInProperty([], "Note1") // false
50
+ * hasLinkInProperty(undefined, "Note1") // false
51
+ * hasLinkInProperty(["[[Folder/Note]]"], "Folder/Note") // true (case-sensitive match)
52
+ * ```
53
+ */
54
+ export declare function hasLinkInProperty(currentValue: string | string[] | undefined, linkPath: string): boolean;
55
+ //# sourceMappingURL=property-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"property-utils.d.ts","sourceRoot":"","sources":["../../src/file/property-utils.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,CAChC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,EAC3C,QAAQ,EAAE,MAAM,GACd,MAAM,EAAE,CAsBV;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,sBAAsB,CACrC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,EAC3C,QAAQ,EAAE,MAAM,GACd,MAAM,EAAE,CAiBV;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAChC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,EAC3C,QAAQ,EAAE,MAAM,GACd,OAAO,CAMT"}
@@ -0,0 +1,90 @@
1
+ import { normalizePath } from "obsidian";
2
+ import { formatWikiLink, parsePropertyLinks } from "./link-parser";
3
+ /**
4
+ * Adds a link to a property, avoiding duplicates using normalized path comparison.
5
+ * Prevents cycles and duplicate relationships by comparing normalized paths.
6
+ *
7
+ * **Important**: linkPath should be WITHOUT .md extension (wikilink format).
8
+ *
9
+ * @param currentValue - The current property value (can be string, string[], or undefined)
10
+ * @param linkPath - The file path to add (without .md extension, e.g., "folder/file")
11
+ * @returns New array with link added, or same array if link already exists
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * addLinkToProperty(undefined, "MyNote") // ["[[MyNote]]"]
16
+ * addLinkToProperty("[[Note1]]", "Note2") // ["[[Note1]]", "[[Note2]]"]
17
+ * addLinkToProperty(["[[Note1]]"], "Note2") // ["[[Note1]]", "[[Note2]]"]
18
+ * addLinkToProperty(["[[Note1]]"], "Note1") // ["[[Note1]]"] (no change - duplicate prevented)
19
+ * addLinkToProperty(["[[Folder/Note]]"], "folder/note") // ["[[Folder/Note]]", "[[folder/note|note]]"] (case-sensitive, different entry)
20
+ * ```
21
+ */
22
+ export function addLinkToProperty(currentValue, linkPath) {
23
+ // Handle undefined or null
24
+ if (currentValue === undefined || currentValue === null) {
25
+ return [formatWikiLink(linkPath)];
26
+ }
27
+ // Normalize to array
28
+ const currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];
29
+ const existingPaths = parsePropertyLinks(currentArray);
30
+ // Normalize paths for comparison to prevent duplicates with different casing or separators
31
+ const normalizedLinkPath = normalizePath(linkPath);
32
+ const normalizedExistingPaths = existingPaths.map((p) => normalizePath(p));
33
+ // Only add if not already present (using normalized path comparison)
34
+ if (!normalizedExistingPaths.includes(normalizedLinkPath)) {
35
+ return [...currentArray, formatWikiLink(linkPath)];
36
+ }
37
+ return currentArray;
38
+ }
39
+ /**
40
+ * Removes a link from a property using normalized path comparison.
41
+ *
42
+ * @param currentValue - The current property value (can be string, string[], or undefined)
43
+ * @param linkPath - The file path to remove (without .md extension)
44
+ * @returns New array with link removed (can be empty)
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * removeLinkFromProperty(["[[Note1]]", "[[Note2]]"], "Note1") // ["[[Note2]]"]
49
+ * removeLinkFromProperty(["[[Note1]]"], "Note1") // []
50
+ * removeLinkFromProperty("[[Note1]]", "Note1") // []
51
+ * removeLinkFromProperty(undefined, "Note1") // []
52
+ * removeLinkFromProperty(["[[Folder/Note]]"], "Folder/Note") // [] (case-sensitive removal)
53
+ * ```
54
+ */
55
+ export function removeLinkFromProperty(currentValue, linkPath) {
56
+ if (currentValue === undefined || currentValue === null) {
57
+ return [];
58
+ }
59
+ // Normalize to array
60
+ const currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];
61
+ const normalizedLinkPath = normalizePath(linkPath);
62
+ return currentArray.filter((item) => {
63
+ const parsed = parsePropertyLinks([item])[0];
64
+ if (!parsed)
65
+ return true; // Keep invalid entries
66
+ return normalizePath(parsed) !== normalizedLinkPath;
67
+ });
68
+ }
69
+ /**
70
+ * Checks if a link exists in a property using normalized path comparison.
71
+ *
72
+ * @param currentValue - The current property value (can be string, string[], or undefined)
73
+ * @param linkPath - The file path to check (without .md extension)
74
+ * @returns True if the link exists
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * hasLinkInProperty(["[[Note1]]", "[[Note2]]"], "Note1") // true
79
+ * hasLinkInProperty("[[Note1]]", "Note1") // true
80
+ * hasLinkInProperty([], "Note1") // false
81
+ * hasLinkInProperty(undefined, "Note1") // false
82
+ * hasLinkInProperty(["[[Folder/Note]]"], "Folder/Note") // true (case-sensitive match)
83
+ * ```
84
+ */
85
+ export function hasLinkInProperty(currentValue, linkPath) {
86
+ const existingPaths = parsePropertyLinks(currentValue);
87
+ const normalizedLinkPath = normalizePath(linkPath);
88
+ return existingPaths.some((path) => normalizePath(path) === normalizedLinkPath);
89
+ }
90
+ //# sourceMappingURL=property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AED,qBAAqB;IACrB,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;IAEjF,MAAM,kBAAkB,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;QACnC,MAAM,MAAM,GAAG,kBAAkB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAE7C,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,CAAC,uBAAuB;QAEjD,OAAO,aAAa,CAAC,MAAM,CAAC,KAAK,kBAAkB,CAAC;IACrD,CAAC,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,iBAAiB,CAChC,YAA2C,EAC3C,QAAgB;IAEhB,MAAM,aAAa,GAAG,kBAAkB,CAAC,YAAY,CAAC,CAAC;IAEvD,MAAM,kBAAkB,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,kBAAkB,CAAC,CAAC;AACjF,CAAC","sourcesContent":["import { normalizePath } from \"obsidian\";\n\nimport { formatWikiLink, parsePropertyLinks } from \"./link-parser\";\n\n/**\n * Adds a link to a property, avoiding duplicates using normalized path comparison.\n * Prevents cycles and duplicate relationships by comparing normalized paths.\n *\n * **Important**: linkPath should be WITHOUT .md extension (wikilink format).\n *\n * @param currentValue - The current property value (can be string, string[], or undefined)\n * @param linkPath - The file path to add (without .md extension, e.g., \"folder/file\")\n * @returns New array with link added, or same array if link already exists\n *\n * @example\n * ```ts\n * addLinkToProperty(undefined, \"MyNote\") // [\"[[MyNote]]\"]\n * addLinkToProperty(\"[[Note1]]\", \"Note2\") // [\"[[Note1]]\", \"[[Note2]]\"]\n * addLinkToProperty([\"[[Note1]]\"], \"Note2\") // [\"[[Note1]]\", \"[[Note2]]\"]\n * addLinkToProperty([\"[[Note1]]\"], \"Note1\") // [\"[[Note1]]\"] (no change - duplicate prevented)\n * addLinkToProperty([\"[[Folder/Note]]\"], \"folder/note\") // [\"[[Folder/Note]]\", \"[[folder/note|note]]\"] (case-sensitive, different entry)\n * ```\n */\nexport function addLinkToProperty(\n\tcurrentValue: string | string[] | undefined,\n\tlinkPath: string\n): string[] {\n\t// Handle undefined or null\n\tif (currentValue === undefined || currentValue === null) {\n\t\treturn [formatWikiLink(linkPath)];\n\t}\n\n\t// Normalize to array\n\tconst currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];\n\n\tconst existingPaths = parsePropertyLinks(currentArray);\n\n\t// Normalize paths for comparison to prevent duplicates with different casing or separators\n\tconst normalizedLinkPath = normalizePath(linkPath);\n\n\tconst normalizedExistingPaths = existingPaths.map((p) => normalizePath(p));\n\n\t// Only add if not already present (using normalized path comparison)\n\tif (!normalizedExistingPaths.includes(normalizedLinkPath)) {\n\t\treturn [...currentArray, formatWikiLink(linkPath)];\n\t}\n\n\treturn currentArray;\n}\n\n/**\n * Removes a link from a property using normalized path comparison.\n *\n * @param currentValue - The current property value (can be string, string[], or undefined)\n * @param linkPath - The file path to remove (without .md extension)\n * @returns New array with link removed (can be empty)\n *\n * @example\n * ```ts\n * removeLinkFromProperty([\"[[Note1]]\", \"[[Note2]]\"], \"Note1\") // [\"[[Note2]]\"]\n * removeLinkFromProperty([\"[[Note1]]\"], \"Note1\") // []\n * removeLinkFromProperty(\"[[Note1]]\", \"Note1\") // []\n * removeLinkFromProperty(undefined, \"Note1\") // []\n * removeLinkFromProperty([\"[[Folder/Note]]\"], \"Folder/Note\") // [] (case-sensitive removal)\n * ```\n */\nexport function removeLinkFromProperty(\n\tcurrentValue: string | string[] | undefined,\n\tlinkPath: string\n): string[] {\n\tif (currentValue === undefined || currentValue === null) {\n\t\treturn [];\n\t}\n\n\t// Normalize to array\n\tconst currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];\n\n\tconst normalizedLinkPath = normalizePath(linkPath);\n\n\treturn currentArray.filter((item) => {\n\t\tconst parsed = parsePropertyLinks([item])[0];\n\n\t\tif (!parsed) return true; // Keep invalid entries\n\n\t\treturn normalizePath(parsed) !== normalizedLinkPath;\n\t});\n}\n\n/**\n * Checks if a link exists in a property using normalized path comparison.\n *\n * @param currentValue - The current property value (can be string, string[], or undefined)\n * @param linkPath - The file path to check (without .md extension)\n * @returns True if the link exists\n *\n * @example\n * ```ts\n * hasLinkInProperty([\"[[Note1]]\", \"[[Note2]]\"], \"Note1\") // true\n * hasLinkInProperty(\"[[Note1]]\", \"Note1\") // true\n * hasLinkInProperty([], \"Note1\") // false\n * hasLinkInProperty(undefined, \"Note1\") // false\n * hasLinkInProperty([\"[[Folder/Note]]\"], \"Folder/Note\") // true (case-sensitive match)\n * ```\n */\nexport function hasLinkInProperty(\n\tcurrentValue: string | string[] | undefined,\n\tlinkPath: string\n): boolean {\n\tconst existingPaths = parsePropertyLinks(currentValue);\n\n\tconst normalizedLinkPath = normalizePath(linkPath);\n\n\treturn existingPaths.some((path) => normalizePath(path) === normalizedLinkPath);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real1ty-obsidian-plugins/utils",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "Shared utilities for Obsidian plugins",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -44,6 +44,7 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/luxon": "^3.7.1",
47
+ "fast-check": "^4.3.0",
47
48
  "typescript": "5.9.2",
48
49
  "vitest": "^2.0.5"
49
50
  },
@@ -1,5 +1,3 @@
1
- import type { BehaviorSubject } from "rxjs";
2
-
3
1
  import { BaseEvaluator, type BaseRule } from "./base";
4
2
 
5
3
  export interface FilterRule extends BaseRule {}