I had 4000+ notes in Markdown. Each note had same structure as following example:
# HowtoForge
- Tags: #documentation, #guide, #hacks
- Description: Linux Howtos and Tutorials.
- Created_at: 2011-09-28 09:49:45
- URL Response: #StatusCode/200
- URL: https://www.howtoforge.com
I want to refactor those 4000+ bookmark notes utilizing the YAML frontmatter feature for better searching. It should look like:
---
tags:
- documentation
- "#guide"
- "#hacks"
description: Linux Howtos and Tutorials.
created: 2011-09-28
http response: "200"
url: https://www.howtoforge.com
---
# HowtoForge
How to refactor such bullet points in markdown to YAML frontmatter?
What the best way to do so? I’m open to any programming framework like python, nodejs, or others
Here my dirty script in Python to accomplish this:
import yaml
import os
notes_directory = r'C:\Obsidian\My Notes\References\Bookmarks'
for filename in os.listdir(notes_directory):
if filename.endswith('.md'):
file_path = os.path.join(notes_directory, filename)
print(file_path)
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
for line in lines:
# check if string present on a current line
if line.find('- Tags:') != -1:
overwrite_file_flag = True
tags_list = line.split(':')[1].split(',')
tags_list = [tag.replace(' ', '').replace('\n','') for tag in tags_list]
tags_dict = {}
tags_dict['tags'] = tags_list
tags_yaml = yaml.dump(tags_dict, explicit_start=True, default_flow_style=False,
default_style=None, indent=4)
else:
overwrite_file_flag = False
if line.find('- Description:') != -1:
overwrite_file_flag = True
description_str = line.split(':')[1].replace('\n','').strip()
description_dict = {}
description_dict['description'] = description_str
description_yaml = yaml.dump(description_dict, default_flow_style=False,
default_style=None, indent=4)
else:
overwrite_file_flag = False
if line.find('- Created_at:') != -1:
overwrite_file_flag = True
created_str = line.split(' ')[2]
created_dict = {}
created_dict['created'] = created_str
created_yaml = yaml.dump(created_dict, default_flow_style=False,
default_style=None, indent=4)
else:
overwrite_file_flag = False
if line.find('- URL Response:') != -1:
overwrite_file_flag = True
http_response_str = line.split('/')[-1].replace(' ','').replace('\n','')
http_response_dict = {}
http_response_dict['http_response'] = http_response_str
http_response_yaml = yaml.dump(http_response_dict, default_flow_style=False,
default_style=None, indent=4)
else:
overwrite_file_flag = False
if line.find('- URL:') != -1:
overwrite_file_flag = True
url_str = line.split(': ')[-1].replace('\n','')
url_dict = {}
url_dict['url'] = url_str
url_yaml = yaml.dump(url_dict, default_flow_style=False,
default_style=None, indent=4)
else:
overwrite_file_flag = False
if line.find('# ') != -1:
h1_str = line
if overwrite_file_flag:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(tags_yaml)
file.write(description_yaml)
file.write(created_yaml)
file.write(http_response_yaml)
file.write(url_yaml + '---')
file.write('\n' + h1_str)
system
Closed
December 11, 2024, 10:07am
3
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.