-
-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathTemplatePropertyCollector.ts
More file actions
165 lines (132 loc) · 5.07 KB
/
Copy pathTemplatePropertyCollector.ts
File metadata and controls
165 lines (132 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { App } from "obsidian";
import { findYamlFrontMatterRange, getYamlContextForMatch } from "./yamlContext";
import {
parseStructuredPropertyValueFromString,
type ParseOptions,
} from "./templatePropertyStringParser";
import { isStructuredYamlValue } from "./yamlValues";
const PATH_SEPARATOR = "\u0000";
type CollectArgs = {
input: string;
matchStart: number;
matchEnd: number;
rawValue: unknown;
fallbackKey: string;
featureEnabled: boolean;
};
export class TemplatePropertyCollector {
private map = new Map<string, unknown>();
private propertyTypeCache = new Map<string, string | null>();
constructor(private readonly app?: App) {}
/**
* Collects a variable for YAML post-processing when it is a complete value for a YAML key
* and the raw value is a structured type (object/array/number/boolean/null).
*/
public maybeCollect(args: CollectArgs): unknown | undefined {
const { input, matchStart, matchEnd, rawValue, fallbackKey, featureEnabled } = args;
if (!featureEnabled) return undefined;
const yamlRange = findYamlFrontMatterRange(input);
const context = getYamlContextForMatch(input, matchStart, matchEnd, yamlRange);
if (!context.isInYaml) return undefined;
const lineContent = input.slice(context.lineStart, context.lineEnd);
const trimmedLine = lineContent.trim();
const propertyKeyMatch = lineContent.match(/^\s*([^:]+):/);
const fallbackPathKey = propertyKeyMatch ? propertyKeyMatch[1].trim() : fallbackKey;
const listItemPattern = /^-\s*['"]?\{\{VALUE:[^}]+\}\}['"]?\s*$/i;
let propertyPath: string[] | null = null;
if (context.isKeyValuePosition) {
propertyPath = [fallbackPathKey];
} else if (listItemPattern.test(trimmedLine)) {
propertyPath = this.findListParentPath(input, context.lineStart, context.baseIndent ?? "");
}
if (!propertyPath || propertyPath.length === 0) return undefined;
const effectiveKey = propertyPath[propertyPath.length - 1];
let structuredValue = rawValue;
if (typeof rawValue === "string") {
const parsed = parseStructuredPropertyValueFromString(rawValue, this.buildParseOptions(effectiveKey));
if (parsed !== undefined) {
structuredValue = parsed;
}
}
if (!isStructuredYamlValue(structuredValue)) return undefined;
const mapKey = propertyPath.join(PATH_SEPARATOR);
this.map.set(mapKey, structuredValue);
return structuredValue;
}
/** Returns a copy and clears the collector. */
public drain(): Map<string, unknown> {
const result = new Map(this.map);
this.map.clear();
return result;
}
private buildParseOptions(propertyKey: string): ParseOptions {
return {
propertyKey,
propertyType: this.resolvePropertyType(propertyKey),
app: this.app,
};
}
private findListParentPath(input: string, currentLineStart: number, currentIndent: string): string[] | null {
let endIndex = currentLineStart - 1;
const path: string[] = [];
let targetIndent = currentIndent.length;
while (endIndex >= 0) {
const lineBreak = input.lastIndexOf("\n", endIndex);
const lineStart = lineBreak === -1 ? 0 : lineBreak + 1;
const line = input.slice(lineStart, endIndex + 1);
const trimmed = line.trim();
if (trimmed.length === 0) {
endIndex = lineStart - 2;
continue;
}
if (trimmed === "---" || trimmed === "...") {
break;
}
if (trimmed.startsWith("-")) {
endIndex = lineStart - 2;
continue;
}
const keyMatch = line.match(/^(\s*)([^:\n]+):/);
if (keyMatch) {
const indent = keyMatch[1] ?? "";
const indentLength = indent.length;
if (indentLength < targetIndent) {
path.unshift(keyMatch[2].trim());
targetIndent = indentLength;
if (targetIndent === 0) {
break;
}
}
}
endIndex = lineStart - 2;
}
return path.length > 0 ? path : null;
}
private resolvePropertyType(propertyKey: string): string | null {
if (!this.app) return null;
if (this.propertyTypeCache.has(propertyKey)) {
return this.propertyTypeCache.get(propertyKey) ?? null;
}
const appAny = this.app as unknown as {
metadataTypeManager?: { getTypeInfo?: (key: string) => unknown };
metadataCache?: { app?: { metadataTypeManager?: { getTypeInfo?: (key: string) => unknown } } };
};
const manager =
appAny.metadataTypeManager ?? appAny.metadataCache?.app?.metadataTypeManager;
if (!manager || typeof manager.getTypeInfo !== "function") {
this.propertyTypeCache.set(propertyKey, null);
return null;
}
const info = manager.getTypeInfo(propertyKey) as
| {
expected?: { type?: string } | null;
inferred?: { type?: string } | null;
}
| undefined;
const type = info?.expected?.type ?? info?.inferred?.type ?? null;
const normalized = typeof type === "string" ? type : null;
this.propertyTypeCache.set(propertyKey, normalized);
return normalized;
}
public static readonly PATH_SEPARATOR = PATH_SEPARATOR;
}