Skip to content

Commit

Permalink
👌 IMPROVE: Updated date output and post sorting by date
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdevore committed Jan 7, 2025
1 parent 80e90e9 commit ffa2603
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions stattic.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@ def replace_srcset(match):

return content

def parse_date(self, date_str):
"""Parse the date string into a datetime object."""
if isinstance(date_str, datetime):
return date_str
elif isinstance(date_str, date):
return datetime(date_str.year, date_str.month, date_str.day)
elif isinstance(date_str, str):
for fmt in ['%Y-%m-%dT%H:%M:%S', '%Y-%m-%d', '%b %d, %Y']:
try:
return datetime.strptime(date_str, fmt)
except ValueError:
continue
self.logger.warning(f"Unable to parse date: {date_str}. Using minimum date as fallback.")
return datetime.min # Fallback to the earliest date for sorting

def parse_markdown_with_metadata(self, filepath):
"""Extract frontmatter and markdown content from the file, process images."""
try:
Expand All @@ -498,20 +513,21 @@ def parse_markdown_with_metadata(self, filepath):

# Check if the content contains frontmatter (starts with ---)
if content.startswith('---'):
# Split into frontmatter and content
parts = content.split('---', 2) # Splitting into 3 parts: '', frontmatter, content
if len(parts) == 3: # Proper frontmatter and content found
if len(parts) == 3:
frontmatter, markdown_content = parts[1], parts[2]
metadata = yaml.safe_load(frontmatter) or {}
else:
# Malformed frontmatter, fallback to handling as plain markdown
self.logger.warning(f"Malformed frontmatter in {filepath}. Treating entire content as markdown.")
metadata, markdown_content = {}, content
else:
# No frontmatter at all, treat entire content as markdown
self.logger.info(f"No frontmatter in {filepath}. Treating as pure markdown.")
metadata, markdown_content = {}, content

# Parse and normalize the date in metadata
if 'date' in metadata:
metadata['date'] = self.parse_date(metadata['date'])

# Process images in the markdown content
markdown_content = self.process_images(markdown_content)

Expand Down Expand Up @@ -719,10 +735,15 @@ def generate_excerpt(self, content):
def build_index_page(self):
"""Render and build the index (homepage) with the list of posts."""
try:
# Sort posts by date in descending order and take the most recent ones
# Ensure all posts have a valid parsed date for sorting
for post in self.posts:
post_date_str = post.get('date', '')
post['parsed_date'] = self.parse_date(post_date_str) # Add parsed_date for sorting

# Sort posts by parsed_date in descending order
posts_for_index = sorted(
self.posts,
key=lambda post: self.format_date(post.get('date', '')),
key=lambda post: post.get('parsed_date', datetime.min), # Fallback to the earliest date
reverse=True
)[:self.posts_per_page]

Expand Down

0 comments on commit ffa2603

Please sign in to comment.