1
0
2026-01-10 14:30:32 -06:00

114 lines
3.3 KiB
JavaScript

async function updateMetadata(tp, options) {
console.log("options", options);
const file = tp.config.target_file;
const data = options?.data ?? {};
const tags = new Set([options?.tags ?? []].flat());
const tagsToRemove = new Set([options?.tagsToRemove ?? []].flat());
const title = options?.title;
for (const tag of tags) {
const splitTag = tag.split("/");
for (let i = 1; i < splitTag.length; i++) {
tagsToRemove.add(splitTag.slice(0, i).join("/"));
}
}
if (tp.file.title === "Untitled" && title) {
await tp.file.rename(title);
}
tp.hooks.on_all_templates_executed(async () => {
await tp.app.fileManager.processFrontMatter(file, (frontmatter) => {
console.log("frontmatter before", frontmatter);
let fmTags = new Set([frontmatter.tags ?? []].flat());
fmTags = tags.difference(tagsToRemove).union(tags);
frontmatter.tags = Array.from(fmTags);
frontmatter.tags.sort();
for (const key in data) {
frontmatter[key] = data[key];
}
console.log("frontmatter before", frontmatter);
});
});
}
function getFileProperty(file, property) {
const frontmatter = app.metadataCache.getFileCache(file)?.frontmatter;
return frontmatter?.[property];
}
function hasPropertyValue(file, property, value) {
const propValue = getFileProperty(file, property);
if (Array.isArray(propValue)) {
return propValue.includes(value);
}
return propValue === value;
}
function frontmatterLinksToTFiles(sourceFile, property) {
const links = app.metadataCache.getFileCache(sourceFile)?.frontmatterLinks;
if (links) {
return app.metadataCache
.getFileCache(sourceFile)
?.frontmatterLinks?.filter((l) => l.key.startsWith(property))
?.map((l) =>
app.metadataCache.getFirstLinkpathDest(l.link, sourceFile.path),
)
?.filter((f) => f);
} else {
return [];
}
}
function slugify(input) {
if (!input) return "";
// Make lower case and trim
var slug = input.toLowerCase().trim();
// Remove accents from characters
slug = slug.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
// Replace invalid chars with spaces
slug = slug.replace(/[^a-z0-9\s-]/g, " ").trim();
// Replace multiple spaces or hyphens with a single hyphen
slug = slug.replace(/[\s-]+/g, "-");
return slug;
}
function getAllTags(file) {
const cache = app.metadataCache.getFileCache(file);
const fileTags =
cache?.tags?.map((t) => (t.tag[0] === "#" ? t.tag.slice(1) : t.tag)) || [];
const fmTags = [cache?.frontmatter?.tags || []].flat();
return fmTags.concat(fileTags);
}
function filesWithTag(tag) {
console.log("files with tag", tag);
const files = app.vault.getMarkdownFiles().filter((file) => {
const tags = getAllTags(file);
return tags?.includes(tag) || tags?.some((t) => t.startsWith(`${tag}/`));
});
return files;
}
function fileHasTag(file, tag) {
const cache = app.metadataCache.getFileCache(file);
const allTags = (cache?.frontmatter?.tags ?? []).concat(
cache?.tags?.map((t) => (t.tag[0] === "#" ? t.tag.slice(1) : t.tag)),
);
return allTags.some((t) => t === tag || t?.startsWith(`${tag}/`));
}
module.exports = {
slugify,
getAllTags,
filesWithTag,
fileHasTag,
frontmatterLinksToTFiles,
getFileProperty,
hasPropertyValue,
updateMetadata,
};