Skip to content

Instantly share code, notes, and snippets.

@therumbler
Created October 3, 2023 16:14
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 therumbler/af66965b7ccda18de899e8adea022be8 to your computer and use it in GitHub Desktop.
Save therumbler/af66965b7ccda18de899e8adea022be8 to your computer and use it in GitHub Desktop.
Turn a folder of markdown files into a JSON Feed object
import os
from datetime import datetime
def filename_to_id(filename):
return f"https://example.com/posts/{filename.replace('.md', '')}"
def markdown_filepath_to_html(filepath):
return "<h1>Implement Me</h1>"
def filepath_to_json_feed_item(filepath, filename):
stat = os.stat(filepath)
date_published = datetime.fromtimestamp(stat.st_atime)
return {
"id": filename_to_id(filename),
"date_published": date_published.isoformat(),
"title": filename.replace(".md", ""),
"content_html": markdown_filepath_to_html(filepath),
}
def create_json_feed_items():
items = []
for root, dirs, files in os.walk("./posts"):
for filename in files:
filepath = f"{root}/{filename}"
items.append(filepath_to_json_feed_item(filepath, filename))
return items
def create_json_feed():
items = create_json_feed_items()
return {
"version": "https://jsonfeed.org/version/1.1",
"title": "My Example Feed",
"home_page_url": "https://example.org/",
"feed_url": "https://example.org/feed.json",
"items": items,
}
def main():
feed = create_json_feed()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment