thomaswilson-sveltekit/scripts/list-posts.py

49 lines
1.4 KiB
Python
Raw Normal View History

import glob
import frontmatter
import json
from datetime import datetime
from datetime import date
from dateutil.parser import *
2022-05-27 19:35:55 +00:00
from markdown import markdown
"""
Build a hashamp of all blog posts and book reviews used in the blog.
This is used on both the blog/index page, and the blog/[svelte] pages to
render out a list of posts (and fetch more detail) to prevent the need
to re-fetch on every request
"""
blog_posts = glob.glob('./src/content/**/*.md')
hash_map = {}
for file in blog_posts:
2022-05-06 21:21:44 +00:00
try:
post = frontmatter.load(file)
slug = post['slug']
frontmatter_keys = post.keys()
published_date = post['date']
# We want a datetime, note a date
if isinstance(published_date, date):
published_date = datetime.combine(published_date, datetime.min.time())
details = {
'title': post['title'],
2022-05-27 19:35:55 +00:00
'author': post['author'],
2022-05-06 21:21:44 +00:00
'date': published_date.isoformat(),
'book_review': 'book_review' in frontmatter_keys,
'preview': post.content[0:180],
2022-05-27 19:35:55 +00:00
'content': markdown(post.content),
2022-05-06 21:21:44 +00:00
'slug': slug
}
hash_map[slug] = details
except:
print("!!!")
print("Caught Error in following file, ignoring")
print(file)
print("---")
with open('./src/content/posts.json', "w") as file:
json.dump(hash_map, file)