Skip to content

Commit

Permalink
Merge pull request #36 from massivebird/group-by-tag
Browse files Browse the repository at this point in the history
feat: group nodes by tag
  • Loading branch information
AlexW00 authored Oct 21, 2023
2 parents 6c82086 + 3776097 commit 377a9f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/graph/Node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Link from "./Link";
import { TFile } from "obsidian";
import { TFile, getAllTags } from "obsidian";

export default class Node {
public readonly id: string;
Expand All @@ -9,20 +9,23 @@ export default class Node {

public readonly neighbors: Node[];
public readonly links: Link[];
public readonly tags: string[];

constructor(
name: string,
path: string,
val = 10,
neighbors: Node[] = [],
links: Link[] = []
links: Link[] = [],
tags: string[] = []
) {
this.id = path;
this.name = name;
this.path = path;
this.val = val;
this.neighbors = neighbors;
this.links = links;
this.tags = tags;
}

// Creates an array of nodes from an array of files (from the Obsidian API)
Expand All @@ -32,6 +35,12 @@ export default class Node {
files
.map((file, index) => {
const node = new Node(file.name, file.path);
const cache = app.metadataCache.getFileCache(file),
tags = cache ? getAllTags(cache) : null;
if (tags != null) {
// stores tags without leading octothorpe `#` as ["tag1", "tag2", ...]
tags.forEach((tag) => node.tags.push(tag.substring(1)));
}
if (!nodeMap.has(node.id)) {
nodeMap.set(node.id, index);
return node;
Expand Down
6 changes: 5 additions & 1 deletion src/settings/categories/GroupSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export class NodeGroup {
}

static matches(query: string, node: Node): boolean {
return node.path.startsWith(this.sanitizeQuery(query));
// queries tags if query begins with "tag:" or "tag:#"
if (query.match(/^tag:#?/)) {
return node.tags.includes(query.replace(/^tag:#?/, ""))
}
return node.path.startsWith(this.sanitizeQuery(query))
}

static sanitizeQuery(query: string): string {
Expand Down

0 comments on commit 377a9f9

Please sign in to comment.