24 August 20251 min read

Today I learned how to strip out YAML frontmatter (--- … ---) from Markdown files using either Python or a shell script.

def remove_frontmatter(md_content):
if md_content.startswith("---"):
parts = md_content.split("---", 2)
if len(parts) > 2:
return parts[2].lstrip("\n")
return md_content

#!/bin/sh
if [ -f "$file" ] && head -n1 "$file" | grep -q '^---$'; then
sed '1,/^---$/d' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
fi

Key takeaway: Whether in Python or shell, the idea is the same—detect if the file starts with ---, then strip out everything up to the next ---.

Back to /til→
𖥸
Email Me
Thanks for reading! If you found this page useful, considerbuying me a coffee
No CopyrightCC0 1.0
PREV
NEXT