Skip to content

Commit

Permalink
feat: highlight path
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisseidel committed Apr 24, 2021
1 parent a250c90 commit 8dbd0fb
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 14 deletions.
5 changes: 3 additions & 2 deletions README.md
@@ -1,8 +1,9 @@
# highlight public notes

This plugin for [Obsidian](https://obsidian.md/) highlights the titlebar of notes that you classify as public in the frontmatter. This prevents you from writing confidential data into notes you later publish e.g. through a script.
This plugin for [Obsidian](https://obsidian.md/) highlights the titlebar of notes that you **classify as public in the frontmatter** or that is **contained in a specific folder**. This prevents you from writing confidential data into notes you later publish e.g. through a script.

![screenshot-full](images/example-highlightpublicnotes.png)

![screenshot-full](https://raw.githubusercontent.com/dennisseidel/highlightpublicnotes-obsidian-plugin/master/images/example-highlightpublicnotes.png)

## Usage
After enabling the plugin in the settings menu, all notes with a frontmatter with an attribute `classification: public` highlight the titlebar red.
Expand Down
2 changes: 1 addition & 1 deletion data.json
@@ -1 +1 @@
{"fronmatterAttribute":"classification","valueToHighlight":"public","mySetting":"classifcation"}
{"useFrontmatterHighlight":false,"usePathHighlight":true,"fronmatterAttribute":"classification","valueToHighlight":"public","pathToHighlight":"03_ARTICLES","mySetting":"classifcation"}
110 changes: 99 additions & 11 deletions main.ts
@@ -1,13 +1,19 @@
import { App, Plugin, PluginSettingTab, Setting, CachedMetadata } from 'obsidian';
import { App, Plugin, PluginSettingTab, Setting, CachedMetadata, TFile } from 'obsidian';

interface PluginSettings {
fronmatterAttribute: string;
useFrontmatterHighlight: boolean,
usePathHighlight: boolean,
fronmatterAttribute: string,
valueToHighlight: string,
pathToHighlight: string,
}

const DEFAULT_SETTINGS: PluginSettings = {
useFrontmatterHighlight: true,
usePathHighlight: false,
fronmatterAttribute: 'classification',
valueToHighlight: 'public'
valueToHighlight: 'public',
pathToHighlight: '',
}

export default class HighlightpublicnotesPlugin extends Plugin {
Expand All @@ -20,16 +26,38 @@ export default class HighlightpublicnotesPlugin extends Plugin {
this.addSettingTab(new SettingTab(this.app, this));
}

async onFileOpen(file: any) {
async onFileOpen(file: TFile) {
if (!file || file.extension !== 'md')
return;
const classifcation = this.app.metadataCache.getFileCache(file)?.frontmatter?.[this.settings.fronmatterAttribute]

if(this.settings.useFrontmatterHighlight) {
const classifcation = this.app.metadataCache.getFileCache(file)?.frontmatter?.[this.settings.fronmatterAttribute]
if (classifcation == this.settings.valueToHighlight) {
this.highlightNote()
} else {
this.unhighlightNote()
}
} else if(this.settings.usePathHighlight) {
if (this.checkPath(file.path, this.settings.pathToHighlight)) {
this.highlightNote()
} else {
this.unhighlightNote()
}
}
}

private highlightNote() {
const titlebar = document.getElementsByClassName("titlebar")[0]
if (classifcation == this.settings.valueToHighlight) {
titlebar.classList.add("myalert");
} else {
titlebar.classList.add("myalert")
}

private unhighlightNote() {
const titlebar = document.getElementsByClassName("titlebar")[0]
titlebar.classList.remove("myalert")
}
}

private checkPath(currentPath: string, blacklistedPath: string): boolean {
return currentPath.includes(blacklistedPath)
}

async loadSettings() {
Expand All @@ -51,10 +79,68 @@ class SettingTab extends PluginSettingTab {

display(): void {
let {containerEl} = this;

containerEl.empty()

new Setting(containerEl)
.setName('check frontmatter')
.setDesc('use frontmatter highlighting')
.addToggle(toogle => {
toogle
.setValue(this.plugin.settings.useFrontmatterHighlight)
.onChange(async _ => {
this.plugin.settings.useFrontmatterHighlight = !this.plugin.settings.useFrontmatterHighlight
await this.plugin.saveSettings()
this.display()
})
})

new Setting(containerEl)
.setName('check path')
.setDesc('use path highlighting')
.addToggle(toogle => {
toogle
.setValue(this.plugin.settings.usePathHighlight)
.onChange(async _ => {
this.plugin.settings.usePathHighlight = !this.plugin.settings.usePathHighlight
await this.plugin.saveSettings()
this.display()
})
})

if (this.plugin.settings.useFrontmatterHighlight) {
this.addFrontMatterSettings(containerEl)
}
if (this.plugin.settings.usePathHighlight) {
this.addPathHighlightSettings(containerEl)
}



}


addPathHighlightSettings(container: HTMLElement): void {
container.createEl('h3', {
text: "Path Highlight Settings"
})
new Setting(container)
.setName('Path')
.setDesc('a path to highlight')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.pathToHighlight)
.onChange(async (value) => {
this.plugin.settings.pathToHighlight = value
await this.plugin.saveSettings()
}))
}

addFrontMatterSettings(container: HTMLElement): void {
container.createEl('h3', {
text: "Frontmatter Settings"
})
new Setting(container)
.setName('Attribute')
.setDesc('the attribute in the frontmatter that indicates the visiblity')
.addText(text => text
Expand All @@ -64,7 +150,8 @@ class SettingTab extends PluginSettingTab {
this.plugin.settings.fronmatterAttribute = value
await this.plugin.saveSettings()
}))
new Setting(containerEl)

new Setting(container)
.setName('Value')
.setDesc('the value that indicates public visibility')
.addText(text => text
Expand All @@ -74,5 +161,6 @@ class SettingTab extends PluginSettingTab {
this.plugin.settings.valueToHighlight = value
await this.plugin.saveSettings()
}))
}
}
}

0 comments on commit 8dbd0fb

Please sign in to comment.