Skip to content

Commit

Permalink
perf(command:freeze-crumbs): Only processFrontmatter if it was actual…
Browse files Browse the repository at this point in the history
…ly mutated
  • Loading branch information
SkepticMystic committed May 3, 2024
1 parent f353688 commit 270d67c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export const init_all_commands = (plugin: BreadcrumbsPlugin) => {
),
);

log.debug(
`freeze-implied-edges-to-vault > took ${timer.elapsed_str()}ms`,
);

notice.setMessage(
`Implied edges frozen to all notes in ${timer.elapsed_str()}ms`,
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/side_views/TreeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
: [];
</script>

<div class="markdown-rendered BC-tree-view -mt-2">
<div class="markdown-rendered BC-tree-view">
<div class="nav-header">
<div class="nav-buttons-container">
<RebuildGraphButton
Expand Down
74 changes: 48 additions & 26 deletions src/utils/drop_crumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,60 @@ export const drop_crumbs = async (

switch (options.destination) {
case "frontmatter": {
await plugin.app.fileManager.processFrontMatter(
destination_file,
(frontmatter) => {
Object.keys(links_by_field).forEach((field) => {
const links = links_by_field[field]!;

const existing = frontmatter[field];
if (existing) {
frontmatter[field] = remove_duplicates(
ensure_is_array(existing).concat(links),
);
} else {
frontmatter[field] = links;
}
});

log.debug(
"drop_crumbs > processed frontmatter",
frontmatter,
let mutated = false;

const frontmatter =
plugin.app.metadataCache.getFileCache(destination_file)
?.frontmatter ?? {};

Object.entries(links_by_field).forEach(([field, links]) => {
if (!links?.length) return;

const existing = frontmatter[field];
if (existing) {
const existing_array = ensure_is_array(existing);
const new_links = remove_duplicates(
existing_array.concat(links),
);
},
);

if (new_links.length !== existing_array.length) {
mutated = true;
frontmatter[field] = new_links;
}
} else {
mutated = true;
frontmatter[field] = links;
}
});

if (mutated) {
await plugin.app.fileManager.processFrontMatter(
destination_file,
(old_frontmatter) => {
const new_frontmatter = Object.assign(
old_frontmatter,
frontmatter,
);

log.debug(
"drop_crumbs > processed frontmatter",
new_frontmatter,
);
},
);
}

break;
}

case "dataview-inline": {
const dataview_fields = Object.keys(links_by_field).map((field) => {
const links = links_by_field[field]!;

return `${field}:: ${links.join(", ")}`;
});
// TODO: Dedupe links using dv page API
const dataview_fields = Object.entries(links_by_field)
.map(([field, links]) => {
if (!links?.length) return "";
else return `${field}:: ${links.join(", ")}`;
})
.filter(Boolean);

// NOTE: Just appends for now
await plugin.app.vault.process(destination_file, (content) => {
Expand Down

0 comments on commit 270d67c

Please sign in to comment.