forked from https://gist.github.com/kmalloy24/6a4e20d30a773a7fc13ebdd4331cc73d
- Clone the repository into a predictable path within your home directory.
- Create a symlink in
~/.local/bin/md-milename-as-titlepointing atmd-filename-as-title.py
forked from https://gist.github.com/kmalloy24/6a4e20d30a773a7fc13ebdd4331cc73d
~/.local/bin/md-milename-as-title pointing at md-filename-as-title.py| #!/usr/bin/env python | |
| import os | |
| import re | |
| # Function to generate title from filename | |
| def generate_title(filename): | |
| # Remove the extension | |
| base = os.path.splitext(filename)[0] | |
| # Remove numbers | |
| base = re.sub(r"\d+", "", base) | |
| # Replace dashes and underscores with spaces | |
| base = base.replace("-", " ").replace("_", " ") | |
| # Convert to title case | |
| title = base.title() | |
| return title.strip() | |
| # Function to add frontmatter to a single file | |
| def add_frontmatter(file_path): | |
| # Extract the filename | |
| filename = os.path.basename(file_path) | |
| # Generate the title | |
| title = generate_title(filename) | |
| # Create the frontmatter content | |
| frontmatter = f"---\ntitle: {title}\n---\n\n" | |
| # Read the original file content with utf-8 encoding | |
| with open(file_path, "r", encoding="utf-8") as file: | |
| content = file.read() | |
| if not content.startswith("---"): | |
| # Write the frontmatter followed by the original content with utf-8 encoding | |
| with open(file_path, "w", encoding="utf-8") as file: | |
| file.write(frontmatter + content) | |
| print(f"Frontmatter added to {file_path}") | |
| # Function to process all markdown and mdx files in the current directory | |
| def process_all_files(): | |
| # Use the current directory | |
| current_directory = os.getcwd() | |
| for filename in os.listdir(current_directory): | |
| if filename.endswith(".md") or filename.endswith(".mdx"): | |
| file_path = os.path.join(current_directory, filename) | |
| try: | |
| add_frontmatter(file_path) | |
| except UnicodeDecodeError as e: | |
| print(f"Failed to process {filename} due to encoding error: {e}") | |
| # Process all files in the current directory | |
| process_all_files() |