hi
i used the script on one of my vaults and was working…
possibly, you have a space or two after tags:
??
then use:
import os
import platform
import re
# Set root directory based on platform
if platform.system() == "Windows":
root_directory = r'C:\Users\<username>\Documents\Obsidian\TEST'
elif platform.system() == "Linux":
root_directory = '/home/<username>/Documents/Obsidian/TEST'
else:
# Placeholder for macOS
root_directory = '/path/to/Macintosh/Obsidian/TEST'
# Regex to find the tags section and ensure section ends when a line does not start with ' - '
tags_section_regex = re.compile(r'(tags:\s{0,8}\n(?:\s{2}- .+\n)*)(?=\S)')
# Regex to add # and quotes unless they have been dealt with before
tag_value_regex = re.compile(r'(\s{2}- )(?!"#)(.+)')
# Function to process a single file
def process_file(filepath):
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
# Find and process each tags section
def replace_tags_section(match):
tags_section = match.group(1)
modified_tags_section = tag_value_regex.sub(r'\1"#\2"', tags_section)
return modified_tags_section
new_content = tags_section_regex.sub(lambda match: replace_tags_section(match), content)
with open(filepath, 'w', encoding='utf-8') as file:
file.write(new_content)
# Walk through all files in the root directory
for root, dirs, files in os.walk(root_directory):
for file in files:
if file.endswith('.md'): # Assuming markdown files
process_file(os.path.join(root, file))
print("Processing complete.")
changed tags_section_regex to allow some spaces after tags:
try now if you will!!