Created
March 23, 2025 16:25
-
-
Save BesrourMS/35264d3fbb797bfa305f5f7040f26672 to your computer and use it in GitHub Desktop.
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
def generate_filename(title): | |
"""Generate a filename from the title by converting to lowercase, replacing spaces with hyphens, and keeping only alphanumeric characters and hyphens.""" | |
filename = title.lower().replace(" ", "-") | |
filename = ''.join(c for c in filename if c.isalnum() or c == '-') | |
filename += ".md" | |
return filename | |
def create_md_file(title, date, description, youtube_id, content, template="post", filename=None): | |
""" | |
Create a Markdown file with YAML front matter, an image part, and content using external data. | |
Parameters: | |
- title (str): The title of the post. | |
- date (str): The date in 'YYYY-MM-DD' format. | |
- description (str): A brief description of the post, also used as alt text for the image. | |
- youtube_id (str): The YouTube video ID for the image link and thumbnail. | |
- content (str): The main body text of the post, can include newlines for paragraphs. | |
- template (str, optional): The template type, defaults to 'post'. | |
- filename (str, optional): The name of the file; if None, generated from the title. | |
""" | |
# Use provided filename or generate one from the title | |
if filename is None: | |
filename = generate_filename(title) | |
# Construct the YAML front matter | |
yaml = f"""--- | |
Title: {title} | |
Date: {date} | |
Description: {description} | |
Template: {template} | |
---""" | |
# Construct the image part with YouTube link and thumbnail | |
img_part = f"""<a href="https://www.youtube.com/watch?v={youtube_id}"> | |
<img src="https://img.youtube.com/vi/{youtube_id}/maxresdefault.jpg" alt="{description}" class="img-fluid"> | |
</a>""" | |
# Combine all parts with blank lines between them | |
full_content = f"{yaml}\n\n{img_part}\n\n{content}" | |
# Write the content to the file with UTF-8 encoding | |
with open(filename, "w", encoding="utf-8") as file: | |
file.write(full_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment