Skip to content

Commit

Permalink
fix: tag capture also looks in frontmatter tags
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Apr 5, 2023
1 parent 2b7d4c4 commit 28919cf
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/utilityObsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,40 @@ export function getMarkdownFilesWithTag(tag: string): TFile[] {
): fileCache is CachedMetadata & { tags: TagCache[] } =>
fileCache.tags !== undefined && Array.isArray(fileCache.tags);

const hasFrontmatterTags = (
fileCache: CachedMetadata
): fileCache is CachedMetadata & { frontmatter: { tags: string } } => {
return (
fileCache.frontmatter !== undefined &&
fileCache.frontmatter.tags !== undefined &&
typeof fileCache.frontmatter.tags === "string" &&
fileCache.frontmatter.tags.length > 0
);
};

return app.vault.getMarkdownFiles().filter((f) => {
const fileCache = app.metadataCache.getFileCache(f);
if (!fileCache) return false;

if (hasTags(fileCache)) {
const tagInTags = fileCache.tags.find((item) => item.tag === tag);

if (!fileCache || !hasTags(fileCache)) return false;
if (tagInTags) {
return true;
}
}

if (hasFrontmatterTags(fileCache)) {
const tagWithoutHash = tag.replace(/^\#/, "");
const tagInFrontmatterTags = fileCache.frontmatter.tags
.split(" ")
.find((item) => item === tagWithoutHash);

if (tagInFrontmatterTags) {
return true;
}
}

return fileCache.tags.find((item) => item.tag === tag);
return false;
});
}

1 comment on commit 28919cf

@vercel
Copy link

@vercel vercel bot commented on 28919cf Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quickadd – ./

quickadd-chrisbbh.vercel.app
quickadd.obsidian.guide
quickadd-git-master-chrisbbh.vercel.app

Please sign in to comment.