Skip to content

Commit 15ae98d

Browse files
AndyEverittZachatoo
authored andcommitted
feat: remove console.log and change merging frontmatter to be async for speed.
I don't think a race condition is an issue since there should only be 1 instance of each key in the properties being merged and each promise only accesses a single key
1 parent ecf68a7 commit 15ae98d

File tree

1 file changed

+74
-66
lines changed

1 file changed

+74
-66
lines changed

src/utils/Utils.ts

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -246,88 +246,96 @@ export async function merge_front_matter(
246246
file: TFile | null,
247247
properties: Record<string, unknown>
248248
): Promise<void> {
249-
if (!file) {
249+
if (!file || !is_object(properties)) {
250250
return;
251251
}
252252
try {
253-
for (const prop of Object.keys(properties)) {
254-
const value = properties[prop];
255-
if (app.metadataCache.getFileCache(file)?.frontmatter == null) {
256-
console.log("Frontmatter is empty");
257-
await app.fileManager.processFrontMatter(
258-
file,
259-
(frontmatter) => {
260-
console.log(
261-
`adding new property: ${prop} to ${file.basename} with value: ${value}`
262-
);
263-
frontmatter[prop] = value;
264-
}
265-
);
266-
} else if (
267-
app.metadataCache
268-
.getFileCache(file)
269-
?.frontmatter?.hasOwnProperty(prop)
270-
) {
271-
console.log(`${file.basename} contains property: ${prop}`);
272-
const originalValue =
273-
app.metadataCache.getFileCache(file)?.frontmatter?.[prop];
274-
if (
275-
value != null &&
276-
(Array.isArray(originalValue) || Array.isArray(value))
277-
) {
278-
console.log(`${prop} is an array`);
253+
await Promise.all(
254+
Object.keys(properties).map(async (prop) => {
255+
const value = properties[prop];
256+
if (app.metadataCache.getFileCache(file)?.frontmatter == null) {
257+
// console.log("Frontmatter is empty");
279258
await app.fileManager.processFrontMatter(
280259
file,
281260
(frontmatter) => {
282-
if (!Array.isArray(originalValue)) {
283-
console.log(
284-
`converting ${prop} to an array in ${file.basename}`
285-
);
286-
frontmatter[prop] = [originalValue];
287-
}
288-
if (Array.isArray(value)) {
289-
for (let i = 0; i < value.length; i++) {
290-
console.log(
291-
`adding ${value[i]} to ${prop} in ${file.basename}`
292-
);
293-
if (!frontmatter[prop].includes(value[i])) {
294-
frontmatter[prop].push(value[i]);
295-
}
261+
// console.log(
262+
// `adding new property: ${prop} to ${file.basename} with value: ${value}`
263+
// );
264+
frontmatter[prop] = value;
265+
}
266+
);
267+
} else if (
268+
app.metadataCache
269+
.getFileCache(file)
270+
?.frontmatter?.hasOwnProperty(prop)
271+
) {
272+
// console.log(`${file.basename} contains property: ${prop}`);
273+
const originalValue =
274+
app.metadataCache.getFileCache(file)?.frontmatter?.[
275+
prop
276+
];
277+
if (
278+
value != null &&
279+
(Array.isArray(originalValue) || Array.isArray(value))
280+
) {
281+
// console.log(`${prop} is an array`);
282+
await app.fileManager.processFrontMatter(
283+
file,
284+
(frontmatter) => {
285+
if (!Array.isArray(originalValue)) {
286+
// console.log(
287+
// `converting ${prop} to an array in ${file.basename}`
288+
// );
289+
frontmatter[prop] = [originalValue];
296290
}
297-
} else {
298-
console.log(
299-
`adding ${value} to ${prop} in ${file.basename}`
300-
);
301-
if (!frontmatter[prop].includes(value)) {
302-
frontmatter[prop].push(value);
291+
if (Array.isArray(value)) {
292+
for (let i = 0; i < value.length; i++) {
293+
// console.log(
294+
// `adding ${value[i]} to ${prop} in ${file.basename}`
295+
// );
296+
if (
297+
!frontmatter[prop].includes(
298+
value[i]
299+
)
300+
) {
301+
frontmatter[prop].push(value[i]);
302+
}
303+
}
304+
} else {
305+
// console.log(
306+
// `adding ${value} to ${prop} in ${file.basename}`
307+
// );
308+
if (!frontmatter[prop].includes(value)) {
309+
frontmatter[prop].push(value);
310+
}
303311
}
304312
}
305-
}
306-
);
307-
} else if (originalValue !== value && value != null) {
308-
console.log(
309-
`updating property: ${prop} in ${file.basename} from ${originalValue} to ${value}`
310-
);
313+
);
314+
} else if (originalValue !== value && value != null) {
315+
// console.log(
316+
// `updating property: ${prop} in ${file.basename} from ${originalValue} to ${value}`
317+
// );
318+
await app.fileManager.processFrontMatter(
319+
file,
320+
(frontmatter) => {
321+
frontmatter[prop] = value;
322+
}
323+
);
324+
}
325+
} else {
326+
// console.log(`${file.basename} doesn't contain ${prop}`);
327+
// console.log(
328+
// `adding property: ${prop} to ${file.basename} with value: ${value}`
329+
// );
311330
await app.fileManager.processFrontMatter(
312331
file,
313332
(frontmatter) => {
314333
frontmatter[prop] = value;
315334
}
316335
);
317336
}
318-
} else {
319-
console.log(`${file.basename} doesn't contain ${prop}`);
320-
console.log(
321-
`adding property: ${prop} to ${file.basename} with value: ${value}`
322-
);
323-
await app.fileManager.processFrontMatter(
324-
file,
325-
(frontmatter) => {
326-
frontmatter[prop] = value;
327-
}
328-
);
329-
}
330-
}
337+
})
338+
);
331339
} catch (error) {
332340
console.error("Error in processing frontmatter: ", error);
333341
}

0 commit comments

Comments
 (0)