Skip to content

Commit

Permalink
✨ ignored paths pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0q committed Sep 9, 2024
1 parent e8e7147 commit d0c33e8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"@electron/remote": "2.1.2",
"ignore": "^5.3.2",
"obsidian": "^1.6.6"
}
}
17 changes: 16 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { BrowserWindow } from "@electron/remote"; // FIXME: import only if Platform.isDesktopApp is true
import ignore from "ignore";
import { MarkdownView, Platform, Plugin, WorkspaceRoot } from "obsidian";
import { DEFAULT_SETTINGS, SettingTab, type Settings } from "./settings";

export default class GraphBannerPlugin extends Plugin {
static graphBannerNodeClass = "graph-banner-content";

settings: Settings;

unloadListeners: (() => void)[] = [];
graphNode: Element | null = null;
graphWindowID: number | null = null;

async onload() {
console.log("Loading GraphBannerPlugin");
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));

// NOTE: https://github.com/mgmeyers/obsidian-style-settings?tab=readme-ov-file#plugin-support
this.app.workspace.trigger("parse-style-settings");
Expand Down Expand Up @@ -68,6 +73,12 @@ export default class GraphBannerPlugin extends Plugin {
this.app.workspace.on("file-open", async (file) => {
if (!file || file.extension !== "md") return;

const isIgnoredPath = ignore()
.add(this.settings.ignore)
.ignores(file.path);
this.graphNode?.toggleClass("hidden", isIgnoredPath);
if (isIgnoredPath) return;

const fileView = await this.tryUntilNonNull(() =>
this.app.workspace.getActiveViewOfType(MarkdownView),
);
Expand Down Expand Up @@ -160,6 +171,10 @@ export default class GraphBannerPlugin extends Plugin {
}
}

private async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

private async tryUntilNonNull<T>(f: () => T, interval = 200, maxCount = 10) {
for (let i = 0; i < maxCount; i++) {
const result = f();
Expand Down
42 changes: 42 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { type App, PluginSettingTab, Setting } from "obsidian";
import type GraphBannerPlugin from "./main";

export interface Settings {
ignore: string[];
}

export const DEFAULT_SETTINGS: Settings = {
ignore: [],
};

export class SettingTab extends PluginSettingTab {
plugin: GraphBannerPlugin;

constructor(app: App, plugin: GraphBannerPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display() {
const { containerEl } = this;

containerEl.empty();

new Setting(containerEl)
.setName("Ignored Path Pattern")
.setDesc(
"Manage notes which do not display the graph banner. This pattern follows .gitignore spec.",
)
.addTextArea((textArea) =>
textArea
.setPlaceholder(
"ignored-path.md\n/ignored-dir\n!/ignored-dir/not-ignored-path.md",
)
.setValue(this.plugin.settings.ignore.join("\n"))
.onChange(async (value) => {
this.plugin.settings.ignore = value.split("\n");
await this.plugin.saveData(this.plugin.settings);
}),
);
}
}
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ settings:
height: var(--banner-height) !important;
margin-bottom: 16px;
}

.graph-banner-content.hidden {
display: none;
}

0 comments on commit d0c33e8

Please sign in to comment.