MarkdownFrontmatter.zip (5.9 KB)
Ok, here it is. A cool 695 lines of code. But the result should be worthwhile. It’s only minimally tested. But at least I did stress test it with >2,000 (small) markdown files (note that Obsidian really isn’t very happy when 2,000 files are modified while it’s open).
Once I figure out how, I’ll publish this to PowerShellGet to make it easier to access and update.
I can provide guidance of downloading, placing and importing the module into your PowerShell environment if you need. But I wanted to get to the fun bit - how to use it to solve your specific issue:
(excuse my formatting, too, I’m new into the Obsidian forum)
Import-Module .\MarkdownFrontmatter.psm1
$VaultPath = "C:\MyVault\Diary"
Add-MarkdownFrontmatter -Path $VaultPath -List "tags" -Item "m/diary"
The above will ADD the item to existing tags, leaving any other tags you have intact. If, instead, you ONLY want the tag to be m/diary, you could use this:
# overwrite the tags property with this array of (a single item) m/diary
Add-MarkdownFrontmatter -Path $VaultPath -Property "tags" -Value @("m/diary") -Force
If you’re worried about what it’ll do, add -WhatIf to the list of parameters. If you really love pressing enter, add -Confirm to the parameters and it will confirm each file update.
By default this will also backup any .md file to .bak. If that’s too much, add -NoBackup to the parameters to avoid that.
Only want to do the one folder and not all subfolders? -NoRecurse
If you’re new into PowerShell you can find the commands I’ve created with:
Get-Command -Module MarkdownFrontmatter
And each command has full help and examples for use:
Get-Help Remove-MarkdownFrontmatter -ShowWindow
I’d love to know:
- does it work
- does it work on a Mac
Oh, sick. I just worked out that the Obsidian forum uses markdown. Well, derrrr.
PS> Get-Help Add-MarkdownFrontmatter
NAME
Add-MarkdownFrontmatter
SYNOPSIS
Adds properties to the frontmatter of markdown files
SYNTAX
Add-MarkdownFrontmatter -Path <String[]> -Property <String> -Value <Object> [-NoRecurse] [-NoBackup] [-Force]
[-WhatIf] [-Confirm] [<CommonParameters>]
Add-MarkdownFrontmatter -Path <String[]> -List <String> -Item <String[]> [-NoRecurse] [-NoBackup] [-Force]
[-WhatIf] [-Confirm] [<CommonParameters>]
DESCRIPTION
Adds properties to frontmatter if it doesn't exist, and assigns a value to it. If the property already exists, it
will not be overwritten unless the Force switch is used. Can also be used to add items to a list in the
frontmatter.