import os import re import yaml from datetime import datetime
Define the directory containing the Markdown files
CONTENT_DIR = “/Users/m1bob/code/Jeremy-Huss/content”
Helper function to convert date to ISO 8601 format
def fix_date_format(date_string): # If it’s already a datetime, return it as ISO 8601 if isinstance(date_string, datetime): return date_string.isoformat()
# If it's a string, attempt to parse it
if isinstance(date_string, str):
try:
# Attempt to parse the date using common formats
parsed_date = datetime.strptime(date_string, "%a, %d %b %Y %H:%M:%S %z")
return parsed_date.isoformat() # Convert to ISO 8601 format
except ValueError:
try:
# Try parsing ISO 8601 formatted string
parsed_date = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S%z")
return parsed_date.isoformat() # Already in ISO 8601, return as is
except ValueError:
print(f"Could not parse date: {date_string}")
return date_string # Return the original if it can't be parsed
return None
Function to fix front matter errors
def fix_front_matter(content): # Extract the front matter using regex (YAML front matter is between “—”) front_matter_regex = re.compile(r"^—(.*?)—", re.DOTALL) match = front_matter_regex.search(content)
if match:
front_matter_str = match.group(1)
front_matter = yaml.safe_load(front_matter_str)
# Check if "date" field exists and is not None
if "date" in front_matter and front_matter["date"]:
corrected_date = fix_date_format(front_matter["date"])
if corrected_date:
front_matter["date"] = corrected_date
else:
print(f"No date field found, skipping file.")
return content # No changes if no date is present
# Convert back to YAML and replace in the content
new_front_matter_str = yaml.dump(front_matter, default_flow_style=False)
new_content = f"---\n{new_front_matter_str}---\n{content[match.end():]}"
return new_content
return content
Function to process each Markdown file in the directory
def process_markdown_files(directory): for root, _, files in os.walk(directory): for file in files: if file.endswith(".md"): file_path = os.path.join(root, file)
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
# Fix the front matter in the file content
updated_content = fix_front_matter(content)
# Write the updated content back to the file
if updated_content != content:
with open(file_path, "w", encoding="utf-8") as f:
f.write(updated_content)
print(f"Updated: {file_path}")
else:
print(f"No changes: {file_path}")
Run the script
if name == “main”: process_markdown_files(CONTENT_DIR)