-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathvalidator.ts
192 lines (186 loc) · 5.56 KB
/
validator.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { Admonition, AdmonitionIconDefinition } from "src/@types";
import { t } from "src/lang/helpers";
import ObsidianAdmonition from "src/main";
type ValidationSuccess = {
success: true;
messages?: string[];
};
type ValidationError = {
success: false;
failed: "type" | "icon" | "rgb" | "title" | "booleans";
message: string;
};
type Result = ValidationSuccess | ValidationError;
export const isSelectorValid = ((dummyElement) => (selector: string) => {
try {
dummyElement.querySelector(selector);
} catch {
return false;
}
return true;
})(document.createDocumentFragment());
export class AdmonitionValidator {
static validateImport(
plugin: ObsidianAdmonition,
admonition: Admonition
): Result {
const result: Result = {
success: true,
messages: []
};
const validType = AdmonitionValidator.validateType(
admonition.type,
plugin
);
if (validType.success == false) {
return validType;
}
const iconName =
typeof admonition.icon == "string"
? admonition.icon
: typeof admonition.icon == "object"
? admonition.icon?.name
: null;
const validIcon = AdmonitionValidator.validateType(iconName, plugin);
if (validIcon.success == false) {
return validIcon;
}
const iconNode = plugin.iconManager.getIconNode(admonition.icon);
if (!iconNode) {
result.messages.push(
"No installed icon found by the name " +
iconName +
". Perhaps you need to install a new icon pack?"
);
}
if (admonition.title && typeof admonition.title != "string") {
return {
success: false,
failed: "title",
message: "Admonition titles can only be strings."
};
}
if (
!("color" in admonition) ||
!/(?:(?:2(?:[0-4]\d|5[0-5])|\d{1,2}|1\d\d)\s*,\s*){2}\s*(?:2(?:[0-4]\d|5[0-5])|\d{1,2}|1\d\d)/.test(
admonition.color
)
) {
console.warn(
"No color provided for the import of " +
admonition.type +
". Adding a random color."
);
admonition.color = `${Math.floor(
Math.random() * 255
)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(
Math.random() * 255
)}`;
}
const booleans: (keyof Admonition)[] = [
"command",
"injectColor",
"noTitle",
"copy"
];
for (const key of booleans) {
if (
key in admonition &&
typeof JSON.parse(JSON.stringify(admonition[key])) != "boolean"
) {
return {
success: false,
failed: "booleans",
message: `The "${key}" property must be a boolean if present.`
};
}
}
return result;
}
static validate(
plugin: ObsidianAdmonition,
type: string,
icon: AdmonitionIconDefinition,
oldType?: string
): Result {
const validType = AdmonitionValidator.validateType(
type,
plugin,
oldType
);
if (validType.success == false) {
return validType;
}
return AdmonitionValidator.validateIcon(icon, plugin);
}
static validateType(
type: string,
plugin: ObsidianAdmonition,
oldType?: string
): Result {
if (!type.length) {
return {
success: false,
message: t("Admonition type cannot be empty."),
failed: "type"
};
}
if (type.includes(" ")) {
return {
success: false,
message: t("Admonition type cannot include spaces."),
failed: "type"
};
}
if (!isSelectorValid(type)) {
return {
success: false,
message: t("Types must be a valid CSS selector."),
failed: "type"
};
}
if (/[A-Z]/.test(type)) {
return {
success: false,
message: t("Admonition type cannot contain uppercase letters."),
failed: "type"
};
}
if (type != oldType && type in plugin.data.userAdmonitions) {
return {
success: false,
message: "That Admonition type already exists.",
failed: "type"
};
}
return { success: true };
}
static validateIcon(
definition: AdmonitionIconDefinition,
plugin: ObsidianAdmonition
): Result {
if (definition.type === "image") {
return {
success: true
};
}
if (!definition.name?.length) {
return {
success: false,
message: t("Icon cannot be empty."),
failed: "icon"
};
}
const icon = plugin.iconManager.getIconType(definition.name);
if (!icon) {
return {
success: false,
message: t("Invalid icon name."),
failed: "icon"
};
}
return {
success: true
};
}
}