Skip to content

Instantly share code, notes, and snippets.

@kjaymiller
Created April 16, 2023 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjaymiller/83175df30291c885508ffa1129ee85c4 to your computer and use it in GitHub Desktop.
Save kjaymiller/83175df30291c885508ffa1129ee85c4 to your computer and use it in GitHub Desktop.
Script to convert all date-like frontmatter attributes to iso8601
# Create a script that iterates through the content folder and looks
# install dateutil, frontmatter
# `pip install python-dateutil frontmatter`
# Run with `python convert_dates.py <CONTENT_DIRECTORY>`
from sys import argv
from dateutil.parser import parse
import frontmatter
import pathlib
date_values = [
"date",
"Date",
"date_published",
"date_modified",
"published_date",
"modified_date",
]
def iterate_through_content(path: pathlib.Path):
for file in path.rglob("**/*.md"):
post = frontmatter.loads(file.read_text())
changed = False
for date_value in date_values:
if post.get(date_value, None):
if isinstance(post[date_value], str):
post[date_value] = parse(post[date_value]).astimezone()
changed = True
print(f"Updating {file.name}: {date_value} to {post[date_value]}...")
if changed:
print(f"Updating {file}...")
new_post = frontmatter.dumps(post)
file.write_text(new_post)
if __name__ == "__main__":
path = pathlib.Path(argv[1])
iterate_through_content(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment