### Auto-Property Rule Interface and Examples (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt Defines the AutoPropRule interface for configuring how content is matched and extracted for frontmatter properties. Includes examples for extracting the first open task and counting tagged tasks. ```typescript interface AutoPropRule { key: string; // Property name in frontmatter enabled: boolean; // Whether rule is active rulePartOne: 'first' | 'all' | 'count'; // What to extract rulePartTwo: 'startsWith' | 'contains' | 'endsWith' | 'regex'; // Match type ruleValue: string; // Search string or regex pattern modifierWhitespace: 'trim' | 'noTrim'; // Handle indentation modifierOmitSearch: 'none' | 'omit'; // Remove search string from result modifierCaseSensitive: 'sensitive' | 'insensitive'; autoAdd: boolean; // Auto-add property to notes rule: 'built' | 'created' | 'modified' | 'characterCount'; // Rule type } // Example rule: Extract first open task const openTaskRule: AutoPropRule = { key: 'next-task', enabled: true, rulePartOne: 'first', rulePartTwo: 'startsWith', ruleValue: '- [ ]', modifierWhitespace: 'trim', modifierOmitSearch: 'omit', modifierCaseSensitive: 'insensitive', autoAdd: false, rule: 'built' }; // Example rule: Count all tasks with specific tag const countTaggedTasks: AutoPropRule = { key: 'urgent-count', enabled: true, rulePartOne: 'count', rulePartTwo: 'contains', ruleValue: '#urgent', modifierWhitespace: 'trim', modifierOmitSearch: 'none', modifierCaseSensitive: 'insensitive', autoAdd: false, rule: 'built' }; ``` -------------------------------- ### Define AutoPropertyPluginSettings Interface and Default Configuration (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt Defines the structure for plugin settings, including rule definitions, mode, notification preferences, and ignored paths. Also provides default settings and an example configuration. ```typescript interface AutoPropertyPluginSettings { autopropertySettings: AutoPropRule[]; mode: 'modify' | 'active-leaf-change' | 'manual'; showNotices: boolean; pathsToIgnore: string[]; } const DEFAULT_SETTINGS: AutoPropertyPluginSettings = { autopropertySettings: [], mode: 'modify', showNotices: true, pathsToIgnore: [] }; // Example complete configuration const exampleSettings: AutoPropertyPluginSettings = { autopropertySettings: [ { key: 'first-task', enabled: true, rulePartOne: 'first', rulePartTwo: 'startsWith', ruleValue: '- [ ]', modifierWhitespace: 'trim', modifierOmitSearch: 'omit', modifierCaseSensitive: 'insensitive', autoAdd: true, rule: 'built' }, { key: 'modified', enabled: true, rulePartOne: 'first', rulePartTwo: 'startsWith', ruleValue: '', modifierWhitespace: 'trim', modifierOmitSearch: 'none', modifierCaseSensitive: 'insensitive', autoAdd: false, rule: 'modified' } ], mode: 'active-leaf-change', showNotices: true, pathsToIgnore: ['templates/', 'archive/'] }; ``` -------------------------------- ### Evaluate Rules and Get Property Value (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt Evaluates a given rule against the content of a note to determine the appropriate value for a frontmatter property. It handles various rule types, including built-in date and character count rules, as well as line-based rules with modifiers like omitting search terms or trimming whitespace. It can return a single value, an array of values, or undefined. ```typescript static getValue( autoProp: AutoPropRule, bodyContent: string, file: TFile ): string | string[] | number | boolean | undefined { // Handle built-in rules if (autoProp.rule === 'modified') return getDateString(new Date(file.stat.mtime)); if (autoProp.rule === 'created') return getDateString(new Date(file.stat.ctime)); if (autoProp.rule === 'characterCount') return bodyContent.length; // Process line-based rules const lines = bodyContent.split(/\r\n|\r|\n/); let matches = lines.filter(line => AutoPropertyPlugin.lineMatchesRule(line, autoProp) ); if (autoProp.rulePartOne === 'count') return matches.length; if (matches.length === 0) return ''; // Apply modifiers if (autoProp.modifierOmitSearch === 'omit') { matches = matches.map(line => line.split(autoProp.ruleValue).join('')); } if (autoProp.modifierWhitespace === 'trim') { matches = matches.map(line => line.trim()); } // Return based on rule type if (autoProp.rulePartOne === 'first') return matches[0]; if (autoProp.rulePartOne === 'all') return matches; } // Example: Note with tasks const bodyContent = " # My Tasks - [ ] Buy groceries - [x] Send email - [ ] Call dentist "; // Rule to get first open task const result = AutoPropertyPlugin.getValue( { rulePartOne: 'first', rulePartTwo: 'startsWith', ruleValue: '- [ ]', modifierOmitSearch: 'omit', modifierWhitespace: 'trim', rule: 'built' }, bodyContent, file ); // Result: "Buy groceries" // Rule to get all open tasks const allTasks = AutoPropertyPlugin.getValue( { rulePartOne: 'all', rulePartTwo: 'startsWith', ruleValue: '- [ ]', modifierOmitSearch: 'omit', modifierWhitespace: 'trim', rule: 'built' }, bodyContent, file ); // Result: ["Buy groceries", "Call dentist"] ``` -------------------------------- ### Core Plugin Class Registration and Event Handling (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt The main AutoPropertyPlugin class registers commands for updating notes and sets up event listeners for file modifications. It extends Obsidian's Plugin class and manages the plugin's lifecycle and settings. ```typescript import { Plugin, MarkdownView, TFile } from 'obsidian'; export default class AutoPropertyPlugin extends Plugin { settings: AutoPropertyPluginSettings; lastRun: Date; private lastActiveFile: TFile | null = null; async onload() { await this.loadSettings(); // Register command to update current file this.addCommand({ id: 'update-current', name: 'Update values', checkCallback: (checking: boolean) => { const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); if (markdownView) { if (!checking && markdownView.file) { void this.applyAllRulesToFile(markdownView.file); } return true; } return false; } }); // Register command to update all vault files this.addCommand({ id: 'update-all', name: 'Update values for every file in the vault', callback: () => this.updateAllNotes() }); // Listen for file modifications this.registerEvent( this.app.vault.on('modify', (file) => { if (this.settings.mode !== 'modify') return; // Prevent infinite loops with 2-second cooldown const timeDiff = new Date().getTime() - this.lastRun.getTime(); if (timeDiff < 2000) return; void this.applyAllRulesToFile(file); }) ); } } ``` -------------------------------- ### Apply All Rules to File (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt The main method for processing a file, applying all enabled auto-property rules, and updating its frontmatter. It checks for path exclusions, reads file content if not provided, and iterates through existing and potential properties to update them based on evaluated rules. It can optionally report the total number of changes made. ```typescript async applyAllRulesToFile( file: TFile, content?: string, reportTotalChanges = true ) { // Check if file is in ignored path let inIgnoredPath = false; this.settings.pathsToIgnore.forEach(path => { if (file.path.startsWith(path)) inIgnoredPath = true; }); if (inIgnoredPath) return; // Get content if not provided if (!content) content = await this.app.vault.read(file); const bodyContent = AutoPropertyPlugin.extractBodyLines(content).join('\n'); let propertiesChanged = 0; await this.app.fileManager.processFrontMatter(file, frontmatter => { const keys = Object.keys(frontmatter); // Process existing properties keys.forEach(key => { const matchedProperty = this.settings.autopropertySettings.find( autoProp => autoProp.key === key && autoProp.enabled ); if (!matchedProperty) return; const newValue = AutoPropertyPlugin.getValue(matchedProperty, bodyContent, file); if (frontmatter[key] !== newValue) { frontmatter[key] = newValue; propertiesChanged++; } }); // Auto-add properties if enabled this.settings.autopropertySettings.forEach(autoProp => { if (autoProp.autoAdd && !keys.includes(autoProp.key)) { const newValue = AutoPropertyPlugin.getValue(autoProp, bodyContent, file); if (newValue !== undefined && newValue !== null && newValue !== '') { frontmatter[autoProp.key] = newValue; propertiesChanged++; } } }); }); // Show notification if (reportTotalChanges && propertiesChanged > 0 && this.settings.showNotices) { new Notice(`Updated ${propertiesChanged} auto-propert${propertiesChanged === 1 ? 'y' : 'ies'}`); } } ``` -------------------------------- ### Check if a Line Matches a Rule with Modifiers (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt A static utility function that determines if a given line of text conforms to a specific rule defined in `AutoPropRule`. It supports case-insensitive matching, trimming whitespace, and various matching strategies like startsWith, contains, endsWith, and regex. ```typescript static lineMatchesRule(line: string, autoProp: AutoPropRule): boolean { if (autoProp.modifierCaseSensitive === 'insensitive') { line = line.toLowerCase(); autoProp.ruleValue = autoProp.ruleValue.toLowerCase(); } if (autoProp.rulePartTwo === 'startsWith') { if (autoProp.modifierWhitespace === 'trim') return line.trim().startsWith(autoProp.ruleValue); return line.startsWith(autoProp.ruleValue); } else if (autoProp.rulePartTwo === 'contains') { return line.includes(autoProp.ruleValue); } else if (autoProp.rulePartTwo === 'endsWith') { if (autoProp.modifierWhitespace === 'trim') return line.trim().endsWith(autoProp.ruleValue); return line.endsWith(autoProp.ruleValue); } else if (autoProp.rulePartTwo === 'regex') { return new RegExp(autoProp.ruleValue).test(line.trim()); } return false; } // Usage examples const taskRule = { rulePartTwo: 'startsWith', ruleValue: '- [ ]', modifierWhitespace: 'trim', modifierCaseSensitive: 'insensitive' }; AutoPropertyPlugin.lineMatchesRule(' - [ ] My task', taskRule); // true AutoPropertyPlugin.lineMatchesRule('- [x] Done', taskRule); // false const tagRule = { rulePartTwo: 'contains', ruleValue: '#project', modifierCaseSensitive: 'insensitive' }; AutoPropertyPlugin.lineMatchesRule('Work on #project today', tagRule); // true ``` -------------------------------- ### Extract Note Body Lines Excluding YAML Frontmatter (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt A static method to parse raw file content and return an array of strings representing the note's body, effectively removing any YAML frontmatter. It handles cases with and without frontmatter. ```typescript static extractBodyLines(fileRawText: string): string[] { const lines = fileRawText.split(/\r\n|\r|\n/); if (lines[0] !== '---') { return lines; // No frontmatter present } let i = 1; while (i < lines.length && lines[i] !== '---') { i++; } if (i < lines.length) i++; // Skip closing '---' return lines.slice(i); } // Usage example const noteContent = `--- title: My Note tags: [daily, work] --- # Heading - [ ] First task - [x] Completed task - [ ] Another task`; const bodyLines = AutoPropertyPlugin.extractBodyLines(noteContent); // Result: ['', '# Heading', '- [ ] First task', '- [x] Completed task', '- [ ] Another task'] ``` -------------------------------- ### Detect and Extract Obsidian Block IDs (TypeScript) Source: https://context7.com/aarongilly/obsidian-auto-properties/llms.txt Provides utility functions to detect if a line contains an Obsidian block ID and to extract that ID. This is used within the `getValue` method to transform lines with block IDs into clickable Obsidian links, where the link text is the content of the line excluding the block ID. ```typescript static hasLinkTarget(textLine: string): boolean { return /\s+\^\w+$/.test(textLine); } static extractBlockIdFromLine(textLine: string): string { const match = textLine.match(/\s+\^\w+$/); return match ? match[0] : ''; } // Example usage in getValue method matches = matches.map(matchedLine => { if (!AutoPropertyPlugin.hasLinkTarget(matchedLine)) return matchedLine; const blockId = AutoPropertyPlugin.extractBlockIdFromLine(matchedLine); let valueLessBlockId = matchedLine.split(blockId).join(''); return `[[#${blockId.trim()}|${valueLessBlockId}]]`; }); // Example: Line with block ID const line = '- [ ] Important task ^task123'; AutoPropertyPlugin.hasLinkTarget(line); // true AutoPropertyPlugin.extractBlockIdFromLine(line); // ' ^task123' // After processing: '[[#^task123|- [ ] Important task]]' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.