Created
October 25, 2022 12:22
-
-
Save SubhadityaMukherjee/24cad590043f1a2dc0d264251c0a7ef3 to your computer and use it in GitHub Desktop.
Create Tag pages script for Obsidian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from glob import glob | |
from pathlib import Path | |
import re | |
#%% | |
main_dir = Path("./docs/") # Change this to where your Obsidian Vault notes are | |
# %% | |
files = glob(str(main_dir) + "/**/*.md", recursive=True) # This looks at all markdown files | |
files.sort() | |
tag_list = ["architecture", "language", "visualization", "dataset", "psychology"] # Add all the tags you care about | |
dict_tags = {x:[] for x in tag_list} | |
# Create a dictionary with files and their tags | |
print("Creating index") | |
for i in files: | |
with open(i, "r+") as myfile: | |
print(i) | |
try: | |
head = [next(myfile) for x in range(5)] # read first n lines | |
head = [x for x in head if "tag" in x][0].strip().split(" ")[1::] | |
for tag in head: | |
try: | |
dict_tags[tag].append(i) | |
except: | |
pass | |
except StopIteration: | |
pass | |
#print(dict_tags) | |
# Create files with tag pages | |
f_string = """ | |
--- | |
tags: anchor | |
___ | |
""" | |
for tag in tag_list: | |
print(tag) | |
files_in_tag = dict_tags[tag] | |
with open(f"docs/{tag}.md", "w+") as tagfile: | |
tagfile.write(f_string) | |
for fle in files_in_tag: | |
fle = fle.replace('docs/','').replace(" ", "%20") | |
tagfile.write(f"- [{fle.replace('%20', ' ')}]({fle})\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment