Last active
January 21, 2025 15:51
-
-
Save mthomason/213b3218810cde77c3d23f75f1bb0785 to your computer and use it in GitHub Desktop.
Create an Index file for daily notes. Edit script to add path to your vault. May also need to edit the regex for the date format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Author: Michael Thomason <[email protected]> | |
# Copyright (C) 2025 Michael Thomason, All rights reserved. | |
# License: MIT | |
# Daily Journal Index Generator | |
# | |
# This script generates an index file for daily journal notes stored in a specific directory. | |
# It scans the specified directory for files named with a date (e.g., YYYY-MM-DD format by default), | |
# sorts them in reverse chronological order, and creates an index file with links to each journal entry. | |
# | |
# How to use: | |
# 1. Update the `DAILY_NOTES_DIR` and `NOTES_DIR` constants to match your directories. | |
# 2. Run this script. The index file will be created in the `NOTES_DIR` directory. | |
# 3. Customize the `DATE_FORMAT_REGEX` parameter if your daily notes use a different date format. | |
# 4. Set `PRINT_WARNING` to False, unless you have a warning file. | |
# | |
# License: This script is available under the MIT License. | |
import os | |
import re | |
from datetime import datetime | |
# Constants: Update these paths as needed | |
DAILY_NOTES_DIR: str = "/Users/michael/Projects/scratch/notes/Notes/DailyNotes/" | |
NOTES_DIR: str = "/Users/michael/Projects/scratch/notes/Notes/" | |
INDEX_NAME: str = "Index Daily Journal.md" | |
DATE_FORMAT_REGEX: str = r"^\d{4}-\d{2}-\d{2}" | |
PRINT_WARNING: bool = True | |
def generate_journal_index( | |
daily_notes_dir: str, | |
notes_dir: str, | |
index_filename: str = INDEX_NAME, | |
date_format_regex: str = DATE_FORMAT_REGEX | |
) -> None: | |
""" | |
Generates an index file for daily journal notes in a specified directory, grouped by month. | |
Args: | |
daily_notes_dir (str): Path to the directory containing daily notes. | |
notes_dir (str): Path to the main notes directory where the index file will be created. | |
index_filename (str): Name of the index file to create. Defaults to 'Index Daily Journal.md'. | |
date_format_regex (str): Regular expression to match filenames with dates. Defaults to YYYY-MM-DD format. | |
Raises: | |
FileNotFoundError: If the specified directories do not exist. | |
""" | |
# Ensure the provided directories exist | |
if not os.path.isdir(daily_notes_dir): | |
raise FileNotFoundError( | |
f"Daily notes directory not found: {daily_notes_dir}" | |
) | |
if not os.path.isdir(notes_dir): | |
raise FileNotFoundError(f"Notes directory not found: {notes_dir}") | |
# Compile regex to match filenames starting with a date | |
date_pattern = re.compile(date_format_regex) | |
# Collect and sort journal filenames in reverse chronological order | |
journal_files: list[str] = [ | |
filename for filename in os.listdir(daily_notes_dir) if date_pattern.match(filename) | |
] | |
journal_files.sort(reverse=True) | |
# Group files by month and year | |
grouped_files: dict[str, list[str]] = {} | |
for filename in journal_files: | |
date_str = filename[:7] # Extract YYYY-MM | |
if date_str not in grouped_files: | |
grouped_files[date_str] = [] | |
grouped_files[date_str].append(filename) | |
# Path for the index file | |
index_file_path = os.path.join(notes_dir, index_filename) | |
# Create or overwrite the index file | |
with open(index_file_path, "w") as index_file: | |
# Write the header and metadata | |
index_file.write("# Daily Journal Index\n\n") | |
if PRINT_WARNING: | |
index_file.write("![[Warning]]\n\n") | |
index_file.write( | |
f"_This file is autogenerated. " | |
f"Last updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}_\n\n" | |
) | |
# Write the top-level index with links to each month | |
index_file.write("## Index by Month\n\n") | |
for month_year in sorted(grouped_files.keys(), reverse=True): | |
# Convert YYYY-MM to "Month, Year" format | |
month_name = datetime.strptime(month_year, "%Y-%m").strftime("%B, %Y") | |
index_file.write(f"* [[#{month_year}|{month_name}]]\n") | |
index_file.write("\n") | |
# Write the grouped journal entries | |
for month_year, files in grouped_files.items(): | |
# Convert YYYY-MM to "Month, Year" format for the header | |
month_name = datetime.strptime(month_year, "%Y-%m").strftime("%B, %Y") | |
index_file.write(f"## {month_name}\n\n") | |
for filename in files: | |
index_file.write(f"* [[{filename}]]\n") | |
index_file.write("\n") | |
print( | |
f"Index file created at '{index_file_path}' " | |
f"with {len(journal_files)} entries." | |
) | |
if __name__ == "__main__": | |
# Example usage | |
generate_journal_index( | |
daily_notes_dir=DAILY_NOTES_DIR, | |
notes_dir=NOTES_DIR | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment