#!/usr/bin/env python3 """ Regenerates the notes index in notes.html based on files in notes/youtube/ Run after adding new notes. """ import os import re import json from pathlib import Path NOTES_DIR = Path(__file__).parent.parent / 'notes' / 'youtube' NOTES_HTML = Path(__file__).parent.parent / 'kanban' / 'notes.html' def extract_metadata(filepath): """Extract title, date, tags from markdown file.""" with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # Extract title (first # heading) title_match = re.search(r'^# (.+)$', content, re.MULTILINE) title = title_match.group(1) if title_match else filepath.stem # Extract date from filename (YYYY-MM-DD_...) date_match = re.match(r'(\d{4}-\d{2}-\d{2})', filepath.name) date = date_match.group(1) if date_match else "Unknown" # Extract tags tags_match = re.search(r'\*\*Tags?:\*\*\s*(.+)$', content, re.MULTILINE) if tags_match: tags = [t.strip().replace('#', '') for t in tags_match.group(1).split(',')] else: # Try to find hashtags tags = re.findall(r'#(\w+)', content)[:5] return { 'file': filepath.name, 'title': title[:50], # Truncate long titles 'date': date, 'tags': tags[:4] # Max 4 tags } def update_index(): """Scan notes directory and update the HTML index.""" if not NOTES_DIR.exists(): print(f"Notes directory not found: {NOTES_DIR}") return # Get all markdown files notes = [] for f in sorted(NOTES_DIR.glob('*.md'), reverse=True): # Newest first notes.append(extract_metadata(f)) # Read current HTML with open(NOTES_HTML, 'r', encoding='utf-8') as f: html = f.read() # Update the notesIndex index_json = json.dumps(notes, indent=12, ensure_ascii=False) # Replace the notesIndex in HTML pattern = r'const notesIndex = \[[\s\S]*?\];' replacement = f'const notesIndex = {index_json};' new_html = re.sub(pattern, replacement, html) with open(NOTES_HTML, 'w', encoding='utf-8') as f: f.write(new_html) print(f"Updated index with {len(notes)} notes:") for n in notes: print(f" - {n['date']}: {n['title']}") if __name__ == '__main__': update_index()