The Markdown File Tagger is a Python script designed to automatically tag Markdown files based on their folder structure. It scans through a given folder tree and appends tags to the front matter of each Markdown file. These tags are generated from the names of the folders that contain the file.
- Automatic Tagging: The script automatically generates tags based on folder names.
- Front Matter Support: The script detects existing front matter sections and places the tags appropriately. If a file doesn't have front matter, the script will create it and add the tags.
- Idempotent: Running the script multiple times on the same folder will not duplicate tags.
- Markdown Files Only: The script will only process markdown files, which should prevent errors when running it on directories containing other types of files.
- Customizable: Easy to adapt and extend for more specialized tagging needs.
- Python 3.x
Run the script from the command line and pass in the root folder you'd like to start tagging from:
tagger_md /path/to/root/folder
This will recursively go through all the Markdown files in that folder and its subfolders, appending or updating a tag section to each.
For a directory tree like:
Test
└── test.md
The test.md file will get a tag Test added to its front matter like this:
---
tags:
- Test
---
For a more complex directory tree like:
Test
└── Test_1
├── test.md
The test.md file will get tags Test and Test_1 added to its front matter like this:
---
tags:
- Test
- Test_1
---
You can also specify additional tags to be added to all files:
tagger_md /path/to/root/folder tag1 tag2
This will add 'tag1' and 'tag2' to all files, in addition to the directory-based tags.
For those who used the first version and want to switch over, I wrote a script called cleanup_scripts/cleanup.py. It will remove the #### Tags section from all files in a directory tree. It's not pretty, but it works. Here is the usage:
python cleanup.py /path/to/root/folder
If you ran a version of the script that added duplicate frontmatters and want to start over, run cleanup_scripts/cleanup_dupes.py. This will clear all frontmatter from all files in a directory tree. Here is the usage:
python cleanup_dupes.py /path/to/root/folder